#!/bin/sh

PLUGIN_ID="2882303761518031252"
MODEL=`uname -m`
INSTALL_DIR="/userdisk/appdata/${PLUGIN_ID}"
MANIFEST_FILE="/userdisk/appdata/app_infos/${PLUGIN_ID}.manifest"

check_model() {
	if [ "$MODEL" != "aarch64" ];then
		echo "目前仅支持aarch64！"
		return 1
	fi
	
	return 0
}

install() {
	echo "开始安装..."
	
	if [ -f "${INSTALL_DIR}/xnetease-uu" ];then
		echo "检测到已经安装，退出"
		return 1
	fi
	
	wget -q http://down.asdi998.com/AX6/uu.tar.gz -O /tmp/uu.tar.gz
	[ ! -d "${INSTALL_DIR}" ] && mkdir ${INSTALL_DIR}
	tar xzvf /tmp/uu.tar.gz -C ${INSTALL_DIR} 1>/dev/null 2>&1
	chown -R root:root ${INSTALL_DIR}
	chmod u+x ${INSTALL_DIR}/S99uuplugin
	ln -sf ${INSTALL_DIR}/S99uuplugin /etc/rc.d/S99uuplugin
	
	cp $0 ${INSTALL_DIR}/uu.sh
	
# 	cp ${INSTALL_DIR}/manifest ${MANIFEST_FILE}
# 	chmod 777 ${MANIFEST_FILE}
	
	/bin/sh ${INSTALL_DIR}/xnetease-uu plugon
	
	echo "安装完成！"
	return 0
}


uninstall() {
	echo "开始卸载..."
	
	if [ ! -d "${INSTALL_DIR}" ];then
		echo "检测到安装目录不存在，退出"
		return 1
	fi

	/bin/sh ${INSTALL_DIR}/xnetease-uu plugoff 1>/dev/null 2>&1
    
	local link_script="/etc/rc.d/S99uuplugin"
    [ -h "${link_script}" ] && rm ${link_script}
	
	if [ -d "${INSTALL_DIR}" ];then
        rm -r "${INSTALL_DIR}"/* 1>/dev/null 2>&1
		rm -r "${INSTALL_DIR}" 1>/dev/null 2>&1
    fi
	
# 	[ -f "${MANIFEST_FILE}" ] && rm ${MANIFEST_FILE}
	
	echo "卸载完成！"
	return 0
}

check_model
[ "$?" != "0" ] && return 1
[ "$1" == "install" ] && install
[ "$1" == "uninstall" ] && uninstall
