#!/bin/bash

TV_VER="15"
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"


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

  [ "$action" = 'remove' ] || [ "$action" = 'upgrade' ] || exit 0

  # save daemon status so that it can be restored after the upgrade
  [ "$action" = 'upgrade' ] && saveDaemonState

  removeDaemon
  [ "$action" = 'remove' ] && RemoveAssignment
}

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

  [ "$action" = 'remove' ] && removeDaemon   # during upgrade, daemon is removed by install script
  [ "$action" = 'remove' ] && RemoveAssignment
}

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

function RemoveAssignment()
{
  echo 'Clearing assignments ...'
  $TV_BIN_DIR/teamviewerd --unmanage
  echo 'done'
}


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

true
