#!/bin/sh
#
# Program: S99fbr
# Current Revision: 1.1
# Last Modification Date: 03/09/08
# Last Modified by: %N%
#
# Copyright 2002,2003 Sun Microsystems, Inc.  All Rights Reserved
# Use is subject to license terms.
#
# This script caches the saved personality of the Service Processor if it
# isn't already there.  This is a startup script for run level 3.
#
CACHE_DIR=/opt/SUNWsefbru/files
CACHE_FILE=SP_Personality.tar.Z
FLASH_DIR=/flash
#
# Check for a cached personality file
#
if ( test ! -s $CACHE_DIR/$CACHE_FILE ); then
#
# If no saved personality, mount USB Flash Disk
# Copy SP_Personality.tar.Z to cache area and extract
#
    mkdir -p $FLASH_DIR
#
# Check to see if USB disk is in the device tree
#
  for i in `ls /dev/rdsk`; do
    DEV=`echo $i`
    if ( test "$DEV" != "" ); then
      if ( test `echo $i | grep -c "s2"` -gt 0 ); then
        RDEV=/dev/rdsk/${i}  # Raw Drive for Inquiry
        FDEV=/dev/dsk/${i}   # Formatted Drive for mount
        PHYS_PATH_LINK=`ls -l $RDEV | grep usb` 
        if ( test "$PHYS_PATH_LINK" !=  "" ); then
          FLASHDRIVE_FOUND=TRUE
          break
        fi
      fi
    fi
  done
  if ( test "$FLASHDRIVE_FOUND" = "TRUE" ); then
    STAT=`fsck -m $RDEV >/dev/null 2>&1; echo $?`
    if ( test $STAT -eq 0 ); then
      STAT=`/usr/sbin/mount -F ufs $FDEV $FLASH_DIR; echo $?`
      if ( test $STAT -eq 0 ); then
        if ( test -s $FLASH_DIR/$CACHE_FILE ); then
          cp $FLASH_DIR/$CACHE_FILE $CACHE_DIR/$CACHE_FILE
          zcat $CACHE_DIR/$CACHE_FILE | tar -xpf - > /dev/null 2>&1
        else
          echo "SE6x20: No personality file on Flash Disk.  No personality restored"
          exit 1
        fi
      else
        echo "SE6x20: USB Flash Disk mount error.  No personality restored"
        exit 1
      fi
    else
      echo "SE6x20: USB Flash Disk not suitable for mounting.  No personality restored"
      exit 1
    fi
  else
    echo "SE6x20: No USB Flash Disk device entry. No personality restored."
    exit 1
  fi
fi
