#!/bin/bash

set -e

data_unformat=$(get_value data-unformat)
if [[ "${data_unformat}" == "true" ]]; then
  echo "save data installer"
else
  return 0
fi

automatic=$(get_value automatic-installation)
if [[ "$automatic" == "0" ]]; then
  return 0
fi

disk_custom=$(get_value disk-custom)
#兼容老版本配置文件
root_size=$(get_value disk-root)
if [[ "${disk_custom}" == "true" ]] && [[ -z "${root_size}" ]]; then
  return 0
fi


disk=$(get_value devpath)
data_device=$(get_value data-device)



partprobe "${disk}" && sync

sleep 1

has_backup=0

do_mkfs_efi(){
  #if egrep -qi 'kirin.?990' /proc/cpuinfo; then
  part_efi=$1
  if [[ "${is_990_9a0}" == "true" ]]; then
    sudo mkfs.vfat -s1 -n ESP "${part_efi}"
  else
    sudo mkfs.vfat -n ESP "${part_efi}" || sudo mkfs.vfat -F16 -n ESP "${part_efi}"
  fi
}

do_prepare_sw() {
  umount -l /media/*/* || true
  dmsetup remove_all -f
  #gsettings set org.ukui.flash-disk.autoload ifautoload false
}

do_mkfs_boot() {
  boot_part=$1
  if [[ "$(archdetect)" == "sw64/generic" ]]; then
  #  do_prepare_sw
    mkfs.ext3 -Fq -L SYSBOOT "${boot_part}"
  else
    mkfs.ext4 -Fq -L SYSBOOT "${boot_part}"
  fi
}



# 保留data
save_history_data() {
	mount "$1" /mnt 
	mkdir -p /tmp/etc
	save_deploy_hashvalue=`cat /var/mnt/ostree/pkgs/ostree-version.info | awk -F " " '{print $2}'`
	tmp_path="/var/mnt/ostree/pkgs/ovl-${save_deploy_hashvalue}/etc-ovl/etc-upper/"
	if [ ! -d ${tmp_path} ]; then
		tmp_path="/var/mnt/ostree/pkgs/ovl-*.0/etc-ovl/etc-upper/"
	fi
	if [ -f ${tmp_path}shadow ]; then
     		cp -a ${tmp_path}shadow* /tmp/etc
	fi
	if [ -f ${tmp_path}passwd ]; then
     		cp -a ${tmp_path}passwd* /tmp/etc
	fi
	if [ -f ${tmp_path}gshadow ]; then
     		cp -a ${tmp_path}gshadow* /tmp/etc
	fi
	if [ -f ${tmp_path}group ]; then
     		cp -a ${tmp_path}group* /tmp/etc
	fi
	if [ -f ${tmp_path}hostname ]; then
     		cp -a ${tmp_path}hostname /tmp/etc
	fi
	if [ -f ${tmp_path}hosts ]; then
     		cp -a ${tmp_path}hosts /tmp/etc
	fi
	
	if [ -f ${tmp_path}uid_list ]; then
                cp -a ${tmp_path}uid_list /tmp/etc
        fi

	if [ -f ${tmp_path}subgid ]; then
                cp -a ${tmp_path}subgid* /tmp/etc
        fi
	if [ -f ${tmp_path}subuid ]; then
                cp -a ${tmp_path}subuid* /tmp/etc
        fi 

	if [ -f ${tmp_path}lightdm/lightdm.conf ]; then
		mkdir -p /tmp/etc/lightdm
		cp -a ${tmp_path}lightdm/lightdm.conf /tmp/etc/lightdm
	fi

	# 保留激活文件
	if [ -f ${tmp_path}.kyinfo ]; then
                cp -a ${tmp_path}.kyinfo /tmp/etc/
        fi
	if [ -f ${tmp_path}LICENSE ]; then
                cp -a ${tmp_path}LICENSE /tmp/etc/
        fi
	if [ -f ${tmp_path}.kyhwid ]; then
                cp -a ${tmp_path}.kyhwid /tmp/etc/
        fi
        if [ -f ${tmp_path}.kyactivation ]; then
                cp -a ${tmp_path}.kyactivation /tmp/etc/
        fi
	if [ -f ${tmp_path}.product ]; then
                cp -a ${tmp_path}.product /tmp/etc/
        fi



	if [ -f /tmp/etc/passwd ]; then

		if [[ -f /usr/share/kylin-os-installer/user-groups.txt ]]; then
			rm -rf /usr/share/kylin-os-installer/user-groups.txt
		fi
               
		user_name=
                user_names=
                user_names=`cat "/tmp/etc/passwd" | grep home | grep "/bin/bash"`
                for i in ${user_names}; do
                        user_name=${i%%:*}
                        echo "${user_name}"
                        user_groups="adm cdrom sudo dip plugdev users lpadmin sambashare"
                        echo "${user_name}=${user_groups}" >> /usr/share/kylin-os-installer/user-groups.txt
                done

        fi
	if [ -f ${tmp_path}fstab ]; then
		cp -a ${tmp_path}fstab /tmp
	fi
	
	umount "$1"
}

save_part=${disk##*/}
save_part_list=$(cat "/var/log/installer/os-prober.log" |grep "/dev/" |grep ^${save_part})
echo "${save_part_list}"
save_part_label_list=$(echo "${save_part_list}"| awk '{print $2}')
echo "${save_part_label_list}"

