# backup
#  dev_ids - stores device id's for dev/disk/by-id remapping after restore
>$RAMTEMP/dev_ids
>$RAMTEMP/local_ids
for i in `ls /dev/disk/by-id/`; do echo "$i $(readlink /dev/disk/by-id/$i)" >> $RAMTEMP/local_ids; done
for i in $DEVICES; do
    for j in $(sfdisk -l $i | grep ^/dev/ | cut -d" " -f1 | cut -d"/" -f3- | awk '{print "../../"$0}'); do
    dev="/dev/$(echo $j | cut -d"/" -f3-)"
    cat $RAMTEMP/local_ids | grep -v ^edd | grep "$j$" | awk -v dev=$dev '{print dev" " $1}' >>$RAMTEMP/dev_ids
    done
    cat $RAMTEMP/local_ids | grep -v ^edd | grep -w $(echo $i | cut -d"/" -f3-) | awk -v dev=/dev/$(echo $i | cut -d"/" -f3-) '{print dev" " $1}' >>$RAMTEMP/dev_ids
done
rm $RAMTEMP/local_ids




# restore
# hack fstab to use new ids ++++++++++++++++++++++++++++++++
# assumes that partition names are the same as on backup
echo "Adjust fstab..."
>/mnt/RESTORING/etc/fstab.ramondo
>$CONFIGS/local_ids
# $1 - /dev/disk/by-id/...
GetKernelId() {
    dev=$(echo $1 | cut -d"/" -f5)
    old_dev=$(cat /tmp/dev_ids | grep $dev$ | cut -d" " -f1)
    echo $old_dev
}
# $1 - kernel name
GetIdFromKernel() {
    >$CONFIGS/local_ids
    for i in `ls /dev/disk/by-id`; do
    if [ "$(echo $i | grep ^edd)" != "" ]; then
        continue
    fi
    echo "$i $(readlink /dev/disk/by-id/$i)" >>$CONFIGS/local_ids
    done
    cat $CONFIGS/local_ids | grep -w "$(echo $1 | cut -d"/" -f3- | awk '{print "../../"$0}' )$" | sort | head -n1 | awk '{print "/dev/disk/by-id/"$1}'
}
# $1 - file to move from $2 - file to move to
MvFileSafe() {
    if [ $(stat -c "%s" $1) -gt 0 ]; then
        rm $2
    mv $1 $2
    else
    logit "Cannot copy over zero - length file"
    fi
}
cp /mnt/RESTORING/etc/fstab /mnt/RESTORING/etc/fstab.ramondo
cat /mnt/RESTORING/etc/fstab | while read i; do
    dev=$(echo $i | cut -d" " -f1 | grep ^/dev/disk/by)
    #check for existing id
    if [ "$dev" != "" ]; then
    if [ ! -e $dev ]; then
        # have to remap
        echo "$dev not exist"
        kern_dev=$(GetKernelId $dev)
        if [ "$kern_dev" != "" ]; then
        new_dev=$(GetIdFromKernel $kern_dev)
        echo "New ID for $kern_dev will be: $new_dev"

        find=$(echo $dev | cut -d"/" -f5)
        replace=$(echo $new_dev | cut -d"/" -f5)
        cat /mnt/RESTORING/etc/fstab.ramondo | sed 's/'$find'/'$replace'/g' >/mnt/RESTORING/etc/fstab.ramondo.temp
        rm /mnt/RESTORING/etc/fstab.ramondo
        mv /mnt/RESTORING/etc/fstab.ramondo.temp /mnt/RESTORING/etc/fstab.ramondo
        else
        echo "Error: device id was not present at backup"
        fi
    fi
    fi
done
MvFileSafe "/mnt/RESTORING/etc/fstab.ramondo" "/mnt/RESTORING/etc/fstab"

