#!/bin/bash

set -e

data_unformat=$(get_value data-unformat)
data_preserve=$(get_value data-preserve)
if [[ "${data_unformat}" == "true" ]] || [[ "${data_preserve}" == "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

    if [[ "${data_unformat}" == "true" ]]; then

        if [[ -f /mnt/etc/shadow ]]; then
            cp -a /mnt/etc/shadow* /tmp/etc
        fi
        if [[ -f /mnt/etc/passwd ]]; then
            cp -a /mnt/etc/passwd* /tmp/etc
        fi
        if [[ -f /mnt/etc/gshadow ]]; then
            cp -a /mnt/etc/gshadow* /tmp/etc
        fi
        if [[ -f /mnt/etc/group ]]; then
            cp -a /mnt/etc/group* /tmp/etc
        fi
        if [[ -f /mnt/etc/hostname ]]; then
            cp -a /mnt/etc/hostname /tmp/etc
        fi
        if [[ -f /mnt/etc/hosts ]]; then
            cp -a /mnt/etc/hosts /tmp/etc
        fi
        
        if [[ -f /mnt/etc/uid_list ]]; then
            cp -a /mnt/etc/uid_list /tmp/etc
        fi
        
        if [[ -f /mnt/etc/subgid ]]; then
            cp -a /mnt/etc/subgid* /tmp/etc
        fi
        if [[ -f /mnt/etc/subuid ]]; then
            cp -a /mnt/etc/subuid* /tmp/etc
        fi
        
        if [[ -f /mnt/etc/lightdm/lightdm.conf ]]; then
            mkdir -p /tmp/etc/lightdm
            cp -a /mnt/etc/lightdm/lightdm.conf /tmp/etc/lightdm
        fi
        
        # 保留激活文件
        if [[ -f /mnt/etc/.kyhwid ]]; then
            cp -a /mnt/etc/.kyhwid /tmp/etc/
        fi
        if [[ -f /mnt/etc/.kyactivation ]]; then
            cp -a /mnt/etc/.kyactivation /tmp/etc/
        fi
        
        
        if [[ -f /mnt/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 "/mnt/etc/passwd" | grep home | grep "/bin/bash"`
            for i in ${user_names}; do
                user_name=${i%%:*}
                echo "${user_name}"
                
                chroot /mnt /bin/sh -c "groups ${user_name}" >/usr/share/kylin-os-installer/123.txt
                user_groups=`cat /usr/share/kylin-os-installer/123.txt`
                rm -rf /usr/share/kylin-os-installer/123.txt
                
                echo "${user_groups}"
                user_groups=${user_groups##*${user_name} }
                user_groups=`echo ${user_groups} | grep -v ":"` || user_groups=
                if [[ -n ${user_groups} ]]; then
                    echo "${user_name}=${user_groups}" >> /usr/share/kylin-os-installer/user-groups.txt
                fi
            done
            
        fi
    fi
    # fstab要保留
    if [[ -f /mnt/etc/fstab ]]; then
        cp -a /mnt/etc/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
        ;;
        *)
            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}
        ;;
        boot)
            part_boot="${save_part_dev}"
            echo "boot=${part_boot}"
            do_mkfs_boot "${part_boot}"
            mkdir -p /target/boot
            mount "${part_boot}" /target/boot
        ;;
        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
            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
        ;;
        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


