#!/bin/bash

source "$TV_SCRIPT_DIR/tvw_aux"
source "$TV_SCRIPT_DIR/tvw_config"
source "$TV_SCRIPT_DIR/tvw_exec"
source "$TV_SCRIPT_DIR/tvw_extra"
source "$TV_SCRIPT_DIR/tvw_daemon"
source "$TV_SCRIPT_DIR/tvw_profile"


function Main()
{
  local param="$1"
  local opt="$2"
  
  echo
  
  case "$param" in
    --help )		PrintHelp			;;
    --version )		PrintVersion			;;
    --info )		PrintInfo			;;
    --daemon )		Run_Daemon $opt			;;
    --winecfg )		shift; Run_WineCfg "$@"		;;
    --regedit )		shift; Run_RegEdit "$@"		;;
    --kill )		Run_KillTeamViewer		;;
    --update-profile )	Init				;;
    --ziplog )		CreateZipLog			;;
    --passwd )		SetPasswd "$opt"		;;
    --export-license )	ExportLicense "$opt"		;;
    * )			Run_TeamViewer "$@"		;;
  esac
  
  echo
}

function Init()
{
  local startLog="$TV_LOG_DIR/startup.log"

  echo "Init..."
  make_path        "$TV_LOG_DIR"	|| die "Could not create $TV_LOG_DIR"
  LogStartupInfo > "$startLog"		|| die "Init failed. Please check '$startLog'"

  UpdateBinaries >> "$startLog"	|| die "UpdateBinaries failed. Please check '$startLog'"

  echo "Checking setup..."
  InitProfile   >> "$startLog"		|| die "InitProfile failed. Please check '$startLog'"
  
}

function LogStartupInfo()
{
  exec 2>&1					# redirect stderr

  echo "TeamViewer: $TV_VERSION - $TV_PKGTYPE"
  echo "Profile: $HOME ($LOGNAME)"
  echo "Desktop: $DESKTOP_SESSION"

  cmdExists xprop
  if [ $? = 0 ]; then
      XFVT=`xprop -root XFree86_VT | grep INTEGER | cut -f2 -d=`
      [ -n "$XFVT" ] || XVFT="none"
      echo "XServer TTY: $XFVT"
  fi

  if [ -x "$(type -p lsb_release)" ] ; then	# log information about the Linux distribution
    lsb_release -idrc
    make_path "$WINEPREFIX/drive_c"
    lsb_release -ds > "$WINEPREFIX/drive_c/distrelease"
  else
    echo /etc/*-release
    for rfile in /etc/*-release ; do		# echo the head of the first valid *-release file
      if [ -e "$rfile" ] ; then
        cat $rfile | head -n 10
        break
      fi
    done
  fi

  validateUser	|| return 1			# die if sudo

  echo "ok"
}

function Run_TeamViewer()
{
  local tvprofile
  local inst
  local tvdir="$TV_PROFILE/drive_c/TeamViewer"

  isInstalledTV || inst="-n"

  Init

  echo "Launching TeamViewer ..."

  RequireNetwork		# modifies tvprofile !!!
  RequireWineServer

  echo "Launching TeamViewer GUI ..."

  if isQuickSupport; then
    chmod ugo-w "$tvdir"
    LD_LIBRARY_PATH="$(getRPathW)" \
    wine "c:\TeamViewer\TeamViewer.exe" $inst "${tvprofile[@]}" "$@" &> "$TV_LOG_DIR/winelog"
    chmod ug+w "$tvdir"
  else
    wine "c:\TeamViewer\TeamViewer.exe" $inst "${tvprofile[@]}" "$@" &> "$TV_LOG_DIR/winelog"
  fi
}

function Run_TeamViewer_Desktop()
{
  [ "$1" = "--desktop" ] && shift

  Init
  echo "Launching TeamViewer_Desktop..."

  RequireWineServer

  exec wine "c:\TeamViewer\TeamViewer_Desktop.exe" "$@" &> "$TV_LOG_DIR/winelogDesktop"
}

function Run_KillTeamViewer()
{
  local this="$0"			# ps: allow user names with more than 8 characters...
  local userlist=$(ps -e -o "user:25,command" | grep -v "^root" | grep TeamViewer | cut --delimiter=' ' -f 1)

  if [ $(id -u) = 0 ] ; then		# if root, launch the script for all other users (except root)
    for user in $userlist ; do
      echo "kill '$this' - $user"
      su -c "$this --kill" - $user
    done
    
    [ -d "$WINEPREFIX" ] && wineserver -k
  else
    wineserver -k	# kill for current user 
  fi
}

function Run_WineCfg()
{
  Init
  wine winecfg "$@"
}

function Run_RegEdit()
{
  Init
  wine regedit "$@"
}

function Run_Daemon()
{
  local opt="$1"

  case "$opt" in
    ( disable )				removeDaemon	|| rootSuggest	;;
    ( enable  )				installDaemon	|| rootSuggest	;;
    ( start | stop | restart )		cmdDaemon $opt	|| rootSuggest	;;
    ( status )				cmdDaemon $opt			;;
    ( * )				echo "unknown option '$opt'"	;;
  esac
}
