#!/bin/bash

# command line parameters that are not essential for normal TeamViewer usage

function PrintHelp()
{
  PrintVersion
  echo
  ABecho "teamviewer"		"start TeamViewer user interface (if not running)"
  echo
  ABecho "teamviewer --help"		"print this help screen"
  ABecho "teamviewer --version"		"print version information"
  ABecho "teamviewer --info"		"print version, status, id"
  ABecho "teamviewer --passwd [PASSWD]"	"set a password (useful when installing remote (ssh)"
  ABecho "teamviewer --ziplog"		"create a zip containing all teamviewer logs (useful when contacting support)"
  echo
  ABecho "teamviewer --daemon status"	"show current status of the TeamViewer daemon"
  ABecho "teamviewer --daemon start"	"start		TeamViewer daemon"
  ABecho "teamviewer --daemon stop"	"stop		TeamViewer daemon"
  ABecho "teamviewer --daemon restart"	"stop/start	TeamViewer daemon"
  ABecho "teamviewer --daemon disable"	"disable	TeamViewer daemon - don't start daemon on system startup"
  ABecho "teamviewer --daemon enable"	"enable		TeamViewer daemon - start daemon on system startup (default)"
}

function PrintVersion()
{
  ABecho "TeamViewer" "$TV_VERSION"
}

function PrintInfo()
{
  PrintVersion
  echo
  PrintDaemonStatus
  echo
  PrintTeamViewerID
}

function PrintDaemonStatus()
{
  local cmd="$(daemonCtl 'status')"
  local txt="$(eval "$cmd")"
  [ $? = 0 ] || txt='n/a (error)'
  
  ABecho "teamviewerd status" "$txt"
}

function PrintTeamViewerID()
{
  local config="$TV_BASE_DIR/config/global.conf"
  local tvid
  
  [ -e $config ] && tvid=$( grep 'ClientID' $config | cut --delimiter='=' -f2 )

  if [ -n "$tvid" ]; then
    ABecho "TeamViewer ID:" "$tvid"
  else
    echo "TeamViewer ID: not found"
    echo "Try restarting the TeamViewer daemon (e.g. teamviewer --daemon restart)"
  fi
}

function SetPasswd()
{
  local pwd="$1"
  [ -n "$pwd" ] || die 'no password specified'

  Run_Daemon 'stop' > /dev/null
  
  $TV_BIN_DIR/teamviewerd --passwd "$pwd"
  case $? in
    0  ) echo 'ok'	;;
    11 ) echo 'password too short - use at least 8 characters'	;;
    12 ) echo 'password too long  - use 12 or less characters'	;;
    13 ) echo 'password not accepted - illegal char detected'	;;
    *  ) echo 'unknown response'	;;
   esac
  
  Run_Daemon 'start' || die 'failed to restart the daemon'
  echo
}

function ExportLicense()
{
  local license="$1"
  local path='/tmp/tv_global.conf'

  [ -n "$license" ] || die 'no license specified'

  Run_Daemon 'stop' > /dev/null
  
  $TV_BIN_DIR/teamviewerd --export-license "$license" "$path"
  case $? in
    0  ) echo "ok - license exported to '$path'"		;;
    11 ) echo "destination '$path' not accessible"		;;
    *  ) echo 'unknown response'	;;
   esac
  
  Run_Daemon 'start' || die 'failed to restart the daemon'
  echo
}

function CreateZipLog()
{
  local cfg_dir="config"
  local log_dir="logfiles"
  local archive="/tmp/tvlog_$(hostname)_$(date +%F)"
  
  echo "Creating a zip archive from all files in $log_dir and $cfg_dir"
  
  cd "$TV_BASE_DIR"

  cmdExists zip
  if [ $? = 0 ]; then
    archive="$archive.zip"
    rm -f $archive
    zip -r9 $archive $cfg_dir $log_dir || die "Done. An error ($?) occurred when creating archive $archive"
  else
    archive="$archive.tar.gz"
    rm -f $archive
    tar -zchf $archive $cfg_dir $log_dir || die "Done. An error ($?) occurred when creating archive $archive"
  fi
  
  echo
  echo "Archive written to $archive"  
  echo 'done'
}

