#!/bin/sh
# arkmigrate1-42-50 : Arkeia Database Migration Procedure
# Copyright Arkeia Corporation 2002


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


oldname=''
fqdn=''
path42=''
path50=''


usage() {
	$ECHO "Arkeia Database Migration Procedure Version $VERSION"
	$ECHO "Usage:"
	$ECHO "  arkmigrate-42-50 --oldname [VALUE] --fqdn [VALUE] --path42 [VALUE] --path50 [VALUE]"
	$ECHO "    --oldname : the Arkeia v4.2 machine name"
	$ECHO "    --fqdn : the fully qualified domain name of the machine"
	$ECHO "    --path42 : the arkeia 4.2 root directory (ex : /usr/knox)"
	$ECHO "    --path50 : the arkeia 5 root directory (ex : /opt/arkeia)"
	$ECHO ""
	$ECHO "CAUTION: please before proceeding the migration, make a tar backup copy of"
	$ECHO "the /usr/knox directory because this program will change the index located"
	$ECHO "in PATH42/arkeia/dbase/o3dbtree directory."
	$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 4.2 tape type name
# $2 : arkeia 5 tape type name
# $3 : a message
set_new_tape_type() {
	msgprinted=''
	for str in `find $path50/server/dbase/f3tape -print | $GREP ".*/tp[^/]*/tp[^/]*\.lst"`; do
		if $GREP "^[[:space:]]*TP_TYPE[[:space:]]*\"$1\"" $str > /dev/null; then
			modify_prop "TP_TYPE" "$2" "$str"
			if [ -z "$msgprinted" ] ; then
				$ECHOE "$3"
				msgprinted="1"
			fi
		fi
	done
}


init_os_variables

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

while [ ${#} -ge 2 ] ; do
	case $1 in
		--oldname)
			oldname=$2
			;;
		--fqdn)
			fqdn=$2
			;;
		--path42)
			path42=$2
			;;
		--path50)
			path50=$2
			;;
	esac
	shift
done

if [ -z "$oldname" -o -z "$fqdn" -o -z "$path42" -o -z "$path50" ] ; then
	usage
fi

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


######
###          Migrate nlp to arkeiad
######


$ECHO "Migrating $path42/nlp to $path50/arkeiad. "

# update admin.cfg with fqdn (leave the default 617 port)
$ECHO $fqdn > $path50/arkeiad/admin.cfg

# copy the rhost.lst file
$CP $path42/nlp/rhost.lst $path50/arkeiad
$ECHO Ok.


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


$ECHO "Migrating the dbase/f3 directories"

# backup a copy of the v5 dbase directory
$ECHO "    backup a copy of the v5 dbase directory"
$CPREC $path50/server/dbase $path50/server/dbase.v5install
$ECHO "    Ok."


# get v42 f3tape, f3pool, f3drv, f3dpk, f3spk, f3bkp and f3lib directories
$ECHO "    Migrate v42 f3tape, f3pool, f3drv, f3dpk, f3spk, f3bkp and f3lib directories"
for str in f3tape f3pool f3drv f3dpk f3spk f3bkp f3lib; do
	$ECHO "        Migrating $str..."
	cd $path42/arkeia/dbase
	$TAR -cf - $str | (cd $path50/server/dbase; $TAR -xf -)
	$ECHO "        Ok."
done

$CP $path50/server/dbase.v5install/f3tape/tptypes.lst \
	$path50/server/dbase/f3tape/
check_error
$CP $path50/server/dbase.v5install/f3drv/drvtypes.lst \
	$path50/server/dbase/f3drv/
check_error
$RMREC $path50/server/dbase/f3lib/libtypes
check_error
$CPREC $path50/server/dbase.v5install/f3lib/libtypes\
	$path50/server/dbase/f3lib/
check_error
$ECHO "    Ok."


# get f3sec/usr.lst and f3sec/licenses.lst
$ECHO "    Migrate f3sec/usr.lst and f3sec/licenses.lst"
$CP $path42/arkeia/dbase/f3sec/usr.lst $path50/server/dbase/f3sec
if [ -f $path42/arkeia/dbase/f3sec/license.lst ] ; then
	$CP $path42/arkeia/dbase/f3sec/license.lst $path50/server/dbase/f3sec
