#!/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-legacy"
    locale_echo "This program is used to system verify in legacy!"
    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

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

SHOW_SERIAL_NO="y"
export SHOW_SERIAL_NO

CODE_NO_HYPHEN="y"
export CODE_NO_HYPHEN


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

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

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 = 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_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_activate_retry $?
	fi
}

show_qrcode() {
	locale_echo "Enter 'Kylin' to display the registration information QR code, or press enter directly to skip"
	sleep 1
	read  SHOW_QR_CODE

	if [ "$SHOW_QR_CODE" = "kylin" ]; then
		echo ""
		clear
		REGISTER_CODE=$(echo $1 | awk '{print $1}')
		SERIAL_CODE=$(echo $1 | awk '{print $2}')
		if [ "$QRCODE_USE_CMD" = "1" ]; then
			QR_CODE_STR=$(/usr/bin/kylin_qrcode 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}"
		fi

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

		echo ""
		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
	fi
}

ky_gen_register() {


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

	clear
	kylin_gen_register > /dev/null || stay_message "Registration code generation failed"

	REGISTER_NO=$(kylin_gen_register)
	show_qrcode "$REGISTER_NO"

	clear
	CODE_NO_HYPHEN="n"

	REGISTER_NO=$(kylin_gen_register)
	echo $REGISTER_NO

	echo ""
	if [ $SHOW_SERIAL_NO = y ]; then
		locale_echo "The above is the registration code and serial number. Please contact Kylin to obtain the activation code"
	else
		locale_echo "The above is the registration code. Please contact Kylin to obtain the activation code"
	fi

	ky_activate
}

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

	kylin_verify || \
		stay_message "System verification failed"

	echo ""
	echo ""
	export KYLIN_REALLY_ACTIVATED=y

	kylin_activation_check
	ky_gen_register

	unset KYLIN_REALLY_ACTIVATED	
	echo ""
}


ky_verify

unset ROOTFS_DEVICE
unset SHOW_SERIAL_NO
unset CODE_NO_HYPHEN


