#!/bin/bash

set -e

automatic=$(get_value automatic-installation)
if [[ "$automatic" == "0" ]]; then
    return 0
    elif [[ "${is_efi}" == "true" ]]; 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

. /usr/share/kylin-os-installer/scripts/autopart.sh
disk=$(get_value devpath)
data_device=$(get_value data-device)
data_unformat=$(get_value data-unformat)
data_preserve=$(get_value data-preserve)
if [[ "${data_unformat}" == "true" ]] || [[ "${data_preserve}" == "true" ]]; then
    echo "unformat data partition"
    return 0
else
    parted -s "${disk}" mktable msdos
fi

### boot
start=1
end=$((start + boot))
parted -s "${disk}" mkpart primary ext4 "${start}"MiB "${end}"MiB

### extended
start=$((end))
end=$((start + root_size))
parted -s "${disk}" mkpart extended "${start}"MiB 100%

### lvm logical
if [[ "${isluks_lvm}" == "true" ]] || [[ "${lvm}" == "true" ]]; then
    start=$((start+1))
    parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
fi

if [[ "${isluks_lvm}" == "true" ]]; then
    PASSWORD="$(get_value_bytearray encryptyPWD)"
    parted -s "${disk}" set 1 boot on
    partprobe "${disk}"
    sync
    if echo "${disk}" | grep -q nvme; then
        disk=${disk}p
        elif echo "${disk}" | grep -q mmcblk; then
        disk=${disk}p
    fi
    sleep 1
    modprobe sm4_generic || true
    
    if [[ -n ${data_device} ]]; then
        umount -l "${data_device}" || true
        parted -s "${data_device}" mktable msdos
        parted -s "${data_device}" mkpart primary ext4 1MiB 100%
        if echo "${data_device}" | grep -q nvme; then
            data_device=${data_device}p
            elif echo "${data_device}" | grep -q mmcblk; then
            data_device=${data_device}p
        fi
        if grep -q sm4 /proc/crypto; then
            #    echo "${PASSWORD}" | cryptsetup -c sm4-xts-plain64 -h sha256 -s 256 luksFormat "${data_device}"1 -q
            echo "${PASSWORD}" | cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q
        else
            #       echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${data_device}"1 -q
            echo "${PASSWORD}" | cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q
        fi
        echo "${PASSWORD}" | cryptsetup luksOpen "${data_device}"1 "${data_device##*/}"1_crypt
        data_uuid=$(lsblk -ro name,uuid | grep "${data_device##*/}1 " | awk '{print $2}')
        pvcreate -ffy /dev/mapper/"${data_device##*/}"1_crypt
        vgcreate kylin-vg1 /dev/mapper/"${data_device##*/}"1_crypt
        lvcreate --wipesignatures n -l 100%free -n data kylin-vg1
        
        if grep -q sm4 /proc/crypto; then
            # 	echo "${PASSWORD}" | cryptsetup -c sm4-xts-plain64 -h sha256 -s 256 luksFormat "${disk}"5 -q
            echo "${PASSWORD}" | cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
        else
            #	echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"5 -q
            echo "${PASSWORD}" | cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
        fi
        echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt UUID=${root_uuid} none luks,keyscript=decrypt_keyctl" >/etc/crypttab
        echo "${data_device##*/}1_crypt UUID=${data_uuid} none luks,keyscript=decrypt_keyctl" >>/etc/crypttab
        pvcreate -ffy /dev/mapper/"${disk##*/}"5_crypt
        vgcreate kylin-vg /dev/mapper/"${disk##*/}"5_crypt
        
        if [[ "${virtual_machine}" == "true" ]]; then
            echo "this is virtual machine"
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n root kylin-vg
            else
                lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        else
            lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
            
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n backup kylin-vg
            else
                lvcreate --wipesignatures n -L "${backup_size}" -n backup kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        fi
    else
        if grep -q sm4 /proc/crypto; then
            #        echo "${PASSWORD}" | cryptsetup -c sm4-xts-plain64 -h sha256 -s 256 luksFormat "${disk}"5 -q
            echo "${PASSWORD}" | cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
        else
            #        echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"5 -q
            echo "${PASSWORD}" | cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
        fi
        echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt UUID=${root_uuid} none luks" >/etc/crypttab
        pvcreate -ffy /dev/mapper/"${disk##*/}"5_crypt
        vgcreate kylin-vg /dev/mapper/"${disk##*/}"5_crypt
        lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
        
        if [[ "${virtual_machine}" == "true" ]]; then
            echo "this is virtual machine"
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n data kylin-vg
            else
                if [[ $data_size -gt 0 ]]; then
                    lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
                fi
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
            fi
            
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n backup kylin-vg
            else
                lvcreate --wipesignatures n -L "${backup_size}" -n backup kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
                
            fi
        fi
    fi
    return 0
