#!/bin/bash
#
# Designed to be called from mhvtl rc script
#
# * Copyright (C) 2005 Mark Harvey markh794@gmail.com
# *                                mark_harvey@symantec.com
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

export PATH=/usr/bin:$PATH

if [ $# != 1 ]; then
	echo "Usage: $0 username"
	exit 1
fi

if [ X$MHVTL_CONFIG_PATH == "X" ]; then
	. /etc/mhvtl/mhvtl.conf
else
	. $MHVTL_CONFIG_PATH/mhvtl.conf
fi

USER=$1
TMP=/var/tmp/_build.$$
DEVICE_CONF=$MHVTL_CONFIG_PATH/device.conf
OPTIONS=""

# rc script will make sure this exists...
. $MHVTL_CONFIG_PATH/mhvtl.conf

if [ $VERBOSE -gt 2 ];then
	OPTIONS="-v -v -v"
elif [ $VERBOSE -gt 1 ];then
	OPTIONS="-v -v"
elif [ $VERBOSE -gt 0 ];then
	OPTIONS="-v"
else
	OPTIONS=""
fi

# FIFO="-f /tmp/mhvtl"

if [ ! -f $DEVICE_CONF ]; then
	echo "Sorry, Can't continue - no $DEVICE_CONF"
	exit 1
fi

# Test for new interface to dynamically add logical units.
if [ ! -f /sys/bus/pseudo/drivers/mhvtl/add_lu ]; then
	echo "Sorry, Can't continue - No kernel module loaded or out of date ??"
	echo "Please visit http://sites.google.com/site/linuxvtl2/"
	exit 1
fi

# First we need to setup & configure drives.

for a in $(awk '$1 == "Drive:" {print $2}' $DEVICE_CONF)
do
	vtltape -q $a $OPTIONS $FIFO
done

# Now we need to setup & configure libraries.

for a in $(awk '$1 == "Library:" {print $2}' $DEVICE_CONF)
do
	vtllibrary -q $a $OPTIONS $FIFO
done

rm -f $TMP
