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



num=1
if [[ ${format_disk} == "false" ]]; then
        num=$(get_value parted-num)
	un_boot=$(get_value parted-num)
fi
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
                continue
        fi

        if [ $num -eq 2 ]; then
                start=$((end + 2))
                end=$start
                parted -s "${disk}" mkpart extended "${start}"MiB 100%
        fi


        if [[ $i == "boot" ]]; then
		parted_boot=
                parted_boot=$(get_value parted-boot)
                if [ -z  ${parted_boot} ]; then
                        start=$((end + 1))
                        end=$((start + size))
                        if [ $num -eq 1 ]; then
                                parted -s "${disk}" mkpart primary "${fs}" "${start}"MiB "${end}"MiB
                        else
                                parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                        fi
                        num=`expr $num + 1`
		else
			if [ ${un_boot} -eq 1 ]; then
				num=`expr $num + 1`
			fi
		fi
                continue
	fi
	
	if [[ $i == "root" ]]; then
                start=$((end + 1))
           	if [[ $size == "free-size" ]]; then
	                parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB 100%
        	else
		     end=$((start + size))
                     if [ $num -eq 1 ]; then
                        parted -s "${disk}" mkpart primary "${fs}" "${start}"MiB "${end}"MiB
                     else
                        parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                     fi
		fi
                num=`expr $num + 1`
                continue
        fi

        if [[ $i == "backup" ]]; then
                start=$((end + 1))
                end=$((start + size))
                if [ $num -eq 1 ]; then
                        parted -s "${disk}" mkpart primary "${fs}" "${start}"MiB "${end}"MiB
                else
                        parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                fi
                num=`expr $num + 1`
                continue
        fi

        if [[ $i == "data" ]]; then
		if [[ -n ${data_device} ]]; then
			umount -l "${data_device}" || true
			parted -s "${data_device}" mktable msdos
			parted -s "${data_device}" mkpart primary "${fs}" 1MiB 100%
		else
                	start=$((end + 1))
			if [[ $size == "free-size" ]]; then
                        	parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB 100%
                        else
                		end=$((start + size))
                		if [ $num -eq 1 ]; then
                        		parted -s "${disk}" mkpart primary "${fs}" "${start}"MiB "${end}"MiB
                		else   
                        		parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                		fi
			fi
        	        num=`expr $num + 1`
		fi
		continue
        fi
	
	if [[ $i == "swap" ]]; then
                start=$((end + 1))
                end=$((start + size))
                if [ $num -eq 1 ]; then
                        parted -s "${disk}" mkpart primary "${fs}" "${start}"MiB "${end}"MiB
                else
                        parted -s "${disk}" mkpart logical "${fs}" "${start}"MiB "${end}"MiB
                fi
                num=`expr $num + 1`
                continue
        fi

        start=$((end + 1))

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

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

