#!/bin/sh
OLD=/opt/1tm/tm_cpps
OPT=/opt/storage/files/tm_cpps
NEW=/usr/share/tm_cpps

##delete old packages
pkgDIR=/opt/packages
fnPREV=
DELETE()
{
for FN in $1; do
  [ -n "$fnPREV" ] && rm -f "$fnPREV"
  fnPREV="$FN"
done
}
STOP()
{
  [ -f /etc/init.d/hwclockd ] && /etc/init.d/hwclockd stop
  [ -f /etc/init.d/tm_serverd ] && /etc/init.d/tm_serverd stop
}
uninstall()
{
  [ -f "$1"/tm_server ] && {
    "$1"/tm_server stop > /dev/null 2>&1
#    sleep 5
    rm -f -r "$1"/tm_server
  }
  [ -d "$1" ] && rm -f -r "$1"
}
cpOPT()
{
  [ -f "$OLD"/"$1" ] && cp -f "$OLD"/"$1" "$OPT"
}
cpOPTdir()
{
  [ -d "$2"/"$1" ] && [ ! -L "$2"/"$1" ] && cp -f -R "$2"/"$1" "$OPT"/"$1"
}
STOP
DELETE "$pkgDIR""/tm_cpps_*.ipk"
#uninstall old
[ -d "$OLD" ] && {
  cpOPT users.cfg
  cpOPT users.xml
  cpOPT all.cfg
  cpOPT sys.cfg
  cpOPT conf.xml
  cpOPT conf_back.xml
  cpOPT pats.xml
  cpOPT tm_server.k2
  cpOPTdir scheme "$OLD"
  cpOPTdir confs "$OLD"
  uninstall "$OLD"
}

#fix scheme dir
cpOPTdir scheme "$NEW"

#uninstall new
uninstall "$NEW"

exit 0
