#!/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 [ $# -ne 1 ]; then
	echo "Usage: $0 username"
	exit 1
fi

MHVTL_HOME_PATH=/opt/mhvtl

# Source default config file if not already set
if [ "X$MHVTL_CONFIG_PATH" == "X" ]; then
	. /etc/mhvtl/mhvtl.conf
else
	. $MHVTL_CONFIG_PATH/mhvtl.conf
fi

USER=${1:-vtl}	# Default user 'vtl'

#dry='echo'

# Test for 'runuser' used on Scientific Linux, else default with 'su'
if [ -x "$(which runuser >/dev/null 2>&1)" ]; then
	RUNAS=runuser
else
	RUNAS=su
fi

run() {
	if [ -x "$(which setuidgid >/dev/null 2>&1)" ]; then
		$dry setuidgid $USER $*
	else
		$dry $RUNAS $USER -c "$*"
	fi
}

# I'm sure there is a better method then this...
# There is.. Thanks to Gavin Barnard.
set_density() {
	density=$1
	regex='[A-Z0-9]{6,8}([SLXTJ])([123456ABWX])$'
	#regex='[A-Z0-9]{4,6}([SLXTJ])([123456ABWX])[0-9]*$' # for bacula

	if [[ $density =~ $regex ]]; then
		matches=${#BASH_REMATCH[*]}
		if [ ${BASH_REMATCH[1]} = 'L' ]; then
			DENSITY=`echo -e "LTO${BASH_REMATCH[2]}"`
		fi
		if [ ${BASH_REMATCH[1]} = 'S' ]; then
			DENSITY=`echo -e "SDLT${BASH_REMATCH[2]}"`
		fi
		if [ ${BASH_REMATCH[1]} = 'X' ]; then
			DENSITY=`echo -e "AIT${BASH_REMATCH[2]}"`
		fi
		if [ ${BASH_REMATCH[1]} = 'T' ]; then
			DENSITY=`echo -e "T10K${BASH_REMATCH[2]}"`
		fi
		if [ ${BASH_REMATCH[1]} = 'J' ]; then
#FIXME: please fix me when ever the J WORM TAPE are added to mktape
			DENSITY=`echo -e "unknown tape density"`
		fi
	fi
}

##################################################################
## Main starts here...
##################################################################

# Set default capacity to 500M if not defined.
CAPACITY=${CAPACITY:=500}

if [ ! -d $MHVTL_HOME_PATH ]; then
	mkdir -p $MHVTL_HOME_PATH
	chmod 750 $MHVTL_HOME_PATH
	chown $USER:$USER $MHVTL_HOME_PATH
fi

# Create any media specified in library config.
umask 002

for LIBCONTENTS in $MHVTL_CONFIG_PATH/library_contents.*
do
	# Cleaning carts
	for a in `cat $LIBCONTENTS|awk '/^Slot.*CLN.+/ {print $3}'|sort -u`
	do
		set_density $a
		if [ ! -d $MHVTL_HOME_PATH/$a ]; then
			run "mktape -s $CAPACITY -t clean -m $a -d $DENSITY"
		fi
	done

	# WORM media
	for a in `cat $LIBCONTENTS|awk '/^Slot/ {print $3}'|grep ^W|sort -u`
	do
		set_density $a
		if [ ! -d $MHVTL_HOME_PATH/$a ]; then
			run "mktape -s $CAPACITY -t WORM -m $a -d $DENSITY"
		fi
	done

	# Rest must be Data
	for a in `cat $LIBCONTENTS|awk '/^Slot/ {print $3}'|sort -u`
	do
		set_density $a
		if [ ! -d $MHVTL_HOME_PATH/$a ]; then
			run "mktape -s $CAPACITY -t data -m $a -d $DENSITY"
		fi
	done
done
