#!/bin/sh

locale_echo()
{
    lang_l=`echo $LANG|awk -F"." '{print $1}'`
    if [ x"$lang_l" == x"" ];then
        echo $1
        return
    fi
    lang_f="/usr/share/libkylin-activation/locale/"$lang_l
    if [ ! -f $lang_f ];then
        echo $1
        return
    fi

    est=`grep "^$1==" $lang_f|head -1|awk -F"==" '{print $2}'`
    if [ x"$est" != x"" ];then
        echo "$est"
	else
        echo $1
    fi

}

if [ $# -gt 0 ];then
    locale_echo "Usage: kylin_activate_secret_key"
    locale_echo "This program uses secret key to activate system."
    exit 0
fi


user=$(whoami)

if [ "$user" != "root" ]; then
	locale_echo "Please use sudo or switch to the root user to execute the program"
	exit 1;
fi


# 0,未激活；1，kms激活；2，扫码激活；3，ukey激活；4，场地授权；5，密钥激活；6，其他方式
get_activate_style()
{
	isactivated=`LANG=C kylin_activation_check|grep "System is activated."`
	##未激活状态
	if [ x"$isactivated" == x"" ];then
		return 0
	fi	

	##场地授权
	isplace=`grep "METHOD:place" /etc/LICENSE`
	if [ x"$isplace" != x"" ];then
        return 4
    fi

	conf=/usr/share/libkylin-activation/activation_conf.ini
	if ! [ -f $conf ];then
		return 6		
	fi
	
	flag=`grep activate_style $conf|awk -F"=" '{print $NF}'|awk '{print $1}'|head -1`

	if [ x"$flag" == x"" ];then
		return 6
	else
		return $flag
	fi
	

}

checkonline() 
{
	kylin_qrcode > /dev/null || exit 1
    qrcode_url=$(kylin_qrcode)
	serial_no=`echo $qrcode_url|awk -F"=" '{print $2}'|awk -F"&" '{print $1}'`

	origin_url="https://wx.kylinos.cn/api/get_activation.json?f=$serial_no"
    #echo "curl -s -o /dev/null -w \"%{http_code}\" \"$origin_url\" -k --connect-timeout 5"
    conn=`curl -s -o /dev/null -w "%{http_code}" "$origin_url" -k --connect-timeout 5` ##5秒超时

    if [ "$conn" != "200" ];then ##离线
        return 0
    else
        return 1
    fi


}

stay_message() {
	msg="${1}"

	locale_echo "$msg"
	exit 0
}

ky_activate_retry() {
	if [ $1 = 1 ] || [ $1 = 2 ] || [ $1 = 3 ]; then
		stay_message "No valid LICENSE file"
	elif [ $1 = 4 ]; then
		stay_message "No valid Kylin information file"
	elif [ $1 = 14 ]; then
		stay_message "No valid public key"
	elif [ $1 = 15 ]; then
		stay_message "There is an issue with the system time"
	elif [ $1 = 16 ]; then
		stay_message "GPG verification encountered issues"
	elif [ $1 = 17 ]; then
		stay_message "Unable to obtain valid information"
	elif [ $1 = 72 ] || [ $1 = 73 ]; then
		stay_message "No valid service serial number"
	elif [ $1 = 8 ] || [ $1 = 9 ] || [ $1 = 10 ] || [ $1 = 11 ] || [ $1 = 12 ] || [ $1 = 13 ]; then
		locale_echo "Please try again:"
		read  secret_key
		if [ "x$secret_key" = "x" ]; then
			ky_activate_retry 9
		else
			echo ""
			kylin_secret_key "$secret_key" || ky_activate_retry $?
		fi
	fi
}

ky_activate() {
	locale_echo "Please enter the secret key:"
	read secret_key
	
	if [ "x$secret_key" = "x" ]; then
		ky_activate_retry 9
	else
		echo ""
		kylin_secret_key "$secret_key" || ky_activate_retry $?
	fi
}

is_subscribed()
{
    tmp_lang=$LANG
    exec_fl=/usr/bin/kylin-subscription
    cmd="$exec_fl status"

    if ! [ -e $exec_fl ];then
        return 0
    fi

    export LANG=C
    issub=`$cmd|grep "The device has subscribed" -i`
    export LANG=$tmp_lang
	
    if [ x"$issub" != x"" ];then
        return 1
    else
        return 0
    fi
}

main()
{
	is_subscribed
	re=$?

	if [ x"$re" == x"1" ];then ##已订阅
		locale_echo "The system has subscribed!"
		exit
	fi

	##远程获取激活状态的方式来判断是否离线
	checkonline
	ret=$?
	if [ "$ret" != "0" ];then ##在线
		ky_activate
	else
		locale_echo "Unable to connect to the network, please follow the steps below to complete activation:"
		sleep 5
		/usr/bin/kylin-system-verify
	fi


}

main
