#!/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

}

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|head -5|grep "The device has subscribed" -i`
    export LANG=$tmp_lang
	
    if [ x"$issub" != x"" ];then
        return 1
    else
        return 0
    fi
}

if [[ $1 = "--help" ]] || [[ $1 = "-h" ]]
then
    locale_echo "Usage: kylin-system-verify"
    locale_echo "This program is used to system verify!"
    exit 0
fi

is_subscribed
re=$?

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

user=$(whoami)

if [ $user != "root" ]; then
	locale_echo "Permission denied. Only users with administrator rights can perform this operation!"
	exit 1;
fi

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

	echo ""
	locale_echo "$msg"
	while true
	do
		sleep 100
	done
}

if [ -f /etc/LICENSE ]; then
	grep -q "^SERIAL:[0-9][0-9]*$" /etc/LICENSE || stay_message "LICENSE file has no valid service serial number"
	SERIAL_NO=$(grep "^SERIAL:[0-9][0-9]*$" /etc/LICENSE | head -n 1 | awk -F":" '{print $2}')
	SERIAL_LENGTH=${#SERIAL_NO}
	if [ $SERIAL_LENGTH -eq "6" ]; then
		/usr/bin/kylin-system-verify-legacy
	elif [ $SERIAL_LENGTH -gt "7" ]; then
		/usr/bin/kylin-system-verify-legacy
	elif [ $SERIAL_LENGTH -eq "7" ]; then
		/usr/bin/kylin-system-verify-new
	else
		stay_message "LICENSE file has no valid service serial number"
	fi
else
	stay_message "No LICENSE file"
fi
