#!/usr/bin/ksh
#
#       Script to recover the NVlog from the losing CPUset
#       after an out-of-sync event
#
#       Arguments:
#               None
#
#       Files:
#               The salvaged logs are saved in the current directory,
#               in files named "u4ftlog.[AB][01].salvage.$$".
#               The names of these files are listed on stdout, if
#               any are successfully salvaged.
#
#               The script also (transiently) creates a character
#               special file called "u4ftlog:salvage.$$,nodelay"
#
#       Assumptions:
#               That the programs named in $SCRIPTS have been installed
#               (somewhere)
#

DRIVER=u4ftlog
MYIDENT=salvage.$$
SCRIPTS='cmsCreateDevice|u4ftctl|u4ftdev_match|u4ftloc_get_parameter'

#
#       The SCRIPTS must be put on the PATH ...
#
export PATH=$(  pkgchk -l |
                egrep "/($SCRIPTS)\$" |
                sed -n -e 's!^.*: \(/.*\)/[^/]*$!\1!p' |
                sort -u |
                tr '\012' ':' )$PATH

for side in A B
do
    for bus in 0 1
    do
        outputFile="$DRIVER.$side$bus.$MYIDENT"
        slotCookie=$(u4ftdev_match CLASS=slot SUB=$side-CPU$bus)
        if [[ $? -eq 0 ]]
        then
            if [ "$(u4ftctl get_state $slotCookie)" != "online" ]
            then
                u4ftctl offline $slotCookie "$DRIVER $MYIDENT"
                u4ftctl online $slotCookie "$DRIVER $MYIDENT"
            fi

            cmsCreateDevice $slotCookie '()' "$MYIDENT" - <<- !!
                compatible = "$DRIVER";
                node = "$MYIDENT";
                no-config = 1;
                non-prom = 1;
                readonly = 1;
                reg = $(u4ftloc_get_parameter DEV_REG CLASS=salvage     \
                                                      SIDE=$side        \
                                                      BUS=$bus);
		!!

            logCookie=$(u4ftctl find $slotCookie "$MYIDENT")
            if [[ $? -eq 0 && -n "$logCookie" ]]
            then
                u4ftctl online $logCookie "$DRIVER $MYIDENT"
                u4ftctl get_minors $logCookie | 
                while read stype major minor name ntype
                do
                    case "$stype-$name" in
                    c-$MYIDENT,nodelay)
                        rm -f $DRIVER:$name
                        mknod $DRIVER:$name $stype ${major%,} $minor
                        cat $DRIVER:$name > $outputFile
                        rm $DRIVER:$name
                        [ -s $outputFile ] || rm -f $outputFile
                        ;;
                    esac
                done
                u4ftctl delete $logCookie
            fi
        fi
    done
done

ls $DRIVER.??.$MYIDENT 2> /dev/null
