#!/bin/bash
#
# ptclone (functions related to partition cloning)
# This file is part of Raider

clone_gpt_partition() {		# clone gpt partition table 
	local disk_to_clone=$1
	partition_table_type=$(ptable_type ${DISK[0]})
	if ! (echo $partition_table_type | grep -qi "gpt"); then
		return 1	# partition table not gpt
	fi
	if ! (sfdisk -R ${disk_to_clone}); then
		return 2	# disk currently in use
	fi

	# create partition table type gpt
	logging "Clone gpt partition with parted:"
	env LC_ALL=C parted -s ${disk_to_clone} mklabel gpt >>$LOGFILE 2>&1

	p=0	# count partition number
	while read parts
	do
		p=$((p+1))
		FS_TYPE=$(echo ${parts} | awk -F: '{print $5}')	# only usefull for formatting
		GPT_PARTITION_NAME=$(echo ${parts} | awk -F: '{print $6}')
		
		START=$(echo ${parts} | awk -F: '{print $2}')
		END=$(echo ${parts} | awk -F: '{print $3}')
		FLAG=$(echo ${parts} | awk -F: '{print $7}' | sed 's/;$//g')
		
		env LC_ALL=C parted -s ${disk_to_clone} mkpart "P${p}" ${START} ${END}	>>$LOGFILE 2>&1 # name will be by default P1, P2, ...
		# this is a workaround to allow blank names
		echo "'"$GPT_PARTITION_NAME"'" | parted ${disk_to_clone} name $p $GPT_PARTITION_NAME	>>$LOGFILE 2>&1 # then will change to the real name.
		if [ -n "${FLAG}" ]; then
			env LC_ALL=C parted -s ${disk_to_clone} set ${p} ${FLAG} on	>>$LOGFILE 2>&1
		fi
	done < <(env LC_ALL=C parted -m -s ${DISK[0]} u B p | tail -n+3)
	partprobe ${disk_to_clone}
}

part_copy_from_disk_virt() {	# create disk partitions copied from disk_virt (BOOT_P)
	local disk=$1
	logging "Create partition from virtual disk:"
	logging "parted -s $disk mklabel msdos"
	parted -s $disk mklabel "msdos" >>$LOGFILE 2>&1 
	while read prttion
	do
		local np=$(echo "$prttion" | awk '{print $1}')
		local p_type=$(echo "$prttion" | awk '{print $2}')
		local start="$(echo "$prttion" | awk '{print $12}')s"
		local end="$(echo "$prttion" | awk '{print $13}')s"
		local fs=$(echo "$prttion" | awk '{print $4}')
		local boot_flag=$(echo "$prttion" | awk '{print $10}')
		if [ "$fs" == "swap" ]; then
			fs="linux-swap"
		else
			fs=""
		fi
		env LC_ALL=C parted --help | grep -q "\-\-align"
		if [ $? -eq 0 ]; then
			minim="-a minimal "
		else
			minim=""
		fi
		logging "parted ${minim}-s ${disk} mkpart ${p_type} ${fs} ${start} ${end}"
		env LC_ALL=C parted ${minim}-s ${disk} mkpart ${p_type} ${fs} ${start} ${end} >>$LOGFILE 2>&1 
		sleep .5	# need to settle down before go on using parted

		if [ "$fs" == "linux-swap" -o "$p_type" == "extended" ]; then
			logging "parted -s ${disk} set $np "lba" off"
			env LC_ALL=C parted -s ${disk} set $np "lba" off >>$LOGFILE 2>&1 
			sleep .5
		fi
		if [ "$fs" == "linux-swap" ]; then	# if it is a swap partition, change id to 82 (parted does not do that)
			sfdisk --change-id ${disk} $np 82 >>$LOGFILE 2>&1
		fi
		if [ "$boot_flag" == "boot" ]; then
			logging "parted -s ${disk} set $np boot on "
			env LC_ALL=C parted -s ${disk} set $np "boot" on >>$LOGFILE 2>&1 
			sleep .5
		fi
		logging "Inform kernel about new partition ${disk}"
		sfdisk -R -f ${disk} >>$LOGFILE 2>&1
	done < $DISK_DB_VIRTUAL
}

ptable_type() {		# parse partition table type
	# e.g: ptable_type /dev/sdb
	local part_table=$(parted_machine $1 | awk -F: 'NR==2 {print $6}')
	if [ "$part_table" == "msdos" ]; then 
		p_table="MBR/msdos"
	elif [ "$part_table" == "gpt" ]; then
		p_table="GPT/GUID"
	else
		p_table=$part_table
	fi
	echo "$p_table"
	if [ -z "$p_table" ]; then
		#Partition table not found
		return 1
	fi
}




