#!/bin/bash

TVD_SYSV_DIR='/etc/init.d'
TVD_SYSV_SRC='teamviewerd.sysv'
TVD_SYSV_FILE='teamviewerd'
TVD_SYSV_PATH="$TVD_SYSV_DIR/$TVD_SYSV_FILE"

TVD_SYSD_DIR='/etc/systemd/system'	# overrides /lib/systemd/system  /usr/lib/systemd/system
TVD_SYSD_FILE='teamviewerd.service'
#TVD_SYSD_PATH="$TVD_SYSD_DIR/$TVD_SYSD_FILE"

TVD_UPST_DIR='/etc/init'
TVD_UPST_ALT='teamviewerdMint.conf'	# only for Mint / mdm or similarly defunct display managers
TVD_UPST_FILE='teamviewerd.conf'
TVD_UPST_PATH="$TVD_UPST_DIR/$TVD_UPST_FILE"


function installLog()
{
  if [ "$TV_PACKAGE_INSTALL" = "yes" ]; then
    cat >> "$TV_INSTALL_LOG"
  else
    tee -a "$TV_INSTALL_LOG"
  fi
}

function installLogHeader()
{
  local action="$1"

  [ -e "$TV_INSTALL_LOG" ] && echo
  date
  echo "Action: $action"
}

function detectUpstart
{
  cmdExists initctl	|| return 1	# no root required
  isSuperUser		|| return 0	# if root, check more

  local upstart=$( getInitCmd | grep init > /dev/null && init --version 2>/dev/null | grep upstart )
  [ -n "$upstart" ]
}

function detectSystemD
{
  cmdExists systemctl	|| return 1	# no root required
  isSuperUser		|| return 0	# if root, check more

  local systemd=$( readlink -e $(getInitCmd) 2>/dev/null | grep systemd )
  [ -n "$systemd" ]
}

function installDaemon()
{
  exec 2>&1
  
  local dtype='V'
  detectSystemD && dtype='D'
  detectUpstart && dtype='U'

  [ $dtype = 'V' ] && installDaemonSysV		| installLog
  [ $dtype = 'D' ] && installDaemonSystemD	| installLog
  [ $dtype = 'U' ] && installDaemonUpstart	| installLog
  
  cmdDaemon 'start'				| installLog
}

function installDaemonSysV()
{
  installLogHeader "Installing daemon ($TV_VERSION) for 'SystemV' ..."
  installDaemonCfg "$TVD_SYSV_DIR" "$TVD_SYSV_SRC" "$TVD_SYSV_FILE"
  
  local chkcfg='chkconfig'
  cmdExists $chkcfg || chkcfg='/sbin/chkconfig'

  cmdExists $chkcfg     && $chkcfg --add teamviewerd           && return
  cmdExists update-rc.d && update-rc.d $TVD_SYSV_FILE defaults && return

  echo "Failed to update daemon configuration"
}

function installDaemonSystemD()
{
  installLogHeader "Installing daemon ($TV_VERSION) for 'systemd' ..."
  installDaemonCfg "$TVD_SYSD_DIR" "$TVD_SYSD_FILE"
  systemctl daemon-reload		# just in case...
  systemctl enable "$TVD_SYSD_FILE"	# on Fedora 15, systemctl does not accept a full path (to /opt/...)
}

function installDaemonUpstart()
{
  installLogHeader "Installing daemon ($TV_VERSION) for 'upstart' ..."
  
  local src="$TVD_UPST_FILE"
  displayManagerIsBroken && src="$TVD_UPST_ALT"

  installDaemonCfg "$TVD_UPST_DIR" "$src" "$TVD_UPST_FILE"
  initctl reload-configuration
}

function displayManagerIsBroken()
{
  ps x | grep '/mdm$' >/dev/null
}

function installDaemonCfg
{
  local dir="$1"
  local file="$2"
  local link="$3"
  [ -n "$link" ] || link="$file"

  [ -d "$dir" ] || return

  echo "installing $dir/$link ("$TV_SCRIPT_DIR/$file")"
  ln -sf "$TV_SCRIPT_DIR/$file" "$dir/$link"
}

function removeDaemon()
{
  exec 2>&1
  
  removeDaemonAny			| installLog
  
  true
}

function removeDaemonAny()
{
  installLogHeader "Removing ..."
  cmdDaemon 'stop'
  stopTeamViewer	# just to make sure ...
 
 # TODO verify / update
 
  if [ -e "$TVD_SYSV_PATH" ]; then
    rm -f "$TVD_SYSV_PATH"
    cmdExists chkconfig   && chkconfig --del teamviewerd
  fi

  if [ -e "$TVD_UPST_PATH" ]; then
    rm -f "$TVD_UPST_PATH"
    detectUpstart && initctl reload-configuration
  fi

  if detectSystemD; then
    systemctl disable "$TVD_SYSD_FILE"
    systemctl daemon-reload
  fi
  
  true
}

function stopTeamViewer()
{
  teamviewer --kill # Kill all running TV instances, if any
}

function cmdDaemon()
{  
  local opt="$1"  
  local cmd="$(daemonCtl $opt)"
  
  echo "$cmd"
  eval "$cmd" && return

  echo "fail"
  false
}

function daemonCtl()
{  
  local opt="$1"  
  local cmd="$TVD_SYSV_PATH $opt"				# SystemV
  detectUpstart && cmd="initctl $opt teamviewerd"		# Upstart
  detectSystemD && cmd="systemctl $opt $TVD_SYSD_FILE"		# SystemD
  echo "$cmd"
}