for i in ${save_part_label_list}
  do
      save_part_dev=$(echo "${save_part_list}" | grep $i | awk '{print $3}')
      case "${i}" in
      root)
              part_root="${save_part_dev}"
              echo "root=${part_root}"
              save_history_data ${part_root}
              fs=$(lsblk -o KNAME,FSTYPE ${part_root} | grep ${part_root##*/} | awk '{print $2}')
              case "$fs" in
                      fat32|fat16)
                      mkfs.vfat -n "$i" "${part_root}";;
                      ext4)
                      mkfs.ext4 -Fq -L SYSROOT "${part_root}";;
                      xfs)
                      mkfs.xfs -fq -L SYSROOT "${part_root}";;
                      btrfs)
                      mkfs.btrfs -fq -L SYSROOT "${part_root}";;
                      ntfs)
                      mkfs.ntfs -fq -L "$i" "${part_root}";;
                      *)
                      mkfs.${fs} -Fq -L SYSROOT "${part_root}" ||  mkfs.${fs} -fq -L SYSROOT "${part_root}";;
              esac
              mkdir -p /target
              mount "${part_root}" /target
	      echo "part-root=${part_root}" >>/tmp/ky-installer.cfg
              ;;
        *)
              continue
              ;;
        esac
 done

for i in ${save_part_label_list}
    do
        save_part_dev=$(echo "${save_part_list}" | grep $i | awk '{print $3}')
        case "${i}" in
        efi)
                part_efi="${save_part_dev}"
                echo "efi=${part_efi}"
                do_mkfs_efi ${part_efi}
		echo "part-efi=${part_efi}" >>/tmp/ky-installer.cfg
                ;;
        boot)
                part_boot="${save_part_dev}"
                echo "boot=${part_boot}"
                do_mkfs_boot "${part_boot}"
                #mkdir -p /target/boot
                #mount "${part_boot}" /target/boot
		echo "part-boot=${part_boot}" >>/tmp/ky-installer.cfg
                ;;
        root)
                continue
                ;;
        backup)
                part_backup="${save_part_dev}"
                echo "backup=${part_backup}"
                mkfs.ext4 -Fq -L KYLIN-BACKUP "${part_backup}"
                #mkdir -p /target/backup
                #mount "${part_backup}" /target/backup
		echo "part-backup=${part_backup}" >>/tmp/ky-installer.cfg
                #set_backup "${part_backup}"
                ;;
        data)
                part_data="${save_part_dev}"
                echo "data=${part_data}"
                mkdir -p /target/data
               # mount "${part_data}" /target/data
               # chmod 1777 /target/data
               # do_bind_data
		echo "part-data=${part_data}" >>/tmp/ky-installer.cfg
                ;;
        swap)
                part_swap="${save_part_dev}"
                echo "swap=${part_swap}"
                mkswap -L SWAP "${part_swap}"
                swapon "${part_swap}"
                ;;
	*)
                part_other="${save_part_dev}"
                echo "other=${part_other}"
                uuid=$(lsblk -o KNAME,UUID ${part_other} | grep ${part_other##*/} | awk '{print $2}')
                mount=
                test_mount=
                test_mount=$(cat "/tmp/fstab"  | grep ${uuid} | awk '{print $5}') || true
                if [[ -n ${test_mount} ]]; then
                        mount=$(cat "/tmp/fstab"  | grep ${uuid} | awk '{print $2}'| grep "/" ) || true
                else
                        test_mount=$(cat "/tmp/fstab"  | grep ${part_other} | awk '{print $5}') || true
                        if [[ -n ${test_mount} ]]; then
                                mount=$(cat "/tmp/fstab"  | grep ${part_other} | awk '{print $2}'| grep "/" ) || true
                        fi
                fi
                if [[ -n ${mount} ]]; then
                        mkdir -p /target${mount}
                        mount "${part_other}" /target${mount}
                fi
                ;;
        esac
   done



#if [[ "${is_efi}" == "true" ]]; then
#  mkdir -p /target/boot/efi
#  mount "${part_efi}" /target/boot/efi
#fi


