#!/bin/bash

is_efi() {
    if type archdetect >/dev/null 2>&1; then
        archdetect=$(archdetect)
    else
        archdetect=unknown/generic
    fi
    arch=${archdetect%/*}
    sub=${archdetect#*/}
    ret=true
    case "$arch" in
        alpha|sw64)
            ret=false;;
        amd64|i386)
            case "$sub" in
                mac|efi)
                ret=true;;
                *)
                ret=false;;
            esac;;
        arm|armhf|armel)
            ret=false;;
        arm64)
            ret=true;;
        mips64el)
            case "$sub" in
                efi)
                ret=true;;
                *)
                ret=false;;
            esac;;
        mips|mipsel)
            ret=false;;
        *)
            ret=true;;
    esac

    # 华为 990 设置为 efi
    if egrep -qi 'kirin.?9[09]0' /proc/cpuinfo; then
        ret=true
    fi

    echo $ret
}

os_list=$(os-prober)

#判断磁盘上是否存在os

if [[ -z ${os_list} ]]; then
    echo "os unexit in disk"
else 
    #判断是否存在非kylin系统
    other_os=$(echo "${os_list}" | grep -v "Kylin") || true
    if [[ -n ${other_os} ]]; then
    	echo "other os exit in disk"
    else    
	#判断是否存在多kylin系统
	kos_num=$(echo "${os_list}" | grep "Kylin" | wc -l)
#	echo "${kos_num}"
	if [[ ${kos_num} == 1 ]]; then	
		#判断kylin系统是否为SPX系统
    		kos_spx=$(echo "${os_list}" | grep "SP")
    		if [[ -n ${kos_spx} ]]; then
    			os_list=`LANG=en_US; echo "${os_list}" | cut -d ':' -f 1`
			is_nvme=$(echo "${os_list}" | grep "nvme")
			if [[ -n ${is_nvme} ]]; then
				os_disk=${os_list%??}
				os_dev=${os_disk##*/}
				os_disk_one=${os_dev}p
			else
				os_disk=${os_list%?}
				os_dev=${os_disk##*/}
				os_disk_one=${os_dev}

			fi
			
			os_partition_dev_list=$(lsblk -o KNAME,LABEL ${os_disk} | grep ${os_disk_one}| awk '{print $1}')	
			is_efi=$(is_efi)
			n=0
			if [[ ${is_efi} == true ]]; then
				for i in ${os_partition_dev_list}
				do
					os_partition_label=$(lsblk -o KNAME,LABEL ${os_disk} | grep $i | awk '{print $2}')
					case "$i" in
			        	${os_disk_one}1)
						if [[ ${os_partition_label} == "ESP" ]]; then
							n=`expr $n + 1`
						fi;;
			        	${os_disk_one}2)
						if [[ ${os_partition_label} == "SYSBOOT" ]]; then
							n=`expr $n + 1`
						fi;;
			        	${os_disk_one}3)
						if [[ ${os_partition_label} == "SYSROOT" ]]; then
							n=`expr $n + 1`
						fi;;
			        	${os_disk_one}4)
						if [[ ${os_partition_label} == "KYLIN-BACKUP" ]]; then
							n=`expr $n + 1`
						fi;;
			        	${os_disk_one}5)
						if [[ ${os_partition_label} == "DATA" ]]; then
							n=`expr $n + 1`
						fi;;
			        	${os_disk_one}6)
						if [[ ${os_partition_label} == "SWAP" ]]; then
							n=`expr $n + 1`
						else
                                                        os_partition_label=$(fdisk -l ${os_disk} | grep $i | grep 'swap') || true
                                                        if [[ -n ${os_partition_label} ]]; then
                                                                n=`expr $n + 1`
                                                        fi
						fi;;
        				*)
            					;;
    					esac
				done
				if [[ $n == 6 ]]; then
					os_partition_ok=true
				fi
			else
				for i in ${os_partition_dev_list}
                                do
                                        os_partition_label=$(lsblk -o KNAME,LABEL ${os_disk} | grep $i | awk '{print $2}')
                                        case "$i" in
                                        ${os_disk_one}1)
                                                if [[ ${os_partition_label} == "SYSBOOT" ]]; then
                                                        n=`expr $n + 1`
                                                fi;;
                                        ${os_disk_one}5)
                                                if [[ ${os_partition_label} == "SYSROOT" ]]; then
                                                        n=`expr $n + 1`
                                                fi;;
                                        ${os_disk_one}6)
                                                if [[ ${os_partition_label} == "KYLIN-BACKUP" ]]; then
                                                        n=`expr $n + 1`
                                                fi;;
                                        ${os_disk_one}7)
                                                if [[ ${os_partition_label} == "DATA" ]]; then
                                                        n=`expr $n + 1`
                                                fi;;
                                        ${os_disk_one}8)
                                                if [[ ${os_partition_label} == "SWAP" ]]; then
                                                        n=`expr $n + 1`
						else
                                                        os_partition_label=$(fdisk -l ${os_disk} | grep $i | grep 'swap') || true
                                                        if [[ -n ${os_partition_label} ]]; then
                                                                n=`expr $n + 1`
                                                        fi
                                                fi;;
                                        *)
                                                ;;
                                        esac
                                done
                                if [[ $n == 5 ]]; then
                                        os_partition_ok=true
                                fi

			fi
			if [[ ${os_partition_ok} == true  ]]; then
				sed -i "/retain-data/d" /usr/share/kylin-os-installer/ky-installer.cfg || true
				echo "retain-data=${os_disk}" >>/usr/share/kylin-os-installer/ky-installer.cfg
			fi

		fi
	fi
    fi
fi
