#!/bin/bash
#!/usr/bin/ksh
# 17:39:04 @(#)common2	141.3 03/07/29
#
#########################################################################
#
# some basic functions used below
#
# fail and Error will accept a numeric first argument as the exit code
# otherwise 1 is used.

#
# Display error message and abort if $REALQUIET

Error() # ({ExitCode} Msg)
{
    # If first argument is numeric use that as exit code, else use 1

    ErrorCode=1
    [[ "$1" = [0-9]* ]] && ErrorCode=$1 && shift
    echo `basename $0`: $*
    [ $REALQUIET ] && echo "Aborting" && exit $ErrorCode
}

fail() # ({ExitCode} Msg)
{
    P1=$1;shift
    Error "$P1" $*
    exit $ErrorCode
}

#
# Prompt and display default, $3, if supplied, input response.
# Avoid user input when $REALQUIET, use AutoReply if non-blank, else Default

Input() # Prompt {AutoReply {Default}}
{
    printf "$1 "
    [ "$3" != "" ] && printf "[$3] "
    printf ": "

    if [ "$REALQUIET" != "" ]
    then
        REPLY=$2
        [ "$REPLY" = "" ] && REPLY=$3
	echo $REPLY
	[ "$REPLY" = "" ] && fail "User input required"
    else
        read REPLY
	[ "$REPLY" = "" ] && REPLY=$3
    fi
}

Query() # (Prompt {AutoReply {Default}})
{
    Default="n"
    [ "$3" != "" ] && Default=$3

    Input "$1" "$2" "$Default"
    case $REPLY in
	[yY])	return 0 ;;
	*)	return 1 ;;
    esac
}

Select()
{
    [ $REALQUIET ] && fail "Select: no user input"
    Name=`expr "                                        $1" : '.*\(..............................\)'`
    Input "$Name" n

    case $REPLY in
	y|Y)	return 0;;
	q|Q)	return 2;;
	*)	return 1;;
    esac
}


#########################################################################
#
# Load in a cpio file, zipped or otherwise
#
Cpio()
{
    if [ -r $1/cpio ]
    then
	cpio $CpioOpts < $1/cpio || fail "Failed to unpack $1/cpio"

    elif [ -r $1/cpio.z ]
    then
	$gzip -d < $1/cpio.z | cpio $CpioOpts || fail "Failed to unpack $1/cpio.z"

    else
        fail "Cannot find cpio file in $1"
    fi
}
