#!/bin/sh
#
# steps:
#   - create AtFS dirs in new directorys
#
argc=$#
ROOT=/usr3/sven/development/xf-doc
SAVING=$ROOT/tmp/Prot.save
RETRIEVING=$ROOT/tmp/Prot.retrv
TOPRELEASE=$ROOT/tmp/Prot.toprel
MYMAIL=mail
SLEEP=30

while [ $argc -gt 0 ]; do
        c=$1
        shift
        argc=`expr $argc - 1`
        case $c in
        "init")
		find . -depth -type d -exec makeit isok {} \; -exec makeit initit {} \; ;;
	"initit")
		echo "making AtFS directory in: $1"
		mkdir $1/AtFS 1>/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "could not make AtFS directory in: $1"
			exit 2
		fi
                if [ $argc -gt 0 ]; then
                        shift
                        argc=`expr $argc - 1`
                fi;;
        "remove")
		find . -depth -type d -exec makeit isok {} \; -exec makeit removeit {} \; ;;
	"removeit")
		echo "removing AtFS directory in: $1"
		rm -rf $1/AtFS 1>/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "could not remove AtFS directory in: $1"
			exit 2
		fi
                if [ $argc -gt 0 ]; then
                        shift
                        argc=`expr $argc - 1`
                fi;;
	"save")
		find . -depth -type d -exec makeit isok {} \; -exec makeit saveit {} \; ;;
	"saveit")
		echo "saving in: $1"
		cd $1
		vadm -lock -q * 1>>$SAVING 2>&1
		save -q * 1>>$SAVING 2>&1
		if [ 0 -ne 0 ]; then
			number=`ls -p | grep -v / | wc -w`
			if [ $number -ne 0 ]; then
				echo "could not make save in: $1"
				exit 2
			fi
		fi
                if [ $argc -gt 0 ]; then
                        shift
                        argc=`expr $argc - 1`
                fi;;
	"retrv")
		find . -depth -type d -exec makeit isok {} \; -exec makeit retrvit {} \; ;;
	"retrvit")
		echo "retrieving in: $1"
		cd $1
		retrv -q -lock `shape -echo COMPONENTS` 1>>$RETRIEVING 2>&1
		if [ $? -ne 0 ]; then
			echo "could not make retrv in: $1"
			exit 2
		fi
                if [ $argc -gt 0 ]; then
                        shift
                        argc=`expr $argc - 1`
                fi;;
	"release")
		find . -depth -type d -exec makeit isok {} \; -exec makeit releaseit {} \; ;;
	"releaseit")
		echo "prereleasing in: $1"
		cd $1
		shape prerelease 1>>$TOPRELEASE 2>&1
		if [ $? -ne 0 ]; then
			echo "could not prerelease in: $1"
			exit 2
		fi
		echo "releasing in: $1"
		shape release 1>>$TOPRELEASE 2>&1
		if [ $? -ne 0 ]; then
			echo "could not release in: $1"
			exit 2
		fi
		if [ $argc -gt 0 ]; then
	         	shift
			argc=`expr $argc - 1`
                fi;;
        "prerelease")
                find . -depth -type d -exec makeit isok {} \; -exec makeit prereleaseit {} \; ;;
        "prereleaseit")
                echo "prereleasing in: $1"
                cd $1
                shape prerelease 1>>$TOPRELEASE 2>&1
                if [ $? -ne 0 ]; then
                        echo "could not prerelease in: $1"
                        exit 2
                fi
                if [ $argc -gt 0 ]; then
                	shift
                        argc=`expr $argc - 1`
                fi;;
	"isok")
		name=`basename $1`
		if [ $1 = ./tmp ]; then
			exit 255
		fi
		if [ $name = AtFS ]; then
			exit 255
		fi
		if [ $name = Attr ]; then
			exit 255
		fi
		if [ $name = Data ]; then
			exit 255
		fi
		if [ $name = Lock ]; then
			exit 255
		fi
                exit 0;;
        *)
		echo "specify one of the following:"
		echo "   init"
		echo "   remove"
		echo "   save"
		echo "   retrv"
		echo "   release"
		echo "usage:"
		echo "   makeit init"
		echo "   makeit save"
		echo "   makeit release"
              	exit 2
        esac
done

