#!/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 [[ $1 = "--help" ]] || [[ $1 = "-h" ]]
then
    locale_echo "Usage: kylin-system-verify-new"
    locale_echo "This program is used to system verify in new!"
    exit 0
fi

PREREQS=""

prereqs() { echo "$PREREQS"; }

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac


user=$(whoami)

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

SERIAL_NO=$(grep "^SERIAL:[0-9][0-9]*$" /etc/LICENSE | head -n 1 | awk -F":" '{print $2}')
SERIAL_LENGTH=${#SERIAL_NO}
ROOTFS_MOUNT=$(awk '!/^rootfs / {if ($2 == "/") {print $1 " " $2 " " $3 " " $4; exit}}' /proc/mounts)
ROOTFS_OPTIONS=$(echo $ROOTFS_MOUNT | awk '{ print $4 }')
ROOTFS_FSTYPE=$(echo $ROOTFS_MOUNT | awk '{ print $3 }')
ROOTFS_DEVICE=$(echo $ROOTFS_MOUNT | awk '{ print $1 }')
echo $ROOTFS_OPTIONS | grep -qe '^ro,' && readonly="y" || readonly="n"

export ROOTFS_DEVICE

CODE_NO_HYPHEN="y"
export CODE_NO_HYPHEN

KY_SERIAL_NUMBER=

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

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

ky_input_serial_retry() {

	if [ $1 = 0 ]; then
		echo ""
		locale_echo "Please enter the service serial number:"
	elif [ $1 = 1 ]; then
		locale_echo "Invalid service serial number, please re-enter:"
	elif [ $1 = 2 ]; then
		locale_echo "The length of the service serial number is less than 8, please re-enter:"
	else
		locale_echo "Please enter the service serial number:"
	fi

	read KY_SERIAL_NUMBER
	echo ""

	if [ "x$KY_SERIAL_NUMBER" = "x" ]; then
		ky_input_serial_retry 1
	fi

	echo "$KY_SERIAL_NUMBER" | grep -q "^[0-9][0-9]*$" || ky_input_serial_retry 1

	KY_SERIAL_LENGTH=${#KY_SERIAL_NUMBER}
	if [ $KY_SERIAL_LENGTH -lt 8 ]; then
		ky_input_serial_retry 2
	fi
}

ky_activate_retry() {
	echo ""
	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  KY_ACT_CODE
		if [ "x$KY_ACT_CODE" = "x" ]; then
			ky_activate_retry 9
		else
			kylin_activate "$KY_ACT_CODE" "$KY_SERIAL_NUMBER" || ky_activate_retry $?
		fi
	fi
}

ky_activate() {
	echo ""
	locale_echo "Please enter the activation code:"
	read KY_ACT_CODE
	echo ""

	if [ "x$KY_ACT_CODE" = "x" ]; then
		ky_activate_retry 9
	else
		kylin_activate "$KY_ACT_CODE" "$KY_SERIAL_NUMBER" || ky_activate_retry $?
	fi
}

show_qrcode() {
	clear
	REGISTER_CODE=$(echo $1)
	SERIAL_CODE=$(echo "$KY_SERIAL_NUMBER")

	if [ "$QRCODE_USE_CMD" = "1" ]; then
		QR_CODE_STR=$(/usr/bin/kylin_qrcode "$SERIAL_CODE" 2>/dev/null)
	fi

	if [ "$QR_CODE_STR" = "" ]; then
		QR_CODE_STR="http://wx.kylinos.cn/qywx/distro/activate?f=${SERIAL_CODE}&z=${REGISTER_CODE}&p=${SERIAL_NO}"
	fi

	if [ "$QRCODE_TYPE" = "" ]; then
		QRCODE_TYPE="ANSI"
	fi

	clear
	qrencode -t "$QRCODE_TYPE" -m 2 "${QR_CODE_STR}" 2> /dev/null && \
		locale_echo "Registration information QR code" && \
		sleep 2 && \
		locale_echo "Please press Enter to continue..." && \
		read TMP || \
		sleep 0.1
}

ky_gen_register_retry() {

	ky_input_serial_retry 0

	clear
	kylin_gen_register "$KY_SERIAL_NUMBER" > /dev/null || stay_message "Registration code generation failed"
	REGISTER_NO=$(kylin_gen_register "$KY_SERIAL_NUMBER")
	show_qrcode "$REGISTER_NO"

	clear
	CODE_NO_HYPHEN="n"
	REGISTER_NO=$(kylin_gen_register "$KY_SERIAL_NUMBER")
	
	echo "$REGISTER_NO $KY_SERIAL_NUMBER"

	locale_echo "The above is the registration code and serial number. Please contact Kylin to obtain the activation code"

	ky_activate
}

ky_gen_register() {

	echo ""
	sleep 2
	locale_echo "Please press Enter to continue..."
	read TMP

	clear

	ky_gen_register_retry
}

ky_verify() {
	clear
	if [ ${readonly} = y ]; then
		locale_echo "The file system is read-only and cannot be validated."
		exit 1
	fi

	kylin_activation_check
	ky_gen_register

}

if [ $SERIAL_LENGTH -eq "7" ]; then
	ky_verify
else
	stay_message "LICENSE file has no valid service serial number"
fi

unset ROOTFS_DEVICE
unset CODE_NO_HYPHEN
