#!/bin/sh
# -----------------
PACKAGE_DIR=`dirname "$0"`
PACKAGE_NAME=`basename "$0" .postinst`
PACKAGE_VERSION=`cat ${PACKAGE_DIR}/${PACKAGE_NAME}.control | awk -F': ' '/^Version/{print $2}'`
DIR=/usr/share/tm_cpps
CFG=/usr/share/tm_cpps_cfg
# -----------------

find /opt/packages/ -type f -name "tm_cpps*.ipk" ! -name "tm_cpps*$PACKAGE_VERSION*.ipk" -exec rm {} \; 2>/dev/null

cfgRestore()
{
  [ -f "$CFG"/"$1" ] && mv -f "$CFG"/"$1" "$DIR" > /dev/null 2>&1
}
[ -d "$CFG" ] && {
  cfgRestore pars_replace.json
}

chmod 755 /etc/init.d/tm_serverd
find /usr/share/tm_cpps -exec chmod 755 {} \;
/etc/init.d/tm_serverd enable
/etc/init.d/tm_serverd start >/dev/null 2>&1

/bin/sh /etc/uci-defaults/90_api_apply_patch >/dev/null 2>&1 && rm /etc/uci-defaults/90_api_apply_patch >/dev/null 2>&1

exit 0
