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

get_is_990_9a0() {
    ret=false
    # 匹配 kirin 990 5g, kirin990, kirin 9006c
    if egrep -qi 'kirin.?9[09]0' /proc/cpuinfo; then
        ret=true
    fi
    if egrep -qi 'PANGU.M900' /proc/cpuinfo; then
        ret=true
    fi

    echo $ret
}


  os_disk=$1
  is_nvme=$(echo "${os_disk}" | grep "nvme")
  if [[ -n ${is_nvme} ]]; then
        os_disk_one=${os_disk##*/}p
  else
        os_disk_one=${os_disk##*/}
  fi

  os_partition_dev=$(lsblk -o KNAME,LABEL ${os_disk} | grep ${os_disk_one})
  os_partition_dev_list=$(echo "${os_partition_dev}" | 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=$(echo "${os_partition_dev}" | grep $i | awk '{print $2}')
  #              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
                                is_990_9a0=$(get_is_990_9a0)
                                if [[ "${is_990_9a0}" == "true" ]]; then
                                        mount /dev/$i /mnt
                                        KYLIN_ID=$(grep -r KYLIN_RELEASE_ID= /mnt/etc/os-release)
                                        KYLIN_ID=${KYLIN_ID#*=}
                                        case ${KYLIN_ID} in
                                        '"2303"')
                                                n=`expr $n + 1`
                                                ;;
                                        *)
                                                ;;
                                        esac
                                        umount /mnt
                                else
                                        n=`expr $n + 1`
                                fi
                        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
	else
        	if [[ $n == 5 ]]; then
			is_990_9a0=$(get_is_990_9a0)
			if [[ "${is_990_9a0}" == "true" ]]; then
                		os_partition_ok=true
			fi
		fi
        fi


  else
        for i in ${os_partition_dev_list}
        do
  		os_partition_label=$(echo "${os_partition_dev}" | grep $i | awk '{print $2}')
  #              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
        exit 0
  else
        exit 1
  fi


