#!/bin/sh
# arkmigrate1-52-53 : Arkeia Migration Procedure from v5.2 to v5.3
# Copyright Arkeia Corporation 2004


VERSION="5.3.10"
CP="cp -f"
MV="mv -f"
RM="rm -f"
RMREC="rm -rf"
TAR="tar"
OSTYPE=`uname`


path52=''


usage() {
	$ECHO "Arkeia Migration Procedure From v5.2 to v5.3 Version $VERSION"
	$ECHO "Usage:"
	$ECHO "  arkmigrate-52-53 --path [VALUE]"
	$ECHO "    --path : the arkeia 5 root directory (ex : /opt/arkeia)"
	$ECHO ""
	exit 0
}


check_error() {
	lasterr=$?
	if [ $lasterr != 0 ] ; then
		$ECHO "Bad end"
		exit $lasterr
	fi
}


init_os_variables() {
	if [ $OSTYPE = "Linux" ]
	then
		ECHO="/bin/echo"
		ECHOE="/bin/echo -e"
		CPREC="cp -pR"
		if [ -x /usr/bin/ex ] ; then
			EX="/usr/bin/ex"
		else
			EX="/bin/ex"
		fi
		if [ -x /usr/bin/grep ] ; then
			GREP="/usr/bin/grep"
		else
			GREP="/bin/grep"
		fi
	elif [ $OSTYPE = "SunOS" ]
	then
		ECHO="/bin/echo"
		ECHOE="/bin/echo"
		CPREC="cp -pR"
		EX="/usr/xpg4/bin/ex"
		GREP="/usr/xpg4/bin/grep"
	elif [ $OSTYPE = "AIX" ]
	then
		ECHO="/usr/bin/echo"
		ECHOE="/usr/bin/echo"
		CPREC="cp -phR"
		EX="/usr/bin/ex"
		GREP="/usr/bin/grep"
	elif [ $OSTYPE = "IRIX" -o $OSTYPE = "IRIX64" ]
	then
		ECHO="/sbin/echo"
		ECHOE="/sbin/echo"
		CPREC="cp -pR"
		EX="/usr/bin/ex"
		GREP="/sbin/grep"
	elif [ $OSTYPE = "HP-UX" ]
	then
		ECHO="/usr/bin/echo"
		ECHOE="/usr/bin/echo"
		CPREC="cp -pR"
		EX="/usr/bin/ex"
		GREP="/usr/bin/grep"
	elif [ $OSTYPE = "OSF1" ]
	then
		ECHO="/usr/bin/echo"
		ECHOE="/usr/bin/echo"
		CPREC="cp -pR"
		EX="/usr/bin/ex"
		GREP="/sbin/grep"
	else
		$ECHO "$OSTYPE not yet supported!"
		exit 1
	fi
	if [ ! -f $ECHO ] ; then
		echo "$ECHO does not exist on your system"
		exit 1
	fi
	if [ ! -f $EX ] ; then
		echo "$EX does not exist on your system"
		exit 1
	fi
	if [ ! -f $GREP ] ; then
		echo "$GREP does not exist on your system"
		exit 1
	fi
}


# 3 parameters
# $1 : the name of the property to modify
# $2 : the new value of the property
# $3 : the file path
modify_prop() {
	$ECHOE "/^[[:space:]]*$1[[:space:]]*\".*\"\n.s/\".*\"/\"$2\"/\nwq\n" | $EX -s $3 > /dev/null
	check_error
}


# 3 parameters
# $1 : arkeia 5.2 file tape type name
# $2 : corresponding tape capacity (in MB)
# $3 : corresponding segment size
set_new_file_tape_type() {
	for str in `find $path52/server/dbase/f3tape -print | $GREP ".*/tp[^/]*/tp[^/]*\.lst"`; do
		if $GREP "^[[:space:]]*TP_TYPE[[:space:]]*\"$1\"" $str > /dev/null; then
			modify_prop "TP_TYPE" "FILE" "$str"

			# add CAPACITY and SIZESEG properties
			$ECHOE "1i\nCAPACITY	\"$2\"\nSIZESEG	\"$3\"\n.\nwq\n" | $EX -s $str > /dev/null
			check_error

			tape_name=`$GREP '^[[:space:]]*NAME[[:space:]]*"[^"][^"]*"' $str | sed -e 's/^[[:space:]]*NAME[[:space:]]*"\([^"][^"]*\)".*/\1/'`
			$ECHO "          Tape \""$tape_name"\" migrated."
		fi
	done
}


init_os_variables

