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

if [[ "${data_unformat}" == "true" ]]; then
        echo "unformat data partition"
	return 0
else
	parted -s "${disk}" mktable msdos
fi

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

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

### root
is_aes=false
start=$((start + 1))
if [[ "${isluks_lvm}" == "true" ]] || [[ "${tpm}" == "true" ]] || [[ "${lvm}" == "true" ]]; then
  parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
  is_aes=$(get_value default-aes)
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
  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%
  	partprobe "${data_device}"
  	sync
        if echo "${data_device}" | grep -q nvme; then
                data_device=${data_device}p
        fi
  	sleep 1
	if [[ "${is_aes}" == "true" ]]; then
                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
        else
                if grep -q sm4 /proc/crypto; then
                        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 --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q
                fi
        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 [[ "${is_aes}" == "true" ]]; then
                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
        else
                if grep -q sm4 /proc/crypto; then
                        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 --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
                fi
        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 [[ "${is_aes}" == "true" ]]; then
                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
        else
                if grep -q sm4 /proc/crypto; then
                        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 --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q
                fi
        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
                        lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
                        lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
                fi
        else
                lvcreate --wipesignatures n -L "${data_size}" -n data 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
  fi
  return 0
fi

if [[ "${tpm}" == "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
  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%
  	partprobe "${data_device}"
  	sync
        if echo "${data_device}" | grep -q nvme; then
                data_device=${data_device}p
        fi
  	sleep 1
	if [[ "${is_aes}" == "true" ]]; then
                cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q --key-tpm --regenerate
        else
                if grep -q sm4 /proc/crypto; then
                        cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q --key-tpm  --regenerate
                else
                        cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${data_device}"1 -q --key-tpm --regenerate
                fi
        fi

        cryptsetup luksOpen "${data_device}"1 "${data_device##*/}"1_crypt --key-tpm
        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 [[ "${is_aes}" == "true" ]]; then
                cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm
        else
                if grep -q sm4 /proc/crypto; then
                        cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm
                else
                        cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm
                fi
        fi

        cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt --key-tpm
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt UUID=${root_uuid} none luks,keyscript=decrypt_keytpm" >/etc/crypttab
        echo "${data_device##*/}1_crypt UUID=${data_uuid} none luks,keyscript=decrypt_keytpm" >>/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 [[ "${is_aes}" == "true" ]]; then
                cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm --regenerate
        else
                if grep -q sm4 /proc/crypto; then
                        cryptsetup --cipher=sm4-xts-plain64 --key-size=256 --hash=sm3 --keyslot-cipher=sm4-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm --regenerate
                else
                        cryptsetup --cipher=aes-xts-plain64 --key-size=512 --hash=sha256 --keyslot-cipher=aes-xts-plain64 --keyslot-key-size=256 luksFormat "${disk}"5 -q --key-tpm --regenerate
                fi
        fi

        cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt --key-tpm
        root_uuid=$(lsblk -ro name,uuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt UUID=${root_uuid} none luks,keyscript=decrypt_keytpm" >/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
                        lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
                        lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
                fi
        else
                lvcreate --wipesignatures n -L "${data_size}" -n data 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
  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
  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%
  	partprobe "${data_device}"
  	sync
        if echo "${data_device}" | grep -q nvme; then
                data_device=${data_device}p
        fi
  	sleep 1
        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
                        lvcreate --wipesignatures n -L "${data_size}" -n data kylin-vg
                        lvcreate --wipesignatures n -l 100%free -n swap kylin-vg
                fi
        else
                lvcreate --wipesignatures n -L "${data_size}" -n data 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
  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 msdos
    	parted -s "${data_device}" mkpart primary ext4 1MiB 100%
        
	if [[ "${is_swapfile}" == "true" ]]; then
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
                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
	parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    	start=$((end + 1))
        if [[ "${is_swapfile}" == "true" ]]; then
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
                end=$((end + data_size))
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    		start=$((end + 1))
                end=$((end + swap))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    fi
else
    parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    start=$((end + 1))
    end=$((end + backup_size))
    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
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
		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
	parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    	start=$((end + 1))
        end=$((end + data_size))
        if [[ "${is_swapfile}" == "true" ]]; then
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB 100%
        else
		parted -s "${disk}" mkpart logical ext4 "${start}"MiB "${end}"MiB
    		start=$((end + 1))
                end=$((end + swap))
                parted -s "${disk}" mkpart logical linux-swap "${start}"MiB 100%
        fi
    fi
fi

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