#!/bin/sh
#    Copyright (c) 2014-2016 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#     restore host images from snapshot images.
#		1) partition
#		2) copy images from snapshot partition
#

PLATFORM=SWBD2500
BD=`sin 2>/dev/null | grep "SWBD" | cut -d ':' -f 2`
SNAPSHOT_PARTITION=/dev/sdb2
SNAPSHOT_DIR=/SNAPSHOT

TMPMNT='/mnt'
warn_count=0
continue=TRUE
error=FALSE

FS=ext4
DEVICE="sda"

node="platform"
major=`grep $node\$ /proc/devices | cut -d ' ' -f 1`
if [  $major ]; then
    rm -f /dev/$node
    mknod  /dev/$node c $major 0
fi



exit_out()
{
    if [ $error = TRUE ]
    then
		echo "SNAPSHOT_RESTORE: FAILED with $warn_count warnings."
		host_snapshot_restore_cleanup
		exit 1
    else
	if [ $warn_count ]
	then
	    echo "SNAPSHOT_RESTORE: PASSED with $warn_count warnings."
	else
	    echo "SNAPSHOT_RESTORE: PASSED."
	fi
	host_snapshot_restore_cleanup
	exit 0
    fi
}

#
# do_command <warn/error> <command...>
#
do_command()
{
    warn=$1
    shift
    $*
    if [ $? -ne 0 ]
    then
	    if [ $warn = TRUE ]
	    then
	        echo "SNAPSHOT_RESTORE: Warning - failed $*"
	        warn_count=`expr $warn_count + 1`
	        return 1
	    else
	        echo "SNAPSHOT_RESTORE: ERROR - failed $*"
	        continue=FALSE
	        error=TRUE
	        exit_out
	    fi
    fi
    return 0
}



host_partition_setup()
{

echo "SNAPSHOT_RESTORE: Settingup Partition"

BLOCKS=`blockdev --getsize64 /dev/${DEVICE}`


PARTSTART="10GiB"
PART1END="20GiB"
PART2END="30GiB"

echo "SNAPSHOT_RESTORE: Drive Size: BLOCKS=${BLOCKS}"
echo "SNAPSHOT_RESTORE: Drive Size: BLOCKS=${BLOCKS} PARTSTART=${PARTSTART} PART1END=${PART1END} PART2END=${PART2END}"

# remove partitions
for partnum in `parted -s /dev/${DEVICE}  print | grep 'primary' | cut -d' ' -f1,2`
	do
        if [ $partnum -ne 1 -a $partnum -ne 2 ]; then
		echo "SNAPSHOT_RESTORE: Removing existing partition $partnum of /dev/${DEVICE}"
		do_command TRUE parted -s /dev/${DEVICE} rm $partnum 
		partprobe
		fi
	done

# do partitioning setup
echo "SNAPSHOT_RESTORE: Partition setup for /dev/${DEVICE} ..."

do_command TRUE parted -a minimal -s /dev/${DEVICE} unit GiB print
do_command TRUE parted -a minimal -s /dev/${DEVICE} mkpart primary $PARTSTART $PART1END
do_command TRUE parted -a minimal -s /dev/${DEVICE} mkpart primary $PART1END $PART2END
do_command TRUE parted -a minimal -s /dev/${DEVICE} mkpart primary $PART2END 100%
do_command TRUE parted -a minimal -s /dev/${DEVICE} unit GiB print
partprobe

# create filesystems ext4
echo "SNAPSHOT_RESTORE: Creating $FS file system on partition 3,4,5 in /dev/${DEVICE}..."

do_command TRUE /sbin/mke2fs -t $FS -g32768 -b4096 -j /dev/${DEVICE}3
do_command TRUE tune2fs -c 0 -i 0 /dev/${DEVICE}3
do_command TRUE /sbin/mke2fs -t $FS -g32768 -b4096 -j /dev/${DEVICE}4
do_command TRUE tune2fs -c 0 -i 0 /dev/${DEVICE}4
do_command TRUE /sbin/mke2fs -t $FS -g32768 -b4096 -j /dev/${DEVICE}5
do_command TRUE tune2fs -c 0 -i 0 /dev/${DEVICE}5



	for snap in `mount | cut -d" " -f3 | grep "$SNAPSHOT_DIR"`
	do
		do_command TRUE umount $SNAPSHOT_DIR
	done

	do_command TRUE mkdir -p $SNAPSHOT_DIR
	do_command TRUE mount $SNAPSHOT_PARTITION   $SNAPSHOT_DIR

}

restore_host_snapshot_images()
{
	echo "SNAPSHOT_RESTORE: restoring HOST"
	for partition in  3 4; do
		do_command TRUE mount -t $FS /dev/${DEVICE}${partition} ${TMPMNT}

		sync
		do_command TRUE  rsync -rlpgoDxt  $SNAPSHOT_DIR/HOST/ ${TMPMNT}/
		sync
		do_command TRUE umount ${TMPMNT}
	done
}


host_snapshot_restore_cleanup()
{

	for snap in `mount | cut -d" " -f3 | grep "$SNAPSHOT_DIR"`
	do
	    umount $SNAPSHOT_DIR
	done

	rmdir  $SNAPSHOT_DIR
}



# main () {

echo "SNAPSHOT_RESTORE: Firmware Restore from Snapshot Images"
echo "======================================================="

# unmount /mnt
for mnt in `mount | cut -d" " -f3 | grep "/mnt"`; do
    umount $mnt
done

# partition setup
host_partition_setup

#restore from HOST snapshot images 
restore_host_snapshot_images

#unmount and cleanup
host_snapshot_restore_cleanup
echo "SNAPSHOT_RESTORE: HOST Images Restored."

#main  end }
