#!/bin/bash

set -e

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

### efi
start=1
end=$((start + efi))
parted -s "${disk}" mkpart "EFI" fat32 "${start}"MiB "${end}"MiB

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

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

if [[ "${isluks_lvm}" == "true" ]]; then
    PASSWORD="$(get_value_bytearray encryptyPWD)"
    parted -s "${disk}" set 1 esp 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 gpt
        parted -s "${data_device}" mkpart "data" 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}"3 -q
            echo "${PASSWORD}" | cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"3 -q
        else
            #	echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"3 -q
            echo "${PASSWORD}" | cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"3 -q
        fi
        echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"3 "${disk##*/}"3_crypt
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}3 " | awk '{print $2}')
        echo "${disk##*/}3_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##*/}"3_crypt
        vgcreate kylin-vg /dev/mapper/"${disk##*/}"3_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}"3 -q
            echo "${PASSWORD}" | cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"3 -q
        else
            # echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"3 -q
            echo "${PASSWORD}" | cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"3 -q
        fi
        echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"3 "${disk##*/}"3_crypt
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}3 " | awk '{print $2}')
        echo "${disk##*/}3_crypt UUID=${root_uuid} none luks" >/etc/crypttab
        pvcreate -ffy /dev/mapper/"${disk##*/}"3_crypt
        vgcreate kylin-vg /dev/mapper/"${disk##*/}"3_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 esp 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 gpt
        parted -s "${data_device}" mkpart "data" ext4 1MiB 100%
        partprobe "${data_device}"
        sync
        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
        pvcreate -ffy /dev/"${data_device##*/}"1
        vgcreate kylin-vg1 /dev/"${data_device##*/}"1
        lvcreate --wipesignatures n -l 100%free -n data kylin-vg1
        
        pvcreate -ffy /dev/"${disk##*/}"3
        vgcreate kylin-vg /dev/"${disk##*/}"3
        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
        pvcreate -ffy /dev/"${disk##*/}"3
        vgcreate kylin-vg /dev/"${disk##*/}"3
        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

### backup
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 gpt
        parted -s "${data_device}" mkpart "data" ext4 1MiB 100%
        if [[ "${is_swapfile}" == "true" ]]; then
            parted -s "${disk}" mkpart "SYSROOT" ext4 "${start}"MiB 100%
        else
            parted -s "${disk}" mkpart "SYSROOT" ext4 "${start}"MiB "${end}"MiB
            start=${end}
            parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    else
        parted -s "${disk}" mkpart "SYSROOT" ext4 "${start}"MiB "${end}"MiB
        start=${end}
        if [[ "${is_swapfile}" == "true" ]]; then
            if [[ $data_size -gt 0 ]]; then
                parted -s "${disk}" mkpart "data" ext4 "${start}"MiB 100%
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                end=$((end + data_size))
                parted -s "${disk}" mkpart "data" ext4 "${start}"MiB "${end}"MiB
                start=${end}
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            else
                start=${end}
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            fi
        fi
    fi
else
    parted -s "${disk}" mkpart "SYSROOT" ext4 "${start}"MiB "${end}"MiB
    start=${end}
    end=$((end + backup_size))
    if [[ -n ${data_device} ]]; then
        umount -l "${data_device}" || true
        parted -s "${data_device}" mktable gpt
        parted -s "${data_device}" mkpart "data" ext4 1MiB 100%
        
        if [[ "${is_swapfile}" == "true" ]]; then
            parted -s "${disk}" mkpart "backup" ext4 "${start}"MiB 100%
        else
            parted -s "${disk}" mkpart "backup" ext4 "${start}"MiB "${end}"MiB
            start=${end}
            parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    else
        parted -s "${disk}" mkpart "backup" ext4 "${start}"MiB "${end}"MiB
        start=${end}
        if [[ "${is_swapfile}" == "true" ]]; then
            if [[ $data_size -gt 0 ]]; then
                parted -s "${disk}" mkpart "data" ext4 "${start}"MiB 100%
            fi
        else
            if [[ $data_size -gt 0 ]]; then
                end=$((end + data_size))
                parted -s "${disk}" mkpart "data" ext4 "${start}"MiB "${end}"MiB
                start=${end}
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            else
                start=${end}
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
            fi
        fi
    fi
fi

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