#! /bin/sh -
#
# $Header: /usr/local/share/RCS/makedisks.main,v 1.6 2005/08/11 02:10:06 raster Exp $
#
# Make the "standard" disk files for the simulator. This version
# succeeds the previous version, know as makedisks, and the two create
# files which are incompatible.  
#
# By default, will create 5 disks each with 16MB of usable space.  A
# -n <num> option may be provided to add <num> disks rather than the
# default, and a -t <type> options may be used to select the type of
# disk to be added, instead of the default. The types are listed below.
#
# Each time this script is run, the given number of disks will be
# added, each being assigned a different "device ID" and a unique
# serial number. A maximum of 56 disks can be created.
#
# This script also creates shelf files which contain names of disks.
# These allow simulation of the SCSI Enclosure Services behavior of
# a type of shelf. Shelf files are prefixed with "Shelf:".
# Multiple shelf files may be created; by default a single file is
# created that enables simulation of a DiskShelf14 shelf.
#

PATH=/bin:/usr/bin:/usr/local/bin

Usage()
{
#	echo "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
	echo "Usage: $0 [ -n <additional number of disks to create> ] "
	echo "          [ -t <disk type ID> ]"	
	echo "          [ -e <additional sectors> ]"
	echo "          [ -a <adapter on which to start populating disks ]"
	echo "          [ -h ]"
	echo ""
	echo "By default 5 disks will be added. The <disk type ID> can be one "
	echo "of the following: (NOTE, 0 is the default)"
	echo ""
	echo "  Type ID   VID      PID              Usable Size    Actual Size     Fast Zero?	Sector Size"
	echo "      0     NETAPP__ VD-16MB_________   16,777,216 B   38,273,024 B     No	512"
	echo "      1     NETAPP__ VD-35MB_________   35,913,728 B   57,409,536 B     No	512"
	echo "      2     NETAPP__ VD-50MB_________   52,428,800 B   73,924,608 B     No	512"
	echo "      3     NETAPP__ VD-100MB________  104,857,600 B  126,353,408 B     No	512"
	echo "      4     NETAPP__ VD-500MB________  524,288,000 B  545,783,808 B     No	512"
	echo "      5     NETAPP__ VD-1000MB_______ 1048,576,000 B 1070,071,808 B     No	512"
	echo "      6     NETAPP__ VD-16MB-FZ______   16,777,216 B   38,273,024 B     Yes	512"
	echo "      7     NETAPP__ VD-35MB-FZ______   35,913,728 B   57,409,536 B     Yes	512"
	echo "      8     NETAPP__ VD-50MB-FZ______   52,428,800 B   73,924,608 B     Yes	512"
	echo "      9     NETAPP__ VD-100MB-FZ_____  104,857,600 B  126,353,408 B     Yes	512"
	echo "     10     NETAPP__ VD-500MB-FZ_____  524,288,000 B  545,783,808 B     Yes	512"
	echo "     11     NETAPP__ VD-1000MB-FZ____ 1048,576,000 B 1070,071,808 B     Yes	512"
	echo "     12     NETAPP__ VD-16MB-520_____   16,777,216 B   38,273,024 B     No	520"
	echo "     13     NETAPP__ VD-35MB-520_____   35,913,728 B   57,409,536 B     No	520"
	echo "     14     NETAPP__ VD-50MB-520_____   52,428,800 B   73,924,608 B     No	520"
	echo "     15     NETAPP__ VD-100MB-520____  104,857,600 B  126,353,408 B     No	520"
	echo "     16     NETAPP__ VD-500MB-520____  524,288,000 B  545,783,808 B     No	520"
	echo "     17     NETAPP__ VD-1000MB-520___ 1048,576,000 B 1070,071,808 B     No	520"
	echo "     18     NETAPP__ VD-16MB-FZ-520__   16,777,216 B   38,273,024 B     Yes	520"
	echo "     19     NETAPP__ VD-35MB-FZ-520__   35,913,728 B   57,409,536 B     Yes	520"
	echo "     20     NETAPP__ VD-50MB-FZ-520__   52,428,800 B   73,924,608 B     Yes	520"
	echo "     21     NETAPP__ VD-100MB-FZ-520_  104,857,600 B  126,353,408 B     Yes	520"
	echo "     22     NETAPP__ VD-500MB-FZ-520_  524,288,000 B  545,783,808 B     Yes	520"
	echo "     23     NETAPP__ VD-1000MB-FZ-520 1048,576,000 B 1070,071,808 B     Yes	520"
	echo "     24     NETAPP__ VD-16MB-FZ-ATA__   16,777,216 B   51,388,416 B     Yes	512"
	echo "     25     NETAPP__ VD-35MB-FZ-ATA__   36,700,160 B   73,801,728 B     Yes	512"
	echo "     26     NETAPP__ VD-50MB-FZ-ATA__   52,428,800 B   91,496,448 B     Yes	512"
	echo "     27     NETAPP__ VD-100MB-FZ-ATA_  104,857,600 B  150,478,848 B     Yes	512"
	echo "     28     NETAPP__ VD-500MB-FZ-ATA_  524,288,000 B  622,338,048 B     Yes	512"
	echo "     29     NETAPP__ VD-1000MB-FZ-ATA 1048,576,000 B 1212,162,048 B     Yes	512"
}

GetDiskVIDPID()
{
    case "$1" in
    0)
	echo "NETAPP__:VD-16MB_________"
	;;
    1)
	echo "NETAPP__:VD-35MB_________"
	;;
    2)
	echo "NETAPP__:VD-50MB_________"
	;;
    3)
	echo "NETAPP__:VD-100MB________"    
	;;
    4)
	echo "NETAPP__:VD-500MB________"    
	;;
    5)
	echo "NETAPP__:VD-1000MB_______"    
	;;


    6)
	echo "NETAPP__:VD-16MB-FZ______"
	;;
    7)
	echo "NETAPP__:VD-35MB-FZ______"
	;;
    8)
	echo "NETAPP__:VD-50MB-FZ______"
	;;
    9)
	echo "NETAPP__:VD-100MB-FZ_____"    
	;;
    10)
	echo "NETAPP__:VD-500MB-FZ_____"    
	;;
    11)
	echo "NETAPP__:VD-1000MB-FZ____"    
	;;


    12)
	echo "NETAPP__:VD-16MB-520_____"
	;;
    13)
	echo "NETAPP__:VD-35MB-520_____"
	;;
    14)
	echo "NETAPP__:VD-50MB-520_____"
	;;
    15)
	echo "NETAPP__:VD-100MB-520____"    
	;;
    16)
	echo "NETAPP__:VD-500MB-520____"    
	;;
    17)
	echo "NETAPP__:VD-1000MB-520___"    
	;;


    18)
	echo "NETAPP__:VD-16MB-FZ-520__"
	;;
    19)
	echo "NETAPP__:VD-35MB-FZ-520__"
	;;
    20)
	echo "NETAPP__:VD-50MB-FZ-520__"
	;;
    21)
	echo "NETAPP__:VD-100MB-FZ-520_"    
	;;
    22)
	echo "NETAPP__:VD-500MB-FZ-520_"    
	;;
    23)
	echo "NETAPP__:VD-1000MB-FZ-520"    
	;;

    24)
	echo "NETAPP__:VD-16MB-FZ-ATA__"
	;;
    25)
	echo "NETAPP__:VD-35MB-FZ-ATA__"
	;;
    26)
	echo "NETAPP__:VD-50MB-FZ-ATA__"
	;;
    27)
	echo "NETAPP__:VD-100MB-FZ-ATA_"
	;;
    28)
	echo "NETAPP__:VD-500MB-FZ-ATA_"
	;;
    29)
	echo "NETAPP__:VD-1000MB-FZ-ATA"
	;;

    esac
}

GetDiskCapacity()
{
    # Every disk type has a base capacity and 14464 sectors (7.06MB) are added 
    # to support new RAID, plus the 41984 sectors (20.5MB) at the front of the disk for other
    # items (old labels, boot block, kernel).
    case "$1" in
    0|6|12|18)
	# 16 MB usable 
	echo "89216"
	;;
    1|7|13|19)
	# 35 MB usable
	echo "126592"
	;;
    2|8|14|20)
	# 50 MB usable
	echo "158848"
	;;
    3|9|15|21)
	# 100 MB usable
	echo "261248"
	;;
    4|10|16|22)
	# 500 MB usable
	echo "1080448"
	;;
    5|11|17|23)
	# 1 GB (1000 MB) usable
	echo "2104448"
	;;

    24)
	# 16 MB usable
	echo "100368"
	;;
    25)
	# 35 MB usable
	echo "144144"
	;;
    26)
	# 50 MB usable
	echo "178704"
	;;
    27)
	# 100 MB usable
	echo "293904"
	;;
    28)
	# 500 MB usable
	echo "1215504"
	;;
    29)
	# 1 GB (1000 MB) usable
	echo "2367504"
	;;

    esac
}

GetDiskSerialNo()
{
    #
    # We try to generate a 8 digit serial number ... hence
    # the bizarre gyrations. The lower 2 digits will be provided
    # by the caller.
    #
    a=`date +%M%S`
    b=$$
    base=`expr \( $a % 1000000 \) + \( \( $b \* 100 \) % 1000000 \)`
    base=`expr $base \* 100 % 100000000`
    if [ $base -lt 10000000 ]; then
	base=`expr $base + 10000000`
    fi
    sn=`expr $base + $1`
    echo "$sn"
}

CheckEnclosureName()
{
    #
    # Check the name of the simulator shelf file
    #
	case $1 in
		DiskShelf14) echo $1 ;;
		*) echo "Error" ;;
	esac
}

MAXTYPES=30
DISKDIR=",disks"

#
# Set default values
#
TYPE=0
NUMDISKS=5
ADDITIONAL=0
FIRST_HA=0
SHELF="DiskShelf14"

while [ -n "$1" ]; do
	case "$1" in
	-t)
		if [ -z "$2" ]; then
			Usage
			exit 1
		fi
		TYPE=$2
		shift
		;;

	-n)
		if [ -z "$2" ]; then
			Usage
			exit 1
		fi
		NUMDISKS=$2
		shift
		;;

	-e)
		if [ -z "$2" ]; then
			Usage
			exit 1
		fi
		ADDITIONAL=$2
		shift
		;;

	-a)
		if [ -z "$2" ]; then
			Usage
			exit 1
		fi
		if [ $2 -ge 4 ]; then
			echo "$0: Adapter must be in the range 0 thru 3."
			exit 1
		fi
		FIRST_HA=$2
		shift
		;;

	-s)
		SHELF=`CheckEnclosureName $2`
		if [ $SHELF = "Error" ]
			then echo "$2 is NOT a recognized disk shelf type"
			exit 1
		fi
		shift
		;;

	-h|-x|-?)
		Usage
		exit 0
		;;

	*)
		echo "Unknown option $1"
		exit 1
		;;
	esac
	shift
done

if [ ! -d "$DISKDIR" ]; then
    echo "$0: Cannot open directory $DISKDIR"
    exit 1
fi

if [ $TYPE -ge $MAXTYPES ]; then
    echo "$0: Invalid type $TYPE"
    Usage
    exit 1
fi

MAXDISKS=0
ha=$FIRST_HA
while [ $ha -lt 4 ]; do
    for id in 16 17 18 19 20 21 22 \
               24 25 26 27 28 29 \
               32 33 34 35 36 37 38 \
	       39 40 41 42 43 44 45 \
	       48 49 50 51; do

	diskname=`GetDiskVIDPID $TYPE`
	capacity=`GetDiskCapacity $TYPE`
	capacity=`expr $capacity + $ADDITIONAL`
	sn=`GetDiskSerialNo $MAXDISKS`

	# 
	# Does the file already exist? If not, then create it, else
	# try the next name
	#
	ls $DISKDIR | grep "v$ha.$id" > /dev/null
	if [ $? -ne 0 ]; then
	    DISKFILE="$DISKDIR/v$ha.$id:$diskname:$sn:$capacity"
            touch $DISKFILE
            chmod +w $DISKFILE
            makespare $DISKFILE
            echo "Creating $DISKFILE"
	    echo "v$ha.$id:$diskname:$sn:$capacity" >> $DISKDIR/Shelf:$SHELF
	    NUMDISKS=`expr $NUMDISKS - 1`
	    if [ $NUMDISKS -eq 0 ]; then
		echo "Shelf file Shelf:$SHELF updated"
		exit 0
	    fi
	fi

	MAXDISKS=`expr $MAXDISKS + 1`

    done
    ha=`expr $ha + 1`
done

if [ $NUMDISKS -ne 0 ]; then
    echo "$0: More than $MAXDISKS disks already exist in directory $DISKDIR."
    exit 1
fi

exit 0