fi


if [[ "${lvm}" == "true" ]]; then
    parted -s "${disk}" set 1 boot on
    partprobe "${disk}"
    sync
    if echo "${disk}" | grep -q nvme; then
        disk=${disk}p
        elif echo "${disk}" | grep -q mmcblk; then
        disk=${disk}p
    fi
    sleep 1
    
    if [[ -n ${data_device} ]]; then
        umount -l "${data_device}" || true
        parted -s "${data_device}" mktable msdos
        parted -s "${data_device}" mkpart primary ext4 1MiB 100%
        if echo "${data_device}" | grep -q nvme; then
            data_device=${data_device}p
            elif echo "${data_device}" | grep -q mmcblk; then
            data_device=${data_device}p
        fi
        data_uuid=$(lsblk -ro name,uuid | grep "${data_device##*/}1 " | awk '{print $2}')
        pvcreate -ffy /dev/"${data_device##*/}"1
        vgcreate kylin-vg1 /dev/"${data_device##*/}"1
        lvcreate --wipesignatures n -l 100%free -n data kylin-vg1
        
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5 UUID=${root_uuid} none luks" >/etc/crypttab
        echo "${data_device##*/}1 UUID=${data_uuid} none luks" >>/etc/crypttab
        pvcreate -ffy /dev/"${disk##*/}"5
        vgcreate kylin-vg /dev/"${disk##*/}"5
        
        if [[ "${virtual_machine}" == "true" ]]; then
            echo "this is virtual machine"
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n root kylin-vg
            else
                lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        else
            lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
            
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n backup kylin-vg
            else
                lvcreate --wipesignatures n -L "${backup_size}" -n backup kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        fi
    else
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5 UUID=${root_uuid} none luks" >/etc/crypttab
        pvcreate -ffy /dev/"${disk##*/}"5
        vgcreate kylin-vg /dev/"${disk##*/}"5
        lvcreate --wipesignatures n -L "${root_size}" -n root kylin-vg
        
        if [[ "${virtual_machine}" == "true" ]]; then
            echo "this is virtual machine"
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n data kylin-vg
            else
                if [[ $data_size -gt 0 ]]; then
                    lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
                fi
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
            fi
            
            if [[ "${is_swapfile}" == "true" ]]; then
                lvcreate --wipesignatures n -l 100%free -n backup kylin-vg
            else
                lvcreate --wipesignatures n -L "${backup_size}" -n backup kylin-vg
                lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
                
            fi
        fi
    fi
    return 0
fi


if [[ "${virtual_machine}" == "true" ]]; then
    echo "this is virtual machine"
    if [[ -n ${data_device} ]]; then
        umount -l "${data_device}" || true
        parted -s "${data_device}" mktable msdos
        parted -s "${data_device}" mkpart primary ext4 1MiB 100%
        
        if [[ "${is_swapfile}" == "true" ]]; then
            start=$((start+1))
            parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
            start=$((start+1))
            end=$(($start+root_size))
            parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
            start=$((end+1))
            parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    else
        start=$((start+1))
        end=$(($start+root_size))
        parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
        ### 如果有swapfile，则接下来全是data分区，因为swap_file包含进根分区
        if [[ "${is_swapfile}" == "true" ]]; then
            if [[ $data_size -gt 0 ]]; then
                start=$((end+1))
                parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                start=$((end+1))
                end=$((start+data_size))
                parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
                start=$((end+1))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            else
                start=$((end+1))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            fi
        fi
    fi
else
    #根分区
    start=$((start+1))
    end=$(($start+root_size))
    parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    
    if [[ -n ${data_device} ]]; then #外部数据分区
        umount -l "${data_device}" || true
        parted -s "${data_device}" mktable msdos
        parted -s "${data_device}" mkpart primary ext4 1MiB 100%
        
        if [[ "${is_swapfile}" == "true" ]]; then #有swapfile，backup分区占剩余的100%
            start=$((end+1))
            parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
            start=$((end+1))
            end=$((start+backup_size))
            parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
            start=$((end+1))
            parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    else
        #backup分区
        start=$((end+1))
        end=$((start+backup_size))
        parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
        if [[ "${is_swapfile}" == "true" ]]; then
            if [[ $data_size -gt 0 ]]; then
                start=$((end+1))
                parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                start=$((end+1))
                end=$((start+data_size))
                parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
                start=$((end+1))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            else
                start=$((end+1))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            fi
        fi
    fi
fi

parted -s "${disk}" set 1 boot on
partprobe "${disk}"
sync
