#!/usr/bin/sh

# The following script converts the exiting 4.1.3_U1 (rev A)  files
# in /export/exec/*, /export/exec/kvm/*  /etc/install/*  /tftpboot/*

# It assumes that all the clients to be upgraded from rev A to rev B
# have been added before the server is upgraded.

#
# Note :
# 
# 1. This script is needed to be run  when the system is running 
#    rev A which is going to be upgraded to rev B.
# 2. Before adding rev B clients to a rev A server.
#
#


CURDIR=`pwd`

#rename files in tftpboot and affect links appropriately.

echo "correcting files in /tftpboot ...."

if [ -d /tftpboot ]
then
	cd /tftpboot

	for j in boot.sun4m.sunos.4.1.3_U1 boot.sun4c.sunos.4.1.3_U1 boot.sun4.sunos.4.1.3_U1
	do
		dollar2="${j}A"
		mv $j $dollar2
		if [ $? != 0 ]
		then
			continue
		fi

		for i in `find . -type l -exec ls -l {} \; | awk '/->/ {printf  "%s!%s\n", $(NF-2), $NF}'`
		do
			one=`echo $i | cut -f1 -d'!'`
			two=`echo $i | cut -f2 -d'!'`
			if [ $two = $j ]
			then
				rm  $one
				ln -s $dollar2 $one	
			fi
		done
	done
fi



cd /export/exec
echo "Modifying files in /export/exec ...."

mv proto.root.sunos.4.1.3_U1 proto.root.sunos.4.1.3_U1A
mv sun4.sunos.4.1.3_U1 sun4.sunos.4.1.3_U1A


echo "Modifying files in /export/exec/kvm  ...."
cd /export/exec/kvm

if [ -d sun4.sunos.4.1.3_U1 -o -h sun4.sunos.4.1.3_U1 ]; then
	mv sun4.sunos.4.1.3_U1  sun4.sunos.4.1.3_U1A
	rm sun4
	ln -s sun4.sunos.4.1.3_U1A sun4
fi

if [ -d sun4c.sunos.4.1.3_U1 -o -h sun4c.sunos.4.1.3_U1 ]; then
	mv sun4c.sunos.4.1.3_U1 sun4c.sunos.4.1.3_U1A
	rm sun4c
	ln -s sun4c.sunos.4.1.3_U1A sun4c
fi

if [ -d sun4m.sunos.4.1.3_U1 -o -h sun4m.sunos.4.1.3_U1 ]; then
	mv sun4m.sunos.4.1.3_U1 sun4m.sunos.4.1.3_U1A
	rm sun4m
	ln -s sun4m.sunos.4.1.3_U1A sun4m
fi
	

cd /etc/install
echo "Modifying files in /etc/install ...."

for i in `ls appl_media_file.*`
do
	file="$i"A
	sed "s/4\.1\.3_U1/4\.1\.3_U1A/g" $i > /tmp/$file
	mv /tmp/$file .
	rm $i
done

for i in `ls client.*`
do
	sed "s/4\.1\.3_U1/4\.1\.3_U1A/g" $i > /tmp/$i
	mv /tmp/$i .
done

for i in `ls media_file.*`
do
	file="$i"A
	sed "s/4\.1\.3_U1/4\.1\.3_U1A/g" $i > /tmp/$file
	mv /tmp/$file .
	rm $i
done

for i in `ls soft_info.*`
do
	file="$i"A
	sed "s/4\.1\.3_U1/4\.1\.3_U1A/g" $i > /tmp/$file
	mv /tmp/$file .
	rm $i
done

for i in `ls client_list.*`
do
	file="$i"A
	mv $i $file
done

# Above, the following two files got changed.
# Change it back 

if [ -f client_list.allA ]; then
	mv client_list.allA client_list.all
fi
if [ -f media_file.tmpA ]; then
	mv media_file.tmpA media_file.tmp
fi

echo "Modifying the /etc/install/arch_info"
sed "s/4\.1\.3_U1/4\.1\.3_U1A/g"  arch_info > /tmp/arch_info
mv /tmp/arch_info .

echo "Modifying the /etc/install/arch_list"
sed "s/4\.1\.3_U1/4\.1\.3_U1A/g"  arch_list > /tmp/arch_list
mv /tmp/arch_list .

echo "Modifying the /etc/install/release"
sed "s/4\.1\.3_U1/4\.1\.3_U1A/g"  release > /tmp/release
mv /tmp/release .

echo "Modifying the /etc/install/sys_info"
sed "s/4\.1\.3_U1/4\.1\.3_U1A/g"  sys_info > /tmp/sys_info
mv /tmp/sys_info .

cd /sys/conf.common
sed "s/4\.1\.3_U1/4\.1\.3_U1A/g"  RELEASE > /tmp/RELEASE
mv /tmp/RELEASE .


cd $CURDIR
