#!/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
start=$((start + 1))
if [[ "${isluks_lvm}" == "true" ]] || [[ "${tpm}" == "true" ]] || [[ "${lvm}" == "true" ]]; then
  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
  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
        fi
	
	if grep -q sm4 /proc/crypto; then
    		echo "${PASSWORD}" | cryptsetup -c sm4-xts-plain64 -h sha256 -s 256 luksFormat "${disk}"1 -q
  	else
    		echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"1 -q
  	fi
  	echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"1 "${disk##*/}"1_crypt
       # cryptsetup-tpm2 create-pass "${data_device}"1 --luks-new-pass "${PASSWORD}"
        data_uuid=$(lsblk -ro name,partuuid | 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
  	else
    		echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"5 -q
  	fi
  	echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt
  	#cryptsetup-tpm2 create-pass "${disk}"5 --luks-new-pass "${PASSWORD}"
  	root_uuid=$(lsblk -ro name,partuuid | grep "${disk##*/}5 " | awk '{print $2}')
  	echo "${disk##*/}5_crypt PARTUUID=${root_uuid} none luks,keyscript=decrypt_keyctl" >/etc/crypttab
        echo "${data_device##*/}1_crypt PARTUUID=${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
  	else
    		echo "${PASSWORD}" | cryptsetup -c aes-xts-plain64 -h sha256 -s 512 luksFormat "${disk}"5 -q
  	fi
  	echo "${PASSWORD}" | cryptsetup luksOpen "${disk}"5 "${disk##*/}"5_crypt
  	#cryptsetup-tpm2 create-pass "${disk}"5 --luks-new-pass "${PASSWORD}"
        root_uuid=$(lsblk -ro name,partuuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt PARTUUID=${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
  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%
        if echo "${data_device}" | grep -q nvme; then
                data_device=${data_device}p
        fi
	
	cryptsetup-tpm2 create "${data_device}"1 --tpm2-no-pin=0
        data_uuid=$(lsblk -ro name,partuuid | 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

	cryptsetup-tpm2 create "${disk}"5 --tpm2-no-pin=0 
        root_uuid=$(lsblk -ro name,partuuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt PARTUUID=${root_uuid} none luks,keyscript=decrypt_tpm2" >/etc/crypttab
        echo "${data_device##*/}1_crypt PARTUUID=${data_uuid} none luks,keyscript=decrypt_tpm2" >>/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
	cryptsetup-tpm2 create "${disk}"5 --tpm2-no-pin=0 
        root_uuid=$(lsblk -ro name,partuuid | grep "${disk##*/}5 " | awk '{print $2}')
        echo "${disk##*/}5_crypt PARTUUID=${root_uuid} none luks,keyscript=decrypt_tpm2" >/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%
        if echo "${data_device}" | grep -q nvme; 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
        mb=$(echo "scale=2; $(sudo blockdev --getsize64 /dev/"${data_device##*/}"1) / 1024 / 1024-16" | bc)
	pvresize --setphysicalvolumesize ${mb}M /dev/"${data_device##*/}"1 -y
        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
 	mb=$(echo "scale=2; $(sudo blockdev --getsize64 /dev/"${disk##*/}"5) / 1024 / 1024-16" | bc)
	pvresize --setphysicalvolumesize ${mb}M /dev/"${disk##*/}"5 -y
  	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
        mb=$(echo "scale=2; $(sudo blockdev --getsize64 /dev/"${disk##*/}"5) / 1024 / 1024-16" | bc)
        pvresize --setphysicalvolumesize ${mb}M /dev/"${disk##*/}"5 -y
        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
