#!/usr/bin/sh
#
# Output data that a real SCSI command would have.
# Read in from the counter file to determine which data file to output

AWK=/usr/bin/awk

# Get the command and device type 
COMMAND_TYPE=`echo $0 | ${AWK} -F"/" '{print $NF}'`
DEVICE_TYPE=`echo $3 | ${AWK} -F"/" '{print $3}'`

# Set the scsi model def and device type 
# NOTE: Use quotes because $DEVICE_TYPE could be empty
#       if the /dev/... convention is not used

if [ "$DEVICE_TYPE" = "ATL_452" ]; then

    DEVICE_TYPE=ATL_452
    SCSI_MODEL_DEF=ATL

elif [ "$DEVICE_TYPE" = "ATL_7100" ]; then

    DEVICE_TYPE=ATL_7100
    SCSI_MODEL_DEF=ATL

elif [ "$DEVICE_TYPE" = "ATL_2640" ]; then

    DEVICE_TYPE=ATL_2640
    SCSI_MODEL_DEF=ATL_2640

elif [ "$DEVICE_TYPE" = "ATL_L200" ]; then

    DEVICE_TYPE=ATL_L200
    SCSI_MODEL_DEF=ATL_L500

elif [ "$DEVICE_TYPE" = "ATL_L500" ]; then

    DEVICE_TYPE=ATL_L500
    SCSI_MODEL_DEF=ATL_L500

elif [ "$DEVICE_TYPE" = "ATL_P1000" ]; then

    DEVICE_TYPE=ATL_P1000
    SCSI_MODEL_DEF=ATL_P1000

elif [ "$DEVICE_TYPE" = "ATL_P3000" ]; then

    DEVICE_TYPE=ATL_P3000
    SCSI_MODEL_DEF=ATL_P1000

elif [ "$DEVICE_TYPE" = "EXB_220" ]; then

    DEVICE_TYPE=EXB_220
    SCSI_MODEL_DEF=EXB_220

elif [ "$DEVICE_TYPE" = "HP_DDS3" ]; then

    DEVICE_TYPE=HP_DDS3
    SCSI_MODEL_DEF=HP_C1557A_MC

elif [ "$DEVICE_TYPE" = "HP_418" ]; then

    DEVICE_TYPE=HP_418
    SCSI_MODEL_DEF=HP_C6280

else

    DEVICE_TYPE=ATL_452
    SCSI_MODEL_DEF=ATL
fi

# Run the proper command
COUNTER=`cat $1/simcounter.dat`
if [ -x "$1/$SCSI_MODEL_DEF/$DEVICE_TYPE/$COMMAND_TYPE$COUNTER.dat" ]; then
    RESULT=`cat  $1/$SCSI_MODEL_DEF/$DEVICE_TYPE/$COMMAND_TYPE$COUNTER.dat`
else
    DEFAULT_COUNT=0
    RESULT=`cat  $1/$SCSI_MODEL_DEF/$DEVICE_TYPE/$COMMAND_TYPE$DEFAULT_COUNT.dat`
fi

echo "$RESULT"
echo "$RESULT" | awk '$1 == "ERROR:Check" {exit 2}' -
if [ $? -eq 2 ]; then
   exit 2;
fi
echo "$RESULT" | awk '$1 == "ERROR:System" {exit 1}' -
if [ $? -eq 1 ]; then
   exit 1;
fi
