#!/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)
if [[ "${disk_custom}" == "false" ]]; then
  return 0
fi

#兼容老版本配置文件
root_size=$(get_value disk-root)
if [[ -n "${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)

format_disk=$(get_value format-disk)
if [[ ${format_disk} == true ]]; then
        parted -s "${disk}" mktable gpt
fi

start=$(get_value kos-start)
if [ -z ${start} ]; then
        start=1
fi
end=${start}

custom_partitions=$(get_value custom-partitions | sed 's/\"//g')
one_list=${custom_partitions//;/ }
two_list=
for p in ${one_list[@]}; do
        two_list="${two_list} $(echo $p | cut -f1)"
done
three_list=$(echo ${two_list} | tr ' ' '\n')

for i in ${three_list}; do
        custom_partition=$(get_value "custom-$i" | sed 's/\"//g')
        custom_partition=${custom_partition//;/|}
        fs=
        fs=$(echo ${custom_partition#*fs=})
        fs=$(echo ${fs%%|*})

        size=
        size=$(echo ${custom_partition#*size=})
        size=$(echo ${size%%|*})

        if [[ $i == "efi" ]]; then
		parted_efi=
                parted_efi=$(get_value parted-efi)
                if [ -z  ${parted_efi} ]; then
                        end=$((end + size + 1))
                        parted -s "${disk}" mkpart "EFI" "${fs}" "${start}"MiB "${end}"MiB
                        start=${end}
                fi
                continue
        fi

        if [[ $i == "boot" ]]; then
                end=$((end + size))
                parted -s "${disk}" mkpart "boot" "${fs}" "${start}"MiB "${end}"MiB
                start=${end}
                continue
        fi

        if [[ $i == "root" ]]; then
               	if [[ $size == "free-size" ]]; then
	                parted -s "${disk}" mkpart "SYSROOT" "${fs}" "${start}"MiB 100%
        	else
			end=$((end + size))
                	parted -s "${disk}" mkpart "SYSROOT" "${fs}" "${start}"MiB "${end}"MiB
                	start=${end}
		fi
                continue
        fi

        if [[ $i == "backup" ]]; then
                end=$((end + size))
                parted -s "${disk}" mkpart "backup" "${fs}" "${start}"MiB "${end}"MiB
                start=${end}
                continue
        fi
	
	if [[ $i == "data" ]]; then
		if [[ -n ${data_device} ]]; then
			umount -l "${data_device}" || true
			parted -s "${data_device}" mktable gpt
			parted -s "${data_device}" mkpart "data" "${fs}" 1MiB 100%
		else
			if [[ $size == "free-size" ]]; then
                                parted -s "${disk}" mkpart "data" "${fs}" "${start}"MiB 100%
                        else
             			end=$((end + size))
                		parted -s "${disk}" mkpart "data" "${fs}" "${start}"MiB "${end}"MiB
                		start=${end}
			fi
		fi
		continue
        fi

        if [[ $i == "swap" ]]; then
                end=$((end + size))
                parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                start=${end}
                continue
        fi
	
	if [[ $size == "free-size" ]]; then
                parted -s "${disk}" mkpart "$i" "${fs}" "${start}"MiB 100%
        else
        	end=$((end + size))
        	parted -s "${disk}" mkpart "$i" "${fs}" "${start}"MiB "${end}"MiB
        	start=${end}
	fi
done
num=1
if [[ ${format_disk} == "false" ]]; then
        num=$(get_value parted-num)
fi

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

