#!/bin/bash

TV_PACKAGE_INSTALL='yes'
TV_SCRIPT_DIR='/opt/teamviewer/tv_bin/script'

source "$TV_SCRIPT_DIR/tvw_aux"
source "$TV_SCRIPT_DIR/tvw_config"
source "$TV_SCRIPT_DIR/tvw_daemon"
source "$TV_SCRIPT_DIR/tvw_profile"
source "$TV_SCRIPT_DIR/tvw_update"


function MainDEB()
{
  local action="$1"
  local version="$2"

  if [ "$action" = 'configure' ]; then
    Configure
    ConfigureUpdateDEB
  fi
}

function MainRPM()
{
  local installCount="$1"
  local action=$(RPMAction)

  if [ "$action" = 'upgrade' ]; then
    saveDaemonState # save daemon status so that it can be restored after the upgrade
    removeDaemon  # during upgrade: remove daemon, then reinstall
  fi

  Configure
  UpdateIconCache
}

function Configure()
{
  InitGlobalSettings			# before installDaemon
  recoverDaemonState
  installDaemon
}

function UpdateIconCache()
{
  local -r iconDir='/usr/share/icons/hicolor'

  cmdExists gtk-update-icon-cache   && gtk-update-icon-cache $iconDir
  cmdExists update-desktop-database && update-desktop-database
}

function RPMAction()
{
  case "$installCount" in
   ( 1 ) echo 'install' ;;
   ( * ) echo 'upgrade' ;; # assume 2
  esac
}

[ "$TV_PKGTYPE" = 'DEB' ] && MainDEB "$@"
[ "$TV_PKGTYPE" = 'RPM' ] && MainRPM "$@"

true