fi
$ECHO "    Ok."


# migrate the 4.2 library definition
$ECHO "    Migrate the 4.2 library definition"
for str in `find $path50/server/dbase/f3lib | $GREP -v "libtypes" \
		| $GREP ".*/lib[^/]*/lib[^/]*\.lst"`; do
	# add SLOT_OFFSET, DRV_OFFSET and ARKLIB_NODE properties
	$ECHO "    Add SLOT_OFFSET, DRV_OFFSET and ARKLIB_NODE properties in $str file"
	$ECHOE "1i\nSLOT_OFFSET	\"1\"\nDRV_OFFSET	\"1\"\n.\n/^[[:space:]]*NODE[[:space:]]*\".*\"\nco .\n.s/NODE/ARKLIB_NODE/\nwq\n" | $EX -s $str > /dev/null
	check_error
	$ECHO "    Ok."
	# specific treatments for FILE libraries
	if $GREP "^[[:space:]]*LIB_TYPE[[:space:]]*\"FILE\"" $str > /dev/null
	then
		pathlen=`echo $str | wc -c`
		libid=`$ECHO $str | cut -b \`expr $pathlen - 12\`-\`expr $pathlen - 5\``
		if [ -z "$libid" ] ; then 
			$ECHO "    Error : Impossible to get libid!"
			exit 1
		fi
		$ECHO "        Treating library $libid..."
		# move and rename the LIB_FILE directory
		$ECHO "        Move and rename the LIB_FILE directory"
		$MV $path50/server/dbase/f3lib/lib$libid/LIB_FILE $path50/server/dbase/f3lib/LIB_FILE$libid
		if [ $? != 0 ] ; then continue; fi
		$ECHO "        Ok."
		# create the REWIND_DEV files
		$ECHO "        Create the REWIND_DEV files"
		ln -s $path50/server/dbase/f3lib/lib$libid/../LIB_FILE$libid/drive_01 $path50/server/dbase/f3lib/lib$libid/../LIB_FILE$libid/REWIND_DEV
		$ECHO "        Ok."
		# update the FILE libraries ARM_DEV property
		$ECHO "        Update the FILE libraries ARM_DEV property"
		if $GREP "^[[:space:]]*ARM_DEV[[:space:]]*\".*LIB_FILE\"" $str > /dev/null
		then
			tmp=`$ECHO "$path50/server/dbase/f3lib/lib$libid/../LIB_FILE$libid" | sed -e 's:\/:\\\/:g' -e 's/\./\\\./g'`
			check_error
			modify_prop "ARM_DEV" "$tmp" "$str"
		fi
		$ECHO "        Ok."
	fi
	$ECHO "    Ok."
done


# remove old REWIND_DEV files
$ECHO "    Remove old REWIND_DEV files"
$RM `find $path50/server/dbase/f3drv -name REWIND_DEV -print`
$ECHO "    Ok."


# update REWIND_DEV and CONTROL_DEV properties in FILE drives
$ECHO "    Update REWIND_DEV and CONTROL_DEV properties in FILE drives"
for str in `find $path50/server/dbase/f3drv -print | $GREP ".*/drv[^/]*/drv[^/]*\.lst"`; do
	if $GREP "^[[:space:]]*DRV_TYPE[[:space:]]*\"FILE\"" $str > /dev/null
	then
		$ECHO "        Treating drive $str..."
		libid=`$GREP "^[[:space:]]*LIBID[[:space:]]*\".*\"" $str | sed -e 's/^[[:space:]]*LIBID[[:space:]]*"//' -e 's/"[[:space:]]*$//'`
		if [ -z "$libid" ] ; then
			$ECHO "        Nothing to do!"
			continue;
		fi
		$ECHO "        Drive is in library $libid. Updating..."
		rwdev=`$ECHO "$path50/server/dbase/f3lib/lib$libid/../LIB_FILE$libid/REWIND_DEV" | sed -e 's:\/:\\\/:g' -e 's/\./\\\./g'`
		check_error
		if $GREP "^[[:space:]]*CONTROL_DEV[[:space:]]*\".*\"" $str > /dev/null
		then
			modify_prop "CONTROL_DEV" "$rwdev" "$str"
		fi
		$ECHO "        Ok for CONTROL_DEV"
		if $GREP "^[[:space:]]*REWIND_DEV[[:space:]]*\".*\"" $str > /dev/null
		then
			modify_prop "REWIND_DEV" "$rwdev" "$str"
		fi
		$ECHO "        Ok for REWIND_DEV"
	fi
done
$ECHO "    Ok."


# set new arkeia tape types
$ECHO "    Set new arkeia tape types"
$ECHO "        Treating LTO tapes..."
set_new_tape_type "LTO Ultrium" "LTO Ultrium 100GB" \
	"WARNING : You have LTO tape(s). Arkeia 5 LTO tapes type has changed. \nYour LTO tape(s) type has been set to ""LTO Ultrium 100GB"". \nOther Arkeia 5 LTO tape types are : ""LTO Ultrium 10GB"" and ""LTO Ultrium 50GB"". \nYou must change your LTO tape(s) type manually if ""LTO Ultrium 100GB"" \nis not the good one!"
$ECHO "        Ok."
$ECHO "        Treating SDLT tapes..."
set_new_tape_type "SDLT" "SDLT 220" \
	"WARNING : You have SDLT tape(s). Arkeia 5 SDLT tapes type has changed. \nYour SDLT tape(s) type has been set to ""SDLT 220"". \nOther Arkeia 5 SDLT tape types are : ""SDLT 220"" and ""SDLT 320"". \nYou must change your SDLT tape(s) type manually if ""SDLT 220"" \nis not the good one!"
$ECHO "        Ok."
$ECHO "    Ok."


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


$ECHO "Migrating the journal..."

for str in `ls $path42/arkeia/*.jl2`; do
	tmp=`$ECHO $str | sed -e 's:^.*\/arkeia\([^/]*\)\.jl2:server\1\.jl2:'`
	$CP $str $path50/server/$tmp
done
$ECHO "Ok."



######
###          Migrate the v4.2 index
######


$ECHO "Migrating the index."

# link the new arkeia 5 database to the old arkeia 4.2 index
$ECHO "    Link the new arkeia 5 database to the old arkeia 4.2 index"
$RMREC $path50/server/dbase/o3dbtree
ln -s $path42/arkeia/dbase/o3dbtree $path50/server/dbase/o3dbtree
$ECHO "    Ok."

ARKEIA_DIR=$path50
export ARKEIA_DIR

$ECHO "    Adding machine_name/file directory and machine_name/file/o3_cpnt file"
cd $path50/server/dbase/o3dbtree
for str in `ls`; do
	cd $path50/server/dbase/o3dbtree
	if [ -d "$str" ] ; then
		$ECHO "        Treating $path50/server/dbase/o3dbtree/$str"
		$ECHO "            Adding file directory..."
		cd $path50/server/dbase/o3dbtree/$str
		check_error
		mkdir file
		for str2 in *; do
			if [ "$str2" != "file" ] ; then
				$MV "$str2" file
				check_error
			fi
		done
		$ECHO "            Ok."

		# create the o3dbtree/$str/o3_cpnt file
		$ECHO "            Creating the o3dbtree/$str/o3_cpnt file"
		$path50/bin/migration/mkfileo3_cpnt $str
		check_error
		$ECHO "            Ok."
		$ECHO "        Ok."
	fi
done
$ECHO "    Ok."


# update the backup index, the savepacks, the libraries
# the drives and the user list
$ECHO "Migrating $oldname to $fqdn using arkmvhost"
$path50/bin/arkmvhost --no_confirm --host $oldname --newhost $fqdn
check_error
$ECHO "Ok."


$ECHO The Database migration was performed successfully !


######       END

