##
## Copyright 2013 Cumulus Networks Inc.
## All rights reserved
##
## Useful functions and variables for boot time.
##
## This assumes the shell is ash/dash.
##

## Import kernel's /proc/cmdline variables
export quiet=${quiet:=n}
export verbose=${verbose:=n}
export testing=${testing:=n}
import_kernel_cmdline()
{

    # Parse kernel command line options
    for x in $(cat /proc/cmdline); do
        parm=${x%%=*}
        val=${x#*=}
        case $parm in
	    quiet)
		quiet=y
		;;
	    verbose)
		verbose=y
		;;
	    testing)
		testing=y
		;;
	    cl_platform)
                cl_platform="$val"
		;;
	esac
    done

    if [ -n "$onie_exec_url" ] ; then
        # always run normal verbosity during provisioning
        quiet=n
    fi

}

# Detect the current platform.  This function sets the PLATFORM
# variable with the format <vendor>,<model>.
get_platform()
{
    [ -x $script_dir/cl-platform ] || {
        log_failure_msg "Unable to find cl-platform"
        bail_out
    }
    model=$($script_dir/cl-platform) || {
        log_failure_msg "Problems running $script_dir/cl-platform"
        bail_out
    }
    PLATFORM="$model"
}

# Then install platform specific files into well known locations.

init_platform_path()
{

    [ -e "${INIT_CONF}/running" ] && return 0

    arch=
    case $(uname -m) in
        ppc)
            arch=powerpc
            ;;
        x86_64)
            arch=amd64
            ;;
        armv7l)
            arch=arm
            ;;
        *)
            log_failure_msg "Unsupported architecture: $(uname -a)"
            bail_out
    esac

    echo "arch=$arch" > $INIT_CONF/arch.conf

    if [ "$arch" = "powerpc" ] ; then
        if [ -r /proc/device-tree/model ] ; then
            model=$(cat /proc/device-tree/model)
            if [ -z $model ] ; then
                log_failure_msg "Unable to determine platform from /proc/device-tree"
                bail_out
            fi
        else
            log_failure_msg "Unable to read /proc/device-tree/model"
            bail_out
        fi

        # remove spaces, translate "," into "_"
        if [ -r /proc/device-tree/model ] ; then
            plat=$(sed -e 's/ //' -e 's/,/_/' /proc/device-tree/model)
        fi
    elif [ "$arch" = "amd64" -o "$arch" = "arm" ] ; then
        # Look for $cl_platform -- either set on the kernel command when
        # running CL, or taken from the ONIE environment.
        if [ -z "$cl_platform" ] ; then
            if [ -x /bin/onie-sysinfo ] ; then
                # get cl_platform from ONIE
               cl_platform=$(onie-sysinfo -p)
                # Work around for Accton 5712-54x with bad name
                if [ "$cl_platform" = "x86_64-as5712_54x-r0" ] ; then
                    cl_platform="x86_64-accton_as5712_54x-r0"
                fi
            else
                log_failure_msg "Unable to determine platform for ARCH: $arch"
                bail_out
            fi
        fi
        # <arch>-<vendor>_<machine>-<revision>
        plat=${cl_platform#*-}
        plat=${plat%-*}
    fi

    [ -d "$INIT_CONF/$plat" ] || {
        log_failure_msg "Unable to find current platform's configuration directory: $INIT_CONF/$plat"
        bail_out
    }

    (cd $INIT_CONF ; ln -sf $plat running)
}

##
## Run command and log failures
##
cl_run()
{
    if [ "$verbose" = "y" ] ; then
        echo "Running command: $@" > /dev/stderr
    fi
    eval "$@" || {
        log_failure_msg "$@ failed."
        return 1
    }
}

##
## Look up a ubi volume name and return the /dev/ubiX_Y device
##
ubi_name_to_device()
{
    name=$1

    for dev in /sys/class/ubi/ubi0/ubi?_?; do
        ubiname=`cat $dev/name`
        if [ "$ubiname" = $name ]; then
            echo /dev/`basename $dev`
            return 0
        fi
    done

    return 1
}

##
## Look up a ubi volume name and return the major:minor device node info
##
ubi_name_to_block_node()
{
    name=$1

    for dev in /sys/class/ubi/ubi0/ubi0_?; do
        ubidev=`cat $dev/name`
        if [ "$ubiname" = $name ]; then
            cat $dev/dev
            return 0
        fi
    done

    return 1
}

##
## Look up a ubi volume name and return the ubi device and volume
##
ubi_name_to_dev_vol()
{
    name=$1

    for dev in /sys/class/ubi/ubi0/ubi0_?; do
        ubidev=`cat $dev/name`
        if [ "$ubiname" = $name ]; then
            echo 0 `echo $dev | tail -c 2`
            return 0
        fi
    done

    return 1
}

cl_panic()
{
    if [ -x /bin/rescue ] ; then
        # Legacy powerpc bail out method
        /bin/rescue 1
    else
        panic "$@"
    fi
}

# Local Variables:
# mode: shell-script
# eval: (sh-set-shell "/bin/sh" t nil)
# End:
