#!/bin/sh

DEBUG_MODE="no"
TOOL_PATH="/var/packages/DiagnosisTool/target/tool/"
TEMP_PROFILE_DIR="/var/packages/DiagnosisTool/etc/"

STATUS_NOT_INSTALLED=101
STATUS_NOT_LOADED=102
STATUS_LOADED=103
STATUS_REMOVED=104

Usage() #{{{
{
	cat << EOF
usage: `basename $0` <command>

The most commonly used commands are:
   install    Get and run this package, also load all tools
   remove     Remove it
   help       Show this usage
   list       List all tools
   check      Check if tools are ready to use

Examples:
   1. `basename $0` install
      Install debug tools and load them into environment
   2. `basename $0` remove
      Remove tools and restore all settings
   3. `basename $0` list
      List all loaded tools

EOF
} #}}}

dmsg()
{
	if [ "$DEBUG_MODE" = "yes" ]; then
		echo $1
	fi
}

WelcomeMessage()
{
	local action=$1
	local info=""

	dmsg "action: $action"
	if [ $action = $STATUS_LOADED ]; then
		echo "Tools are installed and ready to use."
		version=`synopkg version DiagnosisTool`
		echo "DiagnosisTool version: $version"
	elif [ $action = $STATUS_NOT_INSTALLED ]; then
		echo "Tools are not installed yet. You can run this command to install it:"
		info="install_cmd"
	elif [ $action = $STATUS_NOT_LOADED ]; then
		echo "Tools are installed but not loaded yet. You can run this command to load it:"
		info="install_cmd"
	elif [ $action = $STATUS_REMOVED ]; then
		echo "DiagnosisTool is removed completely, you can issue exit to close this session."
	fi

	if [ "$info" = "install_cmd" ]; then
		echo "   `basename $0` install"
		echo ""
	fi
}

#check package status
Check_Status()
{
	local loaded=0
	local installed=0
	
	synopkg status DiagnosisTool > /dev/null
	installed=$?

	# check if package are not installed
	if [ $installed != 0 ]; then
		return $STATUS_NOT_INSTALLED
	fi

	# check if tools are loaded
	echo $PATH | grep $TOOL_PATH > /dev/null
	loaded=$?
	if [ $loaded != 0 ]; then
		return $STATUS_NOT_LOADED
	fi

	return $STATUS_LOADED
}

# load tool path to default PATH environment
Load()
{
	Check_Status
	if [ $? = $STATUS_LOADED ]; then # already loaded, just return
		return 0
	fi

	if [ ! -d "$TEMP_PROFILE_DIR" ]; then
		echo "no temp profile folder found! exit. dir path: $TEMP_PROFILE_DIR"
		return 1
	fi

	cat /etc/profile | grep -v '^PATH=' | grep -v '^export PATH' > $TEMP_PROFILE_DIR.profile

	echo "" >> $TEMP_PROFILE_DIR.profile
	echo "PATH=$TOOL_PATH:$PATH" >> $TEMP_PROFILE_DIR.profile
	echo "export PATH" >> $TEMP_PROFILE_DIR.profile

	export ENV=$TEMP_PROFILE_DIR.profile

	# use /bin/sh, unless we have to use ash ($SHELL)
	/bin/sh
}

Action_Load()
{
	local status=0

	Check_Status
	status=$?

	if [ $status = $STATUS_LOADED ]; then
		WelcomeMessage $status
		return 1
	fi

	Load

	Check_Status
	status=$?
	WelcomeMessage $status
}

