#!/bin/bash

TV_SCRIPT_DIR="$(dirname "$(readlink -e "$0")")"
source "$TV_SCRIPT_DIR/tvw_main"

function TVSetup()
{
  CheckInteractive $#

  local opt="$1"
  
  case "$opt" in
    ( '--checklibs'	)	CheckLibs	;;
    ( '--install'	)	InstallTar	;;
    ( *			)	PrintInfo	;;
  esac
}

function CheckInteractive()
{
  [ "$1" = 0 ] || return	# prevent recursion in case of error

  cmdExists tty || return	# fail gracefully
  tty -s
  [ $? = 0 ] && return		# we have a tty - check successful
  
  # try to call self with terminal emulator
  local cmd="$0 --nocheck"
  local geomp="600x400+300+100"
  local geomc="120x40+300+100"
  if cmdExists konsole; then
    konsole --hold -e "$cmd"
  elif cmdExists gnome-terminal; then
    gnome-terminal --geometry=$geomc -e "/bin/bash -c '$cmd; exec bash -i'"
  else
    xterm -geometry $geomc -hold -e "$cmd"
  fi

  exit
}

function CheckLibs()
{
  echo
  ABecho "   -=-   TeamViewer tar.gz check   -=-   "
  echo
  echo "In order to use the tar.gz version of TeamViewer, "
  echo "you have to make sure that the necessary libraries are installed."
  echo "NOTE: All needed libraries are 32 bit libraries, even if you are on a 64 bit system!"
  echo
  echo

  if ! cmdExists ldd; then
    echo "Failed to execute ldd (missing?)"
    exit
  fi

  export LC_ALL="C"	# force english output for ldd
  #tmpfile="/tmp/tv_sys_check.tmp"
  tmpfile="$TV_LOG_DIR/tv_sys_check.log"
  
  echo "Writing raw output to $tmpfile"

  # ldd $TV_BIN_DIR/wine/bin/wine			# pthread
  # ldd $TV_BIN_DIR/wine/lib/wine/winex11.drv.so	# X11, Xext, Xau, Xdmcp

  # TODO validate paths first
  
  echo WINEINET							> "$tmpfile"
  ldd "$TV_BIN_DIR/wine/lib/wine/wininet.dll.so"		>> "$tmpfile"	# z
  echo WINEALSA							>> "$tmpfile"
  ldd "$TV_BIN_DIR/wine/lib/wine/winealsa.drv.so"		>> "$tmpfile"	# asound, pthread, rt
  echo TV_D							>> "$tmpfile"
  ldd "$TV_BIN_DIR/teamviewerd"					>> "$tmpfile"	# pthread, rt, X11, Xau, Xdmcp
  echo TV_DLL							>> "$tmpfile"
  ldd "$TV_BIN_DIR/wine/drive_c/TeamViewer/tvwine.dll.so"	>> "$tmpfile"	# X11, Xext, Xtst, Xdamage, Xfixes, Xau, Xdmcp
  echo TV_LIBDEP						>> "$tmpfile"
  ldd "$TV_BIN_DIR/script/libdepend"				>> "$tmpfile"	# freetype, SM, Xrender, (z, ICE, Xau Xdmcp)
  
  if [ $? != 0 ]; then
    echo "An error occurred."
    cat "$tmpfile" | grep dynamic > /dev/null
    if [ $? = 0 ]; then
      echo
      echo "Your system probably does not support 32 bit binaries (yet)."
      echo "If you are on a 64 bit Intel/AMD system, you might need to install"
      echo "initial 32 bit support by installing a basic 32 bit package, for example:"
      echo -e " libc6:i386\n libc6-i386\n glibc.i686\n ia32-libs\nor similar."
    fi
    echo
    exit
  fi

  echo "Analyzing ..."
  echo
  cat "$tmpfile" | sort | grep found

  if [ $? = 0 ]; then
    echo
    echo "The libraries listed above seem to be missing"
    echo "Find and install the corresponding packages."
  else
    ABecho "All dependencies seem to be satisfied!"
  fi
  
  echo
  echo
}

function InstallTar()
{
  echo "not implemented"
}

function PrintInfo()
{
  echo
  ABecho "How to use TeamViewer (tar.gz)"
  echo
  ABecho "teamviewer" "run teamviewer directly"
  echo "   You can just extract the tar.gz package and run 'teamviewer' without installation."
  echo "   It will behave similar to a TeamViewer Portable or QuickSupport on Windows."
  echo "   This should work if all necessary libraries (packages) are installed on your system."
  echo
  ABecho "tv-setup --checklibs" "identify missing libraries"
  echo "   Run this command to identify missing libraries"
  echo "   You can then look for the matching packages and install them manually."
  echo
  ABecho "install..."
  echo "   A permanent installation with all the features of the rpm/deb packages"
  echo "   (start menu entries, auto start, background daemon for permanent access)"
  echo "   is unfortunately not available in this release."
  echo
  echo
}

TVSetup "$@"