#!/bin/bash


# error message, set return value
function fail()
{
  echo -e "\nError: $@\n"
  false
}

# die with message
function die()
{
  echo -e "\nError: $@\n"
  exit 1
}

# die with red message
function Rdie()
{
  Recho "\nError: $@\n"
  exit 1
}

# echo to stderr, don't catch if die is called in VAR=$(func) statements
function Rdie2()
{
  Recho "\n $@ \n" 1>&2
  exit 1
}

# *A*    b
function ABecho()
{
  printf "\E[1m %-35s \E[0m %s \n" "$1" "$2"
}

function HeadEcho()
{
  printf "%-18s %s\n" "$1" "$2"
}

function IndentEcho()
{
  local data="$1"
  local pre="$2"

  while read -r line; do
    echo "$pre$line"
  done <<< "$data"
}

# echo bold
function BDecho()
{
  echo -ne "\E[1m$@\E[0m\n"
}

function Recho()
{
  # force black background: "\E[1;31;40m$@\E[0m\n"
  echo -ne "\E[1;31m$@\E[0m\n"
}

function Gecho()
{
  # force black background: "\E[1;32;40m$@\E[0m\n"
  echo -ne "\E[1;32m$@\E[0m\n"
}

function Yecho()
{
  # force black background: "\E[1;33;40m$@\E[0m\n"
  #        auto background: "\E[1;33m$@\E[0m\n"
  echo -ne "\E[1;33;40m$@\E[0m\n"
}

# logging

function Techo()
{
  echo "$@" | tee --append "$TV_STARTLOG"
}

function TeeLog()
{
  local append=$( [ "$1" = 'reset' ] || echo '--append' )
  tee $append "$TV_STARTLOG"
}

function Log()
{
  cat >> "$TV_STARTLOG"
}

# other commands

function cmdExists()
{
  command -v "$1" >/dev/null 2>&1
}

function isStrAscii()
{
  local str="$1"

  # echo -n "$str" | od -x -w2 | cut -d' ' -f2 | grep -E '^[89abcdef]|^..[89abcdef]' # also works
  for (( i=0; i<${#str}; i++ )); do
    (( $(printf '%d' \'${str:$i:1}) > 127 )) && return 1
  done

  return 0
}

function isStrNumeric()
{
  local str="$1"

  [ "$str" -eq "$str" ] 2>/dev/null
}

function getInitCmd()
{
  #exec 2> /dev/null
  readlink /proc/1/exe 2> /dev/null
  #cat /proc/1/cmdline | tr "\000" " " | cut -d' ' -f1
}

# ensure path exists
function make_path()
{
  local path="$1"
  local mode=${2:+-m $2}    # e.g. '-m 755' or ''
  if [ -d "$path" ] ; then
      # Don't fail here, as chmod is not possible on all file systems
      [ -n "$2" ] && chmod $2 "$path"
      true
  else
      mkdir -p $mode "$path" || fail "Could not create $path"
  fi
}

# if sudo is used to run teamviewer the wine-profile could become useless
function validateUser()
{
  if [ -n "$SUDO_UID" ] && [ "$SUDO_UID" != "$UID" ]; then
    Techo -e " *** TeamViewer can not be executed with sudo! ***\n Either use your normal user account without sudo\n or use a the real root account to log in to your desktop (not recommended!).\n"

    chown $SUDO_UID:$SUDO_GID "$TV_STARTLOG"

    return 1
  fi
}


function isSuperUser # root or sudo
{
  local userid=$(id -u)
  [ "$userid" == 0 ]
}

function rootSuggest()
{
  isSuperUser || echo -e "\nTry with root / sudo ?"
  false
}

function isInstalledTV()
{
  [ "$TV_PKGTYPE" == "DEB"    ] && return 0
  [ "$TV_PKGTYPE" == "RPM"    ] && return 0
  [ "$TV_PKGTYPE" == "TAR_IN" ] && return 0
  [ "$TV_PKGTYPE" == "TAR_NI" ] && return 1
  [ "$TV_PKGTYPE" == "TAR_QS" ] && return 1
  
  die 'Invalid package type'
}

function isQuickSupport()
{
  [ "$TV_PKGTYPE" == "TAR_QS" ]
}

function installedTVorDie()
{
  isInstalledTV || die "Only available if TeamViewer is installed"
}

function has32BitSupport()
{
  [ -x "$TV_LD32_PATH" ]
}

function has64BitSupport()
{
  [ -x "$TV_LD64_PATH"  ]
}

function hasIA32Libs()
{
#  cmdExists apt-cache || return
#  apt-cache pkgnames ia32-libs | grep -q '^ia32-libs$'
  cmdExists apt-get || return
  apt-get install ia32-libs --simulate > /dev/null
}

function hasI386Libs()
{
  cmdExists apt-get || return
  apt-get install libasound2:i386 --simulate > /dev/null
}

function CheckSupportedArchitecture()
{
  { has64BitSupport || has32BitSupport; } && return
  
  Rdie "Unsupported architecture:\n\tCould not find $TV_LD32_PATH\n\tCould not find $TV_LD64_PATH"
}

function updateMenuEntries()
{
  local action="$1"						# install / uninstall
  xdg-desktop-menu $action --mode system "$TV_DESKTOP_FILE"	# prefer installed xdg script (tvw_config)

  cmdExists update-menus			&& update-menus
  cmdExists update-desktop-database && update-desktop-database
  cmdExists update-icon-caches		&& update-icon-caches /usr/share/icons/hicolor
}