# get and install diagtool
Install()
{
	local tmp_link_json="/tmp/link.json"
	local tmp_package_path="/tmp/DiagnosisTool.spk"
	local INFO_LINK=""
	local PKG_LINK=""

	local ret=0
	local MAJOR_VER=`synogetkeyvalue /etc.defaults/VERSION majorversion`
	local MINOR_VER=`synogetkeyvalue /etc.defaults/VERSION minorversion`
	local BUILD_VER=`synogetkeyvalue /etc.defaults/VERSION buildnumber`
	local TIME_ZONE=`synogetkeyvalue /etc.defaults/synoinfo.conf timezone`
	local UNIQUE=`synogetkeyvalue /etc.defaults/synoinfo.conf unique`
	local LANG="enu"

	Check_Status
	status=$?

	# if package was installed, return
	if [ $status = $STATUS_LOADED ] || [ $status = $STATUS_NOT_LOADED ]; then
		return $status
	fi

	# clear old files
	rm -f $tmp_link_json $tmp_package_path

	# combine link.json request string
	INFO_LINK="https://payment.synology.com/api/getPackage.php?language=$LANG&timezone=$TIME_ZONE&unique=$UNIQUE&major=$MAJOR_VER&minor=$MINOR_VER&build=$BUILD_VER&package_update_channel=stable&package=DiagnosisTool"

	# try to get the package info from package download site, which contain the actual package download link info
	wget -q -O "$tmp_link_json" "$INFO_LINK"

	if [ ! -s "$tmp_link_json" ]; then
		echo "can't get correct package link string!"
		return 1
	fi

	# get the actual package download link
	PKG_LINK=`jq -e -r '.["package"]["link"]' $tmp_link_json`
	ret=$?

	# if it fails to get correct link info, stop it
	if [ $ret -ne 0 ]; then
		echo "Failed to get DiagnosisTool ... can't parse actual package download link from info file, error code: $ret"
		return 255
	fi

	wget -q -O "$tmp_package_path" "$PKG_LINK"

	if [ ! -s "$tmp_package_path" ]; then
		echo "failed to get diagtool package from synology server!"
		return 1
	fi

	# check if file download correctly
	synopkg install "$tmp_package_path" > /dev/null
	ret=$?

	[ $ret -ne 0 ] && echo "Failed to install DiagnosisTool ... synopkg error code: $ret"

	synopkg start DiagnosisTool > /dev/null
	ret=$?

	if [ $ret -ne 0 ]; then
		echo "Failed to start DiagnosisTool ... synopkg error code: $ret"
		return 255
	fi

	# remove temporary files
	if [ "$DEBUG_MODE" != "yes" ]; then
		rm -f $tmp_list_path $tmp_package_path
	fi

	return $STATUS_NOT_LOADED
}

Action_Install()
{
	local status=0
	Check_Status
	status=$?
	if [ $status = $STATUS_LOADED ]; then
		WelcomeMessage $status
		return 0
	fi

	Install
	status=$?

	# if it's installed correctly, load tools
	if [ $status != $STATUS_NOT_LOADED ]; then
		return 1
	fi

	# load tools and run another shell with tools loaded to default path
	Load
}

# uninstall diagtool
Action_Remove()
{
	local status=0
	Check_Status
	status=$?

	if [ $status = $STATUS_NOT_INSTALLED ]; then
		WelcomeMessage $status
		return 1
	fi

	synopkg uninstall DiagnosisTool > /dev/null

	if [ $? -ne 0 ]; then
		echo "failed to remove DiagnosisTool package ..."
		return 1
	fi

	WelcomeMessage $STATUS_REMOVED
}

Action_Check()
{
	Check_Status
	#dmsg "status: $?"
	WelcomeMessage $?
}

Action_List()
{
	local status=0
	Check_Status
	status=$?

	if [ $status = $STATUS_LOADED ]; then
		echo "All tools:"
		ls $TOOL_PATH
	else
		WelcomeMessage $status
	fi
}

if [ $# -gt 0 ]; then
	action=$1
fi

if [ "$action" = "install" ]; then
	Action_Install
elif [ "$action" = "remove" ]; then
	Action_Remove
elif [ "$action" = "load" ]; then
	Action_Load
elif [ "$action" = "help" ]; then
	Usage
elif [ "$action" = "check" ]; then
	Action_Check
elif [ "$action" = "list" ]; then
	Action_List
else
	Action_Check
	Usage
fi