if [ ${#} -eq 1 ] && [ "$1" = --version ] ; then
	$ECHO $VERSION
	exit 0
fi

if [ ${#} -eq 2 ] && [ "$1" = --path ] ; then
	path52=$2
fi

if [ -z "$path52" ] ; then
	usage
fi

if [ ! -d "$path52" ] ; then $ECHO "$path52: no such directory!"; exit 1; fi


######
###          Migrate the dbase/f3 directory
######


if [ -d $path52/server/dbase/f3tape.v52install ] ; then
	$ECHO "This database has already been migrated."
	exit 1
fi

$ECHO "Migrating the dbase/f3 directories..."

# If the tptypes.lst file is not in 5.3 format (TP_TYPE "FILE"), stop
# now. This can happen if tptypes.lst was edited, or on Debian during
# the transition from tar.gz to .deb if the administrator replied "n"
# when prompted by dpkg.
f="$path52/server/dbase/f3tape/tptypes.lst"
if test -f "$f" && $GREP -q '^[[:space:]]*"NAME"[[:space:]]*"FILE[^"]' "$f"
then
	cat <<EOF

ERROR
$f looks like it comes from a
pre-5.3 version of Arkeia. Before you run arkmigrate-52-53, you need an
up-to-date tptypes.lst.
EOF
	exit 1
fi

# backup a copy of the v5.2 f3tape directory
$ECHO "    Making a backup copy of the f3tape directory..."
$CPREC $path52/server/dbase/f3tape $path52/server/dbase/f3tape.v52install
check_error
$ECHO "    Ok."


# set new arkeia file tape types
$ECHO "    Set new arkeia FILE tape types"
$ECHO "        Treating FILE 20MB tapes..."
set_new_file_tape_type "FILE 20MB" "20" "2000"
$ECHO "        Ok."
$ECHO "        Treating FILE 50MB tapes..."
set_new_file_tape_type "FILE 50MB" "50" "5000"
$ECHO "        Ok."
$ECHO "        Treating FILE 100MB tapes..."
set_new_file_tape_type "FILE 100MB" "100" "10000"
$ECHO "        Ok."
$ECHO "        Treating FILE 500MB tapes..."
set_new_file_tape_type "FILE 500MB" "500" "50000"
$ECHO "        Ok."
$ECHO "        Treating FILE 1GB tapes..."
set_new_file_tape_type "FILE 1GB" "1000" "100000"
$ECHO "        Ok."
$ECHO "        Treating FILE 5GB tapes..."
set_new_file_tape_type "FILE 5GB" "5000" "500000"
$ECHO "        Ok."
$ECHO "        Treating FILE 10GB tapes..."
set_new_file_tape_type "FILE 10GB" "10000" "1000000"
$ECHO "        Ok."

# 100GB FILE tapes are not supposed to be supported, but some users
# have created such tapes.
$ECHO "        Treating FILE 100GB tapes..."
set_new_file_tape_type "FILE 100GB" "100000" "10000000"
$ECHO "        Ok."

$ECHO "        Checking for other old FILE tapes..."
for str in `find $path52/server/dbase/f3tape -print | $GREP ".*/tp[^/]*/tp[^/]*\.lst"`; do
	if $GREP '^[[:space:]]*TP_TYPE[[:space:]]*"FILE[^"][^"]*"' $str > /dev/null; then
		tape_type=`$GREP '^[[:space:]]*TP_TYPE[[:space:]]*"FILE[^"][^"]*"' $str | sed -e 's/^[[:space:]]*TP_TYPE[[:space:]]*"\(FILE[^"][^"]*\)".*/\1/'`
		tape_name=`$GREP '^[[:space:]]*NAME[[:space:]]*"[^"][^"]*"' $str | sed -e 's/^[[:space:]]*NAME[[:space:]]*"\([^"][^"]*\)".*/\1/'`
		$ECHO "WARNING: the type \"$tape_type\" of the tape \"$tape_name\""
		$ECHO "is unknown and cannot be migrated. Please contact the Arkeia support for"
		$ECHO "more information."
	fi
done
$ECHO "        Ok."
$ECHO "    Ok."


######
###          Migrate the journal
######


$ECHO "Migrating the journal..."

cd "$path52"/server
for str in `ls server_??.jl2 2> /dev/null`; do
	year=`head -n 1 "$str" | cut -b 3-6`
	month=`$ECHO $str | cut -b 8-9`
	if [ ! -f "$path52/server/server_${year}_${month}".jl2 ] ; then
		$MV "$str" "$path52/server/server_${year}_${month}".jl2
		check_error
	else
		$ECHO "WARNING: it seems that the journal file $str has already been migrated."
		$ECHO "Not migrating it again."
	fi
done
$ECHO "Ok."



$ECHO The migration from Arkeia v5.2 to v5.3 was performed successfully !


######       END

