#!/bin/ksh

#############################################################################
# Copyright (c) 1990-99 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
#############################################################################

#############################################################################
#
# @(#)nts_migrate $Revision: 1.5 $ $Date: 1999/05/10 12:55:36 $
#
#############################################################################

nts_echo()
{
	echo $* | tee -a ${LOGFILE}
}

usage()
{
	cat <<EOF | tee -a ${LOGFILE}
USAGE:      $0 [pkg-path]

ARGUMENTS:  pkg-path   optional file specification for the new
                       installation package

DESCRIPTION:

	This script attempts to migrate existing configurations of the
	PortServer  device  driver  package  in order to be compatible
	with the new (rev 3) device driver.   In doing so,  the script
	will uninstall the current driver package and will attempt (if
	the optional "pkg-path" parameter is used)  to install the new
	driver package.

	The  script should be run as "root",  with the PortServer (sa)
	devices and all attached serial ports in the "defined" state.

	The  script  and  associated  binaries manipulate both the ODM
	database and the device files in the "/dev" directory.   It is
	therefore recommended that a system backup be made before use.
	The script knows how to handle common configuration cases, but
	verification of operation after use is recommended.
EOF
}

assign_command()
{
	eval "$1=`which $2`"
	if [[ $? -ne 0 ]]
	then
		nts_echo "Couldn't find required command \"$2\" in path."
		exit 1
	fi
}

define_instructions()
{
	nts_echo ""
	nts_echo "The devices must be brought to the \"defined\" state before"
	nts_echo "this script can migrate the existing configuration to one"
	nts_echo "compatible with the new device driver package."
	nts_echo ""
	nts_echo "There are a number of ways to do this, including:"
	nts_echo ""
	nts_echo "   1. Execute the command:"
	nts_echo ""
	nts_echo "         odmdelete -q\"rule LIKE *nts*\" -o Config_Rules"
	nts_echo ""
	nts_echo "      and then reboot.  This will prevent the autoconfig."
	nts_echo "      of the PortServer devices and ports.  Due to its"
	nts_echo "      simplicity, this is the preferred method."
	nts_echo ""
	nts_echo "   2. Shutdown into single-user mode."
	nts_echo ""
	nts_echo "   3. By hand, disable all of the ports for login,"
	nts_echo "      kill all applications using any PortServer ports,"
	nts_echo "      and then execute \"rmdev -Rl\" for each PortServer"
	nts_echo "      (sa) device."
	nts_echo ""
	nts_echo "Only ONE of these methods should be used!"
}

process_file()
{
	filnam="$1"

	errcnt=0

	if [[ ! -e ${filnam} ]]
	then
		return 1
	fi

	while read line
	do
		first=`expr "$line" : '\(.\).*'`
		rest=`expr "$line" : '.\(.*\)'`

		CMD="echo"

		if [[ "$first" = "-" ]]
		then
			CMD=$RM

		elif [[ "$first" = "!" ]]
		then
			CMD=$MKNOD

		elif [[ "$first" = "+" ]]
		then
			CMD=$CHOWN

		elif [[ "$first" = "=" ]]
		then
			CMD=$CHMOD

		else
			continue
		fi

		$CMD $rest 2>&1 | tee -a ${LOGFILE}

		if [[ $? -ne 0 ]]
		then
			nts_echo ""
			nts_echo "ERROR: Couldn't \"$CMD $rest\""
			errcnt=`expr ${errcnt} + 1`
		fi
	done < $filnam

	if [[ $errcnt -ne 0 ]]
	then
		nts_echo ""
		nts_echo "${errcnt} errors."
		return ${errcnt}
	fi

	return 0
}

#
#  MAIN portion of script
#

ODMDIR=/etc/objrepos

PWD=`pwd`
TTYMAJOR=""
SAMAJOR=""
ALREADYREMOVED=""
DEVRMSCRIPT="${PWD}/.devremove"
DEVMKSCRIPT="${PWD}/.devcreate"
LOGFILE="${PWD}/migrate.log"


echo "Beginning migration procedure..." > ${LOGFILE}

