#!/bin/bash
#copyright by hiboy
source /etc/storage/script/init.sh

link="Advanced_Extensions_app27.asp"
echo $1
if [ "$1"x = "stop"x ] ; then
    nvram set app_140=0 #gotify_enable
    exit
fi

if [ "$1" != "del" ] ; then
eval 'nvram set tablink'$1'='$link';'

nvram set gotify_L2="$(($3 + 10))"
nvram set gotify_L3="$2"
#     show_menu(8,<% nvram_get_x("", "gotify_L2"); %>,<% nvram_get_x("", "gotify_L3"); %>);
fi

get_www ()
{

# 加载程序配置页面
mkdir -p /opt/app/gotify
if [ -f "/tmp/www_asp/Advanced_Extensions_gotifyasp" ] ; then
if [ ! -f "/opt/app/gotify/Advanced_Extensions_gotify.asp" ] ; then
	mv -f /tmp/www_asp/Advanced_Extensions_gotifyasp /opt/app/gotify/Advanced_Extensions_gotify.asp
else
	rm -f /tmp/www_asp/Advanced_Extensions_gotifyasp
fi
fi
if [ ! -f "/opt/app/gotify/Advanced_Extensions_gotify.asp" ] || [ ! -s "/opt/app/gotify/Advanced_Extensions_gotify.asp" ] ; then
	wgetcurl.sh /opt/app/gotify/Advanced_Extensions_gotify.asp "$hiboyfile/Advanced_Extensions_gotifyasp" "$hiboyfile2/Advanced_Extensions_gotifyasp"
fi
umount /www/Advanced_Extensions_app27.asp
if [ -f "/opt/app/gotify/Advanced_Extensions_gotify.asp" ] ; then
	mount --bind /opt/app/gotify/Advanced_Extensions_gotify.asp /www/Advanced_Extensions_app27.asp
	app27_ver=$(grep 'app27_ver=' /opt/app/gotify/Advanced_Extensions_gotify.asp | awk -F '=' '{print $2;}')
	nvram set app27_ver=${app27_ver}
fi

}

get_app ()
{

# 更新程序启动脚本
[ "$1" = "del" ] && rm -rf /etc/storage/script/Sh86_go_tify.sh
if [ ! -f "/etc/storage/script/Sh86_go_tify.sh" ] || [ ! -s "/etc/storage/script/Sh86_go_tify.sh" ] ; then
	wgetcurl.sh /etc/storage/script/Sh86_go_tify.sh "$hiboyscript/script/Sh86_go_tify.sh" "$hiboyscript/script/Sh86_go_tify.sh"
fi
chmod 777 /etc/storage/script -R
if [ ! -f "/etc/storage/app_36.sh" ] || [ ! -s "/etc/storage/app_36.sh" ] ; then
/etc/storage/script/Sh86_go_tify.sh update_app
fi

[ "$1" = "del" ] && exit

}

if [ -f /tmp/webui_yes ] ; then
get_www &
get_app $1 &
[ "$1" = "del" ] && exit
fi