#! /bin/bash
# Usage ./copydcss <dcssname>

DEBUGON="FALSE"
ERR_BLK_UNAVAIL=2
ERR_NODE_CREATE=3
ERR_DCSS_ADD=4
ERR_DCSS_UNSHARE=5
ERR_DCSS_EXT2=6
ERR_DCSS_MNT=7
ERR_DCSS_COPY=8
ERR_DCSS_SAVE=9
ERR_DCSS_UMNT=10
ERR_DCSS_REMOVE=11

# Echo the parameters if DEBUGON is true
debugecho() {
	if [ "$DEBUGON" = "TRUE" ]; then
		echo $1
	fi
}

# Exit with an Error
#    Use only after DCSS has been loaded
exitwitherror() {
	if [ $# = 3 ]
	then
		debugecho "Must first umount"
		umount /mnt
	fi

	echo $1 > /sys/devices/dcssblk/remove	
	debugecho "Removed the DCSS"
	exit $2
} 

# Check Script Usage
if [ $# = 0 ]
then
	debugecho "Usage: ./copydcss <dcssname>"
	exit 1
fi

DMOTEMP="dmotemp"
DCSSNAME=$1
device="firstpass"

# Load the DCSS Block Device Driver 
modprobe dcssblk

# See if the DCSSBLK device exists
result=`grep dcssblk /proc/devices`

if [ $? -ne 0 ]; then
	debugecho "DCSS Block device driver is unavailable. "
        exit $ERR_BLK_UNAVAIL
fi

# Get the DCSSBLK Major number
major=`echo $result | (read field1 field2; echo $field1)`
device=`echo $result | (read field1 field2; echo $field2)`

debugecho "$device : $major"
debugecho "DCSSBLK major Number: $major"

# Check if device nodes exist for the DCSS Block Device
NODES=4
for ((ctr=0; ctr < NODES ; ctr++))
do
	if [ -e "/dev/dcssblk$ctr" ]; then
		debugecho "/dev/dcssblk$ctr exists - do nothing"
	else
		mknod /dev/dcssblk$ctr b $major 0
		if [ $? -ne 0 ]; then
			debugecho "Could not create node /dev/dcssblk$ctr. Exiting."
			exit $ERR_NODE_CREATE
		else
			debugecho "Created node /dev/dcssblk$ctr"
		fi
	fi
done

# Load the DCSS
echo "$DCSSNAME" > /sys/devices/dcssblk/add

# Check for errors
if [ $? -ne 0 ]; then
	debugecho "Error adding $DCSSNAME"
	exitwitherror $DCSSNAME $ERR_DCSS_ADD
else
	debugecho "$DCSSNAME added successfully"
fi

# Find the node used for the DCSS
blockline=`ls -l /sys/devices/dcssblk/$DCSSNAME/block` 
# line is f1	    f2 f3   f4   f5 f6 f7 f8    f9	                             f10 f11
#         lrwxrwxrwx 1 root root 0 Aug 16 17:16 /sys/devices/dcssblk/$DCSSNAME/block -> ../../../block/$node

var11=`echo $blockline | (read f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11; echo $f11)`
node=`basename $var11`
debugecho "NODE: $node"

# Create an ext2 file system on the block device
mke2fs  -qb 4096 /dev/$node

# Check for errors
if [ $? -ne 0 ]; then
	debugecho "Error creating file system for $DCSSNAME"
	exitwitherror $DCSSNAME $ERR_DCSS_EXT2
else
	debugecho "$DCSSNAME file system created successfully"
fi

# Mount the file system in the DCSS 
if [ -e "/mnt" ]; then
	# Do nothing
	debugecho "/mnt already exists"
else
	debugecho "creating /mnt"
	mkdir /mnt
fi

mount /dev/$node /mnt

# Check for errors
if [ $? -ne 0 ]; then
	debugecho "Error mounting file system for $DCSSNAME"
	exitwitherror $DCSSNAME $ERR_DCSS_MNT "umount"
else
	debugecho "$DCSSNAME mounted successfully"
fi

# Copy the temp directory on to this DCSS
cp -a $DMOTEMP/* /mnt

# Flush Cache to disk
sync

# Check for errors
if [ $? -ne 0 ]; then
	debugecho "Error copying files for $DCSSNAME"
	exitwitherror $DCSSNAME $ERR_DCSS_COPY "umount"
else
	debugecho "$DCSSNAME copied successfully"
fi

# If we got here everything worked
rc=0

# Return
exit $rc