if [[ $# -gt 1 ]]
then
	usage
	exit 1
fi

if [[ $# -eq 1 ]]
then
	INSTPKG="$1"
	if [[ ! -e ${INSTPKG} ]]
	then
		usage
		exit 1
	fi
else
	INSTPKG=""
fi


#-----------------------------------------------------------------------------

assign_command ID        id

nts_echo ""
nts_echo "Confirming that script is being run as \"root\"... \c"

curid=`$ID -u`
if [[ ${curid} -ne 0 ]]
then
	nts_echo ""
	nts_echo "Script is apparently being run as user ID ${curid},"
	nts_echo "when it is expected that one run it as user ID 0,"
	nts_echo "(the UID assigned to the \"root\" user)."
	exit 1
fi

nts_echo "done."

#-----------------------------------------------------------------------------

assign_command LSDEV     lsdev
assign_command LSLPP     lslpp
assign_command RMDEV     rmdev
assign_command INSTALLP  installp
assign_command RM        rm
assign_command MKNOD     mknod
assign_command CHOWN     chown
assign_command CHMOD     chmod
assign_command ODMDELETE odmdelete
assign_command ODMGET    odmget
assign_command CP        cp
assign_command TAIL      tail
assign_command HEAD      head
assign_command LS        ls
assign_command DF        df
assign_command BOSBOOT   bosboot

DEVLIST=`$LSDEV -C -c nts -Fname 2>>${LOGFILE}`

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Checking space requirements... \c"

$LS -s ${ODMDIR}/CuDvDr | read sizeofCuDvDr rest
$BOSBOOT -vq | $TAIL -1 | read bosbootnam bosbootsize rest

$DF ${PWD} | $TAIL -1 | read filesysnam dummy2 freeblocks rest
$DF ${bosbootnam} | $TAIL -1 | read dummy1 dummy2 bosbootfree rest

#
#  Assume 2M for all log files, plus size of CuDvDr
#
sizereq=`expr ${sizeofCuDvDr} + 1 + 2048`

if [[ ${freeblocks} -lt ${sizereq} ]]
then
	nts_echo ""
	nts_echo "It is suggested that at least ${sizereq} 1024-byte blocks"
	nts_echo "be available on the ${filesysnam} filesystem..."
	nts_echo "i.e. the filesystem where this migration procedure"
	nts_echo "resides and executes."
	exit 1
fi

#
#  There also needs to be enough room for the bosboot temporary files
#
if [[ ${bosbootfree} -lt ${bosbootsize} ]]
then
	nts_echo ""
	nts_echo "It is suggested that at least ${bosbootsize} 1024-byte blocks"
	nts_echo "be available on the ${bosbootnam} filesystem..."
	nts_echo "this is to allow for enough space to execute the"
	nts_echo "bosboot procedure."
	exit 1
fi

nts_echo "${freeblocks} blocks free... \c"

nts_echo "done."

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Checking installed revision... \c"

revstring=`$LSLPP -q -L digiasync.ncxa.obj 2>/dev/null | $HEAD -1`
rev=`expr "${revstring}" : '.*digi[^0-9]*\([^.]*\).[^.]*.[^.]*.[0-9]*[^0-9]`

if [[ "xx${rev}" = "xx" ]]
then
	ALREADYREMOVED="yes"

	tst=`$ODMGET -q"resource=ddins AND value1=ncxadd1" CuDvDr`
	if [[ "xx${tst}" = "xx" ]]
	then
		nts_echo ""
		nts_echo "No trace of a driver version prior to version three has"
		nts_echo "been found.  No migration is necessary.  Please install"
		nts_echo "the new driver."
		exit 1
	fi

	rev=2
fi

if [[ $rev -gt 2 ]]
then
	nts_echo ""
	nts_echo "A version 3 based device driver has already been installed."
	nts_echo "If a version 3 driver was installed accidentally before"
	nts_echo "noticing configuration problems, it is suggested that one"
	nts_echo "uninstall the version 3 driver and reinstall a version 2"
	nts_echo "driver before using this script."
	exit 1
fi

nts_echo "done."

#-----------------------------------------------------------------------------

if [[ "xx$DEVLIST" = "xx" ]]
then
	nts_echo "No migration is necessary.  Please uninstall the current"
	nts_echo "driver and install the new driver via SMIT."
	exit 1
fi

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Checking the integrity of the script and it's associated binaries... \c"

filelist="nts_migrate nts_chgodmdev"
for fil in ${filelist}
do
	if [[ ! -x ${PWD}/${fil} ]]
	then
		nts_echo ""
		nts_echo "Could not find \"${fil}\" in current directory"
		nts_echo "\"${PWD}\"."
		nts_echo ""
		nts_echo "The script and its associated binaries must be in the"
		nts_echo "same directory, and this script must be run in that"
		nts_echo "directory."
		exit 1
	fi
done

nts_echo "done."

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Confirm that all PortServer devices are in the defined state... \c"

all_defined=1

for dev in $DEVLIST
do
	curstatus=`$LSDEV -C -l $dev -r status 2>&1`
	if [[ $? -ne 0 ]]
	then
		nts_echo ""
		nts_echo "Couldn't test status of device $dev.\c"
		all_defined=0
		continue
	fi
	
	defstatus=`$LSDEV -C -l $dev -r status -S0 2>&1`
	if [[ $? -ne 0 ]]
	then
		nts_echo ""
		nts_echo "Couldn't test status of device $dev.\c"
		all_defined=0
		continue
	fi
	
	if [[ "${curstatus}" != "${defstatus}" ]]
	then
		nts_echo ""
		nts_echo "Device ${dev} in state ${curstatus} needs to be \"Defined\".\c"
		all_defined=0
		continue
	fi
done

if [[ ${all_defined} -eq 0 ]]
then
	nts_echo ""
	define_instructions
	exit 1
fi

nts_echo "done."

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Determining the PortServer major numbers... \c"

TTYMAJOR=`$ODMGET -q "resource=ddins AND value1=ncxadd" CuDvDr | grep value2`
TTYMAJOR=`expr "$TTYMAJOR" : '.*value2 = "\([0-9]*\)"'`

SAMAJOR=`$ODMGET -q "resource=ddins AND value1=ncxadd1" CuDvDr | grep value2`
SAMAJOR=`expr "$SAMAJOR" : '.*value2 = "\([0-9]*\)"'`

if [[ "xx${TTYMAJOR}" = "xx" ]]
then
	nts_echo ""
	nts_echo "The version ${rev} device driver may not have been"
	nts_echo "installed properly, as the tty major numbers could"
	nts_echo "not be located in the ODM."
	exit 1
fi

if [[ "xx${SAMAJOR}" = "xx" ]]
then
	nts_echo ""
	nts_echo "The version ${rev} device driver may not have been"
	nts_echo "installed properly, as the SA device major numbers"
	nts_echo "could not be located in the ODM."
	exit 1
fi

NEWMAJOR=$TTYMAJOR

nts_echo "${TTYMAJOR} and ${SAMAJOR} ==> ${NEWMAJOR}... \c"

nts_echo "done."

#-----------------------------------------------------------------------------

if [[ "xx${ALREADYREMOVED}" = "xx" ]]
then
	nts_echo ""
	nts_echo "Uninstalling the original driver... "

	$INSTALLP -u digiasync.ncxa.obj 2>&1 | tee -a ${LOGFILE}

	if [[ $? -ne 0 ]]
	then
		nts_echo "Failure reported from \"uninstall\"."
		nts_echo "Migration aborted."
		exit 1
	fi

	nts_echo "done."
fi

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Migrating ODM entries and generating scripts of /dev changes... \c"

$CP ${ODMDIR}/CuDvDr ${PWD}/CuDvDr.tmp 2>>${LOGFILE}

${PWD}/nts_chgodmdev ${SAMAJOR} ${TTYMAJOR} ${NEWMAJOR} \
                     "${DEVRMSCRIPT}" "${DEVMKSCRIPT}"

if [[ $? -ne 0 ]]
then
	nts_echo ""
	nts_echo "Errors were reported during this phase.  The ODM database"
	nts_echo "is being restored... \c"

	$CP ${PWD}/CuDvDr.tmp ${ODMDIR}/CuDvDr 2>>${LOGFILE}

	nts_echo "done.  Migration failed."
	exit 1
fi

$RM -f ${PWD}/CuDvDr.tmp 2>>${LOGFILE}

nts_echo "done."

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Deleting obsolete ODM entries... "

for dev in $DEVLIST
do
	$ODMDELETE -q"name=${dev} AND attribute=netname" \
	           -o CuAt 2>&1 | tee -a ${LOGFILE}
done

$ODMDELETE -q"resource=ddins AND value1=ncxadd1" \
           -o CuDvDr 2>&1 | tee -a ${LOGFILE}

nts_echo "done."

#-----------------------------------------------------------------------------

if [[ -e ${DEVRMSCRIPT} ]]
then
	nts_echo ""
	nts_echo "Removing existing device files... \c"

	process_file ${DEVRMSCRIPT}
	if [[ $? -ne 0 ]]
	then
		nts_echo ""
		nts_echo "Due to errors in remove process, migration aborted."
		echo "==================================================" >> ${LOGFILE}
		echo "Remove script:" >> ${LOGFILE}
		echo "--------------------------------------------------" >> ${LOGFILE}
		cat ${DEVRMSCRIPT} >> ${LOGFILE}
		echo "==================================================" >> ${LOGFILE}
		exit 1
	fi

	echo ""
	echo "==================================================" >> ${LOGFILE}
	echo "Remove script:" >> ${LOGFILE}
	echo "--------------------------------------------------" >> ${LOGFILE}
	cat ${DEVRMSCRIPT} >> ${LOGFILE}
	echo "==================================================" >> ${LOGFILE}

	nts_echo "done."
fi

#-----------------------------------------------------------------------------

if [[ -e ${DEVMKSCRIPT} ]]
then
	nts_echo ""
	nts_echo "Creating compatible device files... \c"

	process_file ${DEVMKSCRIPT}
	if [[ $? -ne 0 ]]
	then
		nts_echo ""
		nts_echo "Due to errors in remove process, migration aborted."
		echo "==================================================" >> ${LOGFILE}
		echo "Create script:" >> ${LOGFILE}
		echo "--------------------------------------------------" >> ${LOGFILE}
		cat ${DEVMKSCRIPT} >> ${LOGFILE}
		echo "==================================================" >> ${LOGFILE}
		exit 1
	fi

	echo ""
	echo "==================================================" >> ${LOGFILE}
	echo "Create script:" >> ${LOGFILE}
	echo "--------------------------------------------------" >> ${LOGFILE}
	cat ${DEVMKSCRIPT} >> ${LOGFILE}
	echo "==================================================" >> ${LOGFILE}

	nts_echo "done."
fi

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Executing the \"bosboot\" procedure to save changes... \c"

$BOSBOOT -a 2>&1 | tee -a ${LOGFILE}
if [[ $? -ne 0 ]]
then
	echo "The bosboot failed.  Please note the error messages and"
	echo "contact Digi.  Your system is (potentially) in an unstable"
	echo "state and SHOULD NOT be rebooted."
	exit 1
fi

nts_echo "done."

#-----------------------------------------------------------------------------

if [[ "xx${INSTPKG}" = "xx" ]]
then
	nts_echo ""
	nts_echo "The migration of the existing configuration information"
	nts_echo "is now complete."
	nts_echo ""
	nts_echo "At this point, you should install the new driver package"
	nts_echo "and then reboot."
	exit 1
fi

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "Attempting to install new package..."

$INSTALLP -acNd ${INSTPKG} digiasync.ncxa.obj 2>&1 | tee -a ${LOGFILE}
if [[ $? -ne 0 ]]
then
	nts_echo ""
	nts_echo "Failures detected during install.  While other migration"
	nts_echo "tasks have completed, the installation of the new driver"
	nts_echo "has failed.  Please manually install the new driver"
	nts_echo "package, then reboot."
	exit 1
fi

nts_echo "done."

#-----------------------------------------------------------------------------

nts_echo ""
nts_echo "The migration process is now complete.  Please reboot your system."
