#!/bin/bash
#
# This script is used to clone a software image from one bank to another.
#

source /ciena/scripts/utils.sh

# globals
debug=0       # enabled by --debug or --dry-run
dry_run=0     # enabled by --dry-run

# exit codes (constants - don't change these, as they will be used in app code)
EXIT_SUCCESS=0
EXIT_INVALID_ARGS=1
EXIT_NO_SOURCE=2
EXIT_NOT_ROOT=3
EXIT_COMMAND_FAILED=4

# -----------------------------------------------------------------------------
print_usage_and_exit() {
    echo "Usage: $this_script [option] [A|B]"
    echo
    echo -e "\tWhen called without a parameter, copies the software load from"
    echo -e "\tthe active image bank into the standby.  When called with a"
    echo -e "\tparameter, copies from the specified bank."
    echo
    echo "Options:"
    echo -e "\t--debug    displays debug information while running"
    echo -e "\t--dry-run  shows commands that will be run (but does nothing)"
    echo -e "\t--help     shows this message"
    echo
    exit $EXIT_INVALID_ARGS
}

# -----------------------------------------------------------------------------
die() {
    echo $2
    exit $1
}

# -----------------------------------------------------------------------------
command_wrapper() {
    local comment=$1; shift
    local command_line="$*"

    if [ "$debug" -eq "1" ] ; then
        echo "# $comment"
        echo "$command_line"
        echo
    fi

    if [ "$dry_run" -eq "1" ] ; then
        return 0
    fi

    if [ "$debug" -eq "1" ] ; then
        $command_line
    else
        $command_line &> /dev/null
    fi

    return "$?"
}

# -----------------------------------------------------------------------------
exit_on_error() {
    local comment=$1

    command_wrapper "$@"

    if [ "$?" -ne "0" ] ; then
        echo "ERROR: $comment failed"

        # try to mount things the way they are supposed to be mounted
        command_wrapper "mount app ro"    mount $dst_app_mount -o remount,ro
        command_wrapper "mount kernel ro" mount $dst_kern_mount -o remount,ro

        exit $EXIT_COMMAND_FAILED
    fi
}

# -----------------------------------------------------------------------------
# Main script

# Take note of our name as invoked
this_script=$(basename $0)

# handle options
while :; do
    case "$1" in
        "--help"|"-h"|"-?"|"help") print_usage_and_exit ;;
        "--debug") debug=1 ; shift ;;
        "--dry-run") debug=1 ; dry_run=1; shift ;;
        *) break ;;
    esac
done

# If first parameter exists, then it should indicate the active image
if [ "$#" -gt "0" ] ; then
    active_image="$1"
else
    get_running_bank
    bank=$?

    case "$bank" in
        $EXIT_BANKA) active_image="A" ;;
        $EXIT_BANKB) active_image="B" ;;
        *)           active_image=""  ;;
    esac
fi

if [ "$#" -gt "1" ] ; then
    echo "ERROR: too many parameters"
    print_usage_and_exit
fi

if [ "$(whoami)" != "root" ] ; then
    echo "ERROR: must run $this_script as root"
    exit $EXIT_NOT_ROOT
fi

case "$active_image" in
    a|A) active_image="A"
         src_app_mount=/mnt/imageA
         dst_app_mount=/mnt/imageB
         src_rfs_mount=/mnt/ramfsA
         dst_rfs_mount=/mnt/ramfsB
        ;;
    b|B) active_image="B"
         src_app_mount=/mnt/imageB
         dst_app_mount=/mnt/imageA
         src_rfs_mount=/mnt/ramfsB
         dst_rfs_mount=/mnt/ramfsA
         ;;
    *)   active_image=""
         ;;
esac

if [ -z "$active_image" ] ; then
    die $EXIT_NO_SOURCE "ERROR: unable to determine source bank"
fi

if [ "$debug" -eq "1" ] ; then
    echo "active_image  = $active_image"
    echo "src_app_mount = $src_app_mount"
    echo "dst_app_mount = $dst_app_mount"
    echo "src_rfs_mount = $src_rfs_mount"
    echo "dst_rfs_mount = $dst_rfs_mount"
fi

# Main protect logic
#
exit_on_error "mount src ramfs ro"     mount $src_rfs_mount -o ro
exit_on_error "mount dst ramfs rw"     mount $dst_rfs_mount -o rw
exit_on_error "erase ramfs files"      rm -rf ${dst_rfs_mount}/*
exit_on_error "copy ramfs files"       cp -a ${src_rfs_mount}/* $dst_rfs_mount
exit_on_error "umount src ramfs"       umount $src_rfs_mount
exit_on_error "umount dst ramfs"       umount $dst_rfs_mount
exit_on_error "remount app rw"         mount $dst_app_mount -o remount,rw
exit_on_error "erase app files"        rm -rf ${dst_app_mount}/*
exit_on_error "copy app files"         cp -a ${src_app_mount}/* $dst_app_mount
exit_on_error "remount app ro"         mount $dst_app_mount -o remount,ro
exit_on_error "cd to $dst_app_mount"   cd $dst_app_mount
exit_on_error "check manifest(sha256)" sha256sum -s -c ./MANIFEST_SHA256
exit_on_error "syncing app"            sync

exit $EXIT_SUCCESS
