#!/bin/bash

# factmode - enable or disable factory mode on the RG
# Primarily, this will remount CM nonvol partitions as read-write or read-only
# based on the command line arg to enable or disable.

declare -r CM_NV_DIR="/mnt/cmnonvol"
declare -r CM_NV_BAK_DIR="/mnt/cmnonvol_bak"
declare -r CM_PRIV_IP_ADDR="172.31.255.45"
declare -r NO_LOCK_MAGIC_FILE=/data/.nolockcmnonvol

. /etc/init.d/rcS.util nodump

# print usage to console
usage ()
{
    echo " "
    echo "    Enable or disable factory mode"
    echo " "
    echo "    usage:"
    echo "          $0 <option>"
    echo " "
    echo "    options:"
    echo "          -e, --enable"
    echo "          -d, --disable"
    echo "          -s, --status"
    echo "          -h, --help"
    echo " "
    return 0
}

# specify that there was an error, then print usage
usage_error ()
{
    echo "    Wrong number of arguments or invalid option."
    echo " "
    usage
    return 1
}

# Enable factory mode
# remount CM nonvol partitons to read-write
enable_factory_mode ()
{
    echo "Enabling factory mode"
    mount -o remount,rw $CM_NV_BAK_DIR
    if [ $? -ne 0 ]; then
        echo "Remount operation for $CM_NV_BAK_DIR failed"
        return 1
    fi
    mount -o remount,rw $CM_NV_DIR
    if [ $? -ne 0 ]; then
        echo "Remount operation for $CM_NV_DIR failed"
        return 1
    fi
    return 0
}

# Disable factory mode
# remount CM nonvol partitions as read-only
disable_factory_mode ()
{

    echo "Disabling factory mode"
    # only mount read-only if the magic file is not present
    if [ -e "$NO_LOCK_MAGIC_FILE" ]; then
        echo "Not mounting cm nonvol read-only due to magic file"
    else
        mount -o remount,ro $CM_NV_BAK_DIR
        if [ $? -ne 0 ]; then
            echo "Remount operation for $CM_NV_BAK_DIR failed"
            return 1
        fi
        mount -o remount,ro $CM_NV_DIR
        if [ $? -ne 0 ]; then
            echo "Remount operation for $CM_NV_DIR failed"
            return 1
        fi
    fi
    return 0
}

# Get factory mode status
# Report the read-only/read-write status of CM nonvol partitons
# and the current status of the CM's factory mode as reported by
# cdPrivateMibEnable SNMP mib
get_factory_mode_status ()
{
    echo "   ---Factory mode status---"

    # check read-only status of cmnonvol partition
    grep -qw "$CM_NV_DIR" /proc/mounts
    if [ $? -eq 0 ] ; then
        grep -w "$CM_NV_DIR" /proc/mounts | grep -q ro
        if [ $? -eq 0 ] ; then
            echo "$CM_NV_DIR partition:      read-only"
        else
            echo "$CM_NV_DIR partition:      read-write"
        fi
    else
        echo "Could not identify $CM_NV_DIR mount status"
    fi

    # check read-only status of cmnonvol_bak partition
    grep -qw "$CM_NV_BAK_DIR" /proc/mounts
    if [ $? -eq 0 ] ; then
        grep -w "$CM_NV_BAK_DIR" /proc/mounts | grep -q ro
        if [ $? -eq 0 ] ; then
            echo "$CM_NV_BAK_DIR partition:  read-only"
        else
            echo "$CM_NV_BAK_DIR partition:  read-write"
        fi
    else
        echo "Could not identify $CM_NV_BAK_DIR mount status"
    fi

    # get CM factory mode via snmp
    cmfactmode=$(snmpget -v 2c -c public -O qv -m "" $CM_PRIV_IP_ADDR 1.3.6.1.4.1.4413.2.99.1.1.1.1.0)
    case $cmfactmode in
        0) echo "CM cdPrivateMibEnable:        disabled(0)";;
        1) echo "CM cdPrivateMibEnable:        factory(1)";;
        2) echo "CM cdPrivateMibEnable:        engineering(2)";;
        *) echo "Could not retrieve CM cdPrivateMibEnable MIB value";;
    esac

    if [ -e $NO_LOCK_MAGIC_FILE ]; then
        echo "$NO_LOCK_MAGIC_FILE         exists"
    fi

    return 0
}

# Main entry point; process cmd line args and redirect accordingly
main ()
{

    local retval=0

    if [ "${CHIP:0:4}" = "3390" ]; then
        if [[ "$#" -ne 1 ]]; then
            usage_error
            retval=1
        else
            case "$1" in
                -e|--enable|enable)   enable_factory_mode;;
                -d|--disable|disable) disable_factory_mode;;
                -s|--status|status)   get_factory_mode_status;;
                -h|--help|help)       usage;;
                (*)                   usage_error;;
            esac
            retval=$?
        fi
    else
        echo "Could not determine chip type...no action for factory mode"
        retval=1
    fi
    return $retval

}

#################################
# one call to main, and then done
#################################
main "$@"
exit $?
