#!/bin/sh

PKGSTATUSFILE=/etc/zyxel/pkg_conf/status
PKGNAME=myZyXELcloud-Agent
PKGPATH=$(grep ${PKGNAME} ${PKGSTATUSFILE} | grep "Installed-Rule" | awk -F":" '{print $2}' | sed 's/\/$//g' | sed 's/ //g')
THUMBNAIL_STATUSFILE=${PKGPATH}/share/thumb_status

THUMBNAIL_STATUSFILE_IN_STORAGE=/i-data/.system/zy-pkgs/myZyXELcloud-Agent_conf/thumb_status
THUMBNAIL_STATUSFILE_FOLDER=/i-data/sysvol/.PKG/myZyXELcloud-Agent/share/
CLOUDAGENT_CONF_PATH=/i-data/.system/zy-pkgs/myZyXELcloud-Agent_conf

# make a folder to save agent conf in storage
if [ ! -d "$CLOUDAGENT_CONF_PATH" ]; then 
	mkdir "$CLOUDAGENT_CONF_PATH" 
fi

# if there is a thumbnail status file in storage, set the status from this file 
if [ -f "$THUMBNAIL_STATUSFILE_IN_STORAGE" ]; then
	cp "$THUMBNAIL_STATUSFILE_IN_STORAGE" "$THUMBNAIL_STATUSFILE_FOLDER"
else
	# thumbnail function is enabled by default
	echo "enable" > ${THUMBNAIL_STATUSFILE}
fi
touch /etc/zyxel/unpair_flag

# change permission of .thumb and thumbnail files generated by old package to 777
# because permission of .thumb and thumbnail files generated by new package will be 777.
$PKGPATH/scripts/chmod_777_for_all_thumb_dir.sh

$PKGPATH/etc/init.d/myZyXELcloud-Agent startup