#  hack /boot/grub/device.map ++++++++++++++++++++++++++++++++++++
echo "Adjust device.map..."
cp /mnt/RESTORING/boot/grub/device.map /mnt/RESTORING/boot/grub/device.map.ramondo
cat /mnt/RESTORING/boot/grub/device.map | while read i; do
    dev=$(echo $i | awk '{print $2}' | grep ^/dev/disk/by)
    if [ "$dev" != "" ]; then
    if [ ! -e $dev ]; then
        echo "$dev not exist..."
        kern_dev=$(GetKernelId $dev)
        if [ "$kern_dev" != "" ]; then
        new_dev=$(GetIdFromKernel $kern_dev)
        echo "New ID for $kern_dev will be: $new_dev"

        find=$(echo $dev | cut -d"/" -f5)
        replace=$(echo $new_dev | cut -d"/" -f5)
        cat /mnt/RESTORING/boot/grub/device.map.ramondo | sed 's/'$find'/'$replace'/g' >/mnt/RESTORING/boot/grub/device.map.ramondo.temp
        rm /mnt/RESTORING/boot/grub/device.map.ramondo
        mv /mnt/RESTORING/boot/grub/device.map.ramondo.temp /mnt/RESTORING/boot/grub/device.map.ramondo
        else
        echo "Error: device id was not present at backup"
        fi
    fi
    fi
done
MvFileSafe "/mnt/RESTORING/boot/grub/device.map.ramondo" "/mnt/RESTORING/boot/grub/device.map"

# hack /boot/grub/menu.lst ++++++++++++++++++++++++++++++++++++
echo "Adjust menu.lst..."

rootdev=$(cat /mnt/RESTORING/boot/grub/menu.lst | grep -v ^# | grep kernel -m 1 | awk -F "root=" '{print $2}' | cut -d" " -f1)
if [ "$(echo $rootdev | grep "/dev/disk/by")" != "" ]; then
    if [ ! -e $rootdev ]; then
    echo "Root device $rootdev not present"
    kern_dev=$(GetKernelId $rootdev)
    if [ "$kern_dev" != "" ]; then
        new_dev=$(GetIdFromKernel $kern_dev)
        echo "New ID for $kern_dev will be: $new_dev"
        find=$(echo $rootdev | cut -d"/" -f5)
        replace=$(echo $new_dev | cut -d"/" -f5)
        cat /mnt/RESTORING/boot/grub/menu.lst | sed 's/'$find'/'$replace'/g' >/mnt/RESTORING/boot/grub/menu.lst.ramondo
        MvFileSafe /mnt/RESTORING/boot/grub/menu.lst.ramondo /mnt/RESTORING/boot/grub/menu.lst
    else
        echo "Error: Old root device id was not found. You will now have chance to edit menu.lst and supply valid device."
        sleep 10
        vi /mnt/RESTORING/boot/grub/menu.lst
    fi
    fi
fi

rootdev=$(cat /mnt/RESTORING/boot/grub/menu.lst | grep -v ^# | grep kernel -m 1 | awk -F "resume=" '{print $2}' | cut -d" " -f1)
if [ "$(echo $rootdev | grep "/dev/disk/by")" != "" ]; then
    if [ ! -e $rootdev ]; then
    echo "Resume device $rootdev not present"
    kern_dev=$(GetKernelId $rootdev)
    if [ "$kern_dev" != "" ]; then
        new_dev=$(GetIdFromKernel $kern_dev)
        echo "New ID for $kern_dev will be: $new_dev"
        find=$(echo $rootdev | cut -d"/" -f5)
        replace=$(echo $new_dev | cut -d"/" -f5)
        cat /mnt/RESTORING/boot/grub/menu.lst | sed 's/'$find'/'$replace'/g' >/mnt/RESTORING/boot/grub/menu.lst.ramondo
        MvFileSafe /mnt/RESTORING/boot/grub/menu.lst.ramondo /mnt/RESTORING/boot/grub/menu.lst
    else
        echo "Error: resume device was not found. You will now have chance to edit menu.lst and supply valid device."
        sleep 10
        vi /mnt/RESTORING/boot/grub/menu.lst
    fi
    fi
fi




