#!/bin/bash
mkdir -p "/mnt/system" "/mnt/data"; clear

while true; do
	fdisk -l | grep sd
	echo "Disque a installer? (Exemple : sda)"
	read disk
		if [ -n "$disk" ]; then
			if [ -e "/dev/$disk" ]; then
				break
			fi
		fi
	clear
done

echo "Disque : $disk"
echo '/!\ Tapez ENTRER pour continuer /!\'
echo '/!\ Sinon {CTRL+C}, Puis lancer : "/live/image/xbr/menu" /!\'
read
clear

echo "......Delete partition of $disk......"
pvremove  --force "/dev/$disk"
for partition in $(parted -s "/dev/$disk" print | awk '/^ / {print $1}'); do
	parted -s "/dev/$disk" rm ${partition}
done

echo "......Create physical partition......"
parted -s /dev/$disk mklabel msdos
parted -s /dev/$disk mkpart primary ext4 1 256
parted -s /dev/$disk set 1 boot on
parted -s /dev/$disk mkpart primary ext4 257 100%

echo "......Formating all partition......"
mkfs.ext4 -m 0 -L system_xbr "/dev/$disk"1
mkfs.ext4 -m 0 -L data_xbr "/dev/$disk"2

echo "......Mount all partition......"
mount -t ext4 "/dev/$disk"1 "/mnt/system"
mount -t ext4 "/dev/$disk"2 "/mnt/data"

echo ".....Copy xbr......."
mkdir -p "/mnt/system/xbr" "/mnt/system/boot/grub"
cp -a "/live/image/xbr" "/mnt/system"
cp "/live/image/boot/grub/grub.cfg" "/mnt/system/boot/grub"

echo ".....Read uuid on data partition......."
uuid_data=`blkid -o udev "/dev/$disk"2 | grep "ID_FS_UUID=" | cut -d '=' -f 2`
echo "uuid_data=$uuid_data" > "/mnt/system/vars"

echo ".....Install grub......."
grub-install --force --no-floppy --root-directory="/mnt/system" /dev/$disk

echo "......Umount all partition......"
umount "/mnt/system"
mkdir "/mnt/data/backup"; chmod -R 777 "/mnt/data/backup"; umount "/mnt/data"

echo "FIN...FIN...FIN..."
echo "FIN...FIN...FIN..."
