#!/bin/sh
#
# This script will pre-format the flash before running software_install
# to write the application.
#

source /ciena/scripts/utils.sh

UBI=ubi0

# 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_COMMAND_FAILED=4

# -----------------------------------------------------------------------------
print_usage_and_exit() {
    echo "Usage: $this_script [option]"
    echo
    echo -e "\tThis script will pre-format the flash before running software_install"
    echo -e "\tto write the application."
    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
}

# -----------------------------------------------------------------------------
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"

        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

rootfs_location=$(get_rootfs_location)

if [ "$rootfs_location" == "flash" ] ; then
   echo "Cannot be run while booted from flash. Try NFS or initramfs."
   exit 1
fi

if [ "$#" -ne "0" ] ; then
    echo "ERROR: wrong number of parameters"
    print_usage_and_exit
fi

mtd=$(find_mtd_device $UBI) || die "Unable to locate mtd device"
MTD_DEVICE=$(echo -n $mtd | sed -e 's/mtd//')

if [ -c /dev/$UBI ] ; then
  exit_on_error "detaching"            ubidetach /dev/ubi_ctrl -m $MTD_DEVICE
fi

exit_on_error "formatting"             ubiformat /dev/mtd/$UBI
exit_on_error "attaching"              ubiattach /dev/ubi_ctrl -m $MTD_DEVICE
exit_on_error "making appA volume"     ubimkvol /dev/$UBI -s 128MiB -n 0 -N appA
exit_on_error "making appB volume"     ubimkvol /dev/$UBI -s 128MiB -n 1 -N appB
exit_on_error "making config volume"   ubimkvol /dev/$UBI -s 32MiB -n 2 -N config
exit_on_error "making logs volume"     ubimkvol /dev/$UBI -s 32MiB -n 3 -N logs
exit_on_error "formatting appA"        mkfs.ubifs /dev/${UBI}_0
exit_on_error "formatting appB"        mkfs.ubifs /dev/${UBI}_1
exit_on_error "mounting imageA"        mount /mnt/imageA
exit_on_error "mounting imageB"        mount /mnt/imageB

if copy_kernel; then
exit_on_error "mounting kernelA"       mount /mnt/kernelA
exit_on_error "mounting kernelB"       mount /mnt/kernelB
fi
