#!/bin/bash
#
# bootcreator
# This file is part of Raider

check_support_bootp() {		# check if bootp_mode can be used	
	# check if there is a /boot partition already
	local b=$(mount | awk '$3 == "/boot"')
	if [ -n "$b" ]; then
		return 1 # not needed. There is already a boot partition
	fi
	# check if partition mode msdos
	echo $PARTITION_TABLE_TYPE | grep -q "msdos"
	if [ $? -ne 0 ]; then
		return 2	# not supported. Partition table not msdos
	fi
}


create_disk_virt() {	# create virtual disk database with new boot partition
	ptable_type ${DISK[0]} | grep -q "msdos"
	if [ $? -ne 0 ]; then
		return 1		# does not support non-msdos partitions
	fi	
	local DISK_DB_BOOT=${TEMP}/dev-virtual-temp.db
	local BOOTP_SIZE_sectors=$(convert2sectors ${BOOTP_SIZE} ${DISK[0]})
	local boot_start_size=$(awk '$1==1 {print $12}' $DISK_DB)	# the same there was in the 1 partition
	local unused_area=$(( boot_start_size ))	# the gap between each logical partition if they exist
	local boot_last_sector=$((boot_start_size+BOOTP_SIZE_sectors-1))
	local BOOT_RECORD="0 primary ${BOOTP_SIZE_sectors} ${BOOTP_FS} 1 /boot 0 # # boot # $boot_start_size $boot_last_sector"
	local EXTENDED_RECORD="0 extended 0 # 0 # -1 # # # # 0 0"
	>$DISK_DB_BOOT
	>$DISK_DB_VIRTUAL
	>${DISK_DB_VIRTUAL}.tmp
	local total_primary=$(awk '$2=="primary"' $DISK_DB | wc -l)	# number of primary partitions
	local np_extended=$(awk '$2=="extended" {print $1}' $DISK_DB)	# partition number of extended partition (if it exists)
	local np_extended=${np_extended:=0}	# if extended partition do not exist, partition number is zero

	## Add boot partition and allocate other partitions
	echo $BOOT_RECORD > $DISK_DB_BOOT
	if [ $total_primary -eq 4 ]; then
		# create an extended partition
		while read prtdb
		do
			if [ $total_primary -eq 4 ] && [ -n "$(echo $prtdb | awk '$1==3')" ]; then
				echo "${EXTENDED_RECORD}" >> ${DISK_DB_BOOT}
				echo "$prtdb" >> ${DISK_DB_BOOT}
			else
				echo "$prtdb" >> $DISK_DB_BOOT
			fi
		done <${DISK_DB}
	elif [ $np_extended -ne 0 ]; then
		while read prtdb1
		do
			if [ $np_extended -eq 4 ]; then
				if [ -n "$(echo $prtdb1 | awk '$1==3')" ]; then
					echo "${EXTENDED_RECORD}" >> ${DISK_DB_BOOT}
					echo "$prtdb1" >> ${DISK_DB_BOOT}
				elif [ -n "$(echo "$prtdb1" | awk '$2=="extended"')" ]; then
					continue
				else
					echo "$prtdb1" >> $DISK_DB_BOOT
				fi
			else
				echo "$prtdb1" >> $DISK_DB_BOOT
			fi
		done <${DISK_DB}
	else
		cat $DISK_DB >> $DISK_DB_BOOT
	fi

	## Renumber partitions and change types if needed
	local np_p=1	# number partition primary
	local np_l=5	# number partition logical
	while read newprt
	do
		ptype=$(echo $newprt | awk '{print $2}')
		case $ptype in
			extended)
				newprt=$(echo "$newprt" | awk -v np="$np_p" '$1 = np')
				np_p=$((np_p+1))
				;;
			primary)
				if [ $np_p -le 4 ]; then
					newprt=$(echo "$newprt" | awk -v np="$np_p" '$1 = np')
					np_p=$((np_p+1))
				else
					# new logical partitions must also decrease his size by "unused area size"
					newprt=$(echo "$newprt" | awk -v np="$np_l" '$1 = np' | awk '$2 = "logical"' | awk -v ua=${unused_area} '$3=$3-ua')
					np_l=$((np_l+1))
				fi
				;;
			logical)
				newprt=$(echo "$newprt" | awk -v np="$np_l" '$1 = np')
				np_l=$((np_l+1))
				;;
		esac
		echo "$newprt" >> ${DISK_DB_VIRTUAL}.tmp
	done < $DISK_DB_BOOT
	# reset boot flag in the older boot partition
	awk '$1 > 1 && $10 == "boot" {$10="#"}{print}' ${DISK_DB_VIRTUAL}.tmp > ${DISK_DB_VIRTUAL}.tmp1
	"mv" -f ${DISK_DB_VIRTUAL}.tmp1 ${DISK_DB_VIRTUAL}.tmp

		## allocate new size partitions for "/" and /boot
		# We need not take care about lvm because in that case, if "/" partition
		# is in lvm there is always a separated /boot partition.
		# but we must care about a logical "/" partition, and fix extended partition size.
		# get new "/" partition size in sectors
		# if extended partition exists, set new size partition (add logical partitions size)
	local root_size_sectors=$(awk '$6=="/" {print $3}' $DISK_DB)
	local new_root_size_sectors=$((root_size_sectors-BOOTP_SIZE_sectors))
	awk -v nsz=${new_root_size_sectors} '$6=="/" {$3 = nsz} {print}' ${DISK_DB_VIRTUAL}.tmp > $DISK_DB_VIRTUAL
	"mv" -f ${DISK_DB_VIRTUAL} ${DISK_DB_VIRTUAL}.tmp

	# if there is an extended partition, allocate new size, because there may be new logical partitions
	local size_extended_partition=$(awk -v ua=${unused_area} '$2=="logical" {sum+= $3+ua} END {print sum}' ${DISK_DB_VIRTUAL}.tmp)
	size_extended_partition=${size_extended_partition:=0}
	if [ ${size_extended_partition} -gt 0 ]; then
		awk -v sep=${size_extended_partition} '$2=="extended" {$3 = sep} {print}' ${DISK_DB_VIRTUAL}.tmp > ${DISK_DB_VIRTUAL}
		"mv" -f ${DISK_DB_VIRTUAL} ${DISK_DB_VIRTUAL}.tmp
	fi

	# based in their positions and size, reallocate first and last sector of each partition in DISK_DB_VIRTUAL
	realloc_partitions
	# renumbering md array numbers in raid partitions
	awk '$5=="1" && $1>"1" {$7=$7+1} {print}' ${DISK_DB_VIRTUAL} > ${DISK_DB_VIRTUAL}.tmp1 
	"mv" -f ${DISK_DB_VIRTUAL}.tmp1 ${DISK_DB_VIRTUAL}
}

is_bootraid1_needed() {	# check if bootraid1_mode is needed
	# if raid not 1, check if grub 1, or lilo => needed in that case
	# return 0 => needed
	# return 1 => not needed
	if [ "$MAIN_COMMAND" != "R1" ]; then
		if [ "$BOOTLOADER" == "GRUB" ]; then
			if [ "$GRUBNAME" == "1" ]; then
				return 0
			else
			
				if ! (check_utils_versions $GRUB_VERSION 1.99); then	# if lesser then 1.99 does need bootraid1
					# some 1.98 versions allow booting from raid 4,5,6,10 but boot fails if array gets degraded.
					return 0
				fi
			fi
		elif [ "$BOOTLOADER" == "LILO" ]; then
			return 0
		fi
		return 1
	else
		# not needed
		return 1
	fi
	return 0
}


realloc_partitions() {	# allocate sectors in new DISK_DB_VIRTUAL
	local previous_last_sector
	local previous_type
	local first_sector
	local last_sector
	local np
	local type_p
	local size
	local prts
	>${DISK_DB_VIRTUAL}
	while read prts
	do
		np=$(echo $prts | awk '{print $1}')
		type_p=$(echo $prts | awk '{print $2}')
		size=$(echo $prts | awk '{print $3}')
		if [ $np -eq 1 ]; then
			previous_last_sector=$(echo $prts | awk '{print $13}')
			previous_type=$type_p
			echo "$prts" >> ${DISK_DB_VIRTUAL}
			continue	# /boot is already fixed
		fi
		if [ "$type_p" == "primary" ] || [ "$type_p" == "extended" ]; then
			first_sector=$((previous_last_sector+1))
		elif [ "$type_p" == "logical" ]; then
			if [ "$previous_type" == "logical" ]; then
				first_sector=$((previous_last_sector+unused_area+1))
			else
				# check if extended or primary
				# get extended first sector and add unused_area sectors (2048 or 63)
				first_sector=$(( $(awk '$2=="extended" {print $12}' ${DISK_DB_VIRTUAL}) + ${unused_area} ))
			fi
		fi
		last_sector=$((first_sector + size - 1))
		prts=$(echo $prts | awk -v fs="$first_sector" '$12 = fs' | awk -v ls="$last_sector" '$13 = ls')
		echo "$prts" >> ${DISK_DB_VIRTUAL}
		previous_type=${type_p}
		previous_last_sector=${last_sector}
	done < ${DISK_DB_VIRTUAL}.tmp
}




