#!/bin/sh
#
# (c)1997, 98, 99, Be, Inc. All rights reserved.

CMD_ESC='-'
NO_ERR='0'
ERR_BAD_CMD='-1'
ERR_NO_VOL='-2'
NEW_SAMPLE_DIR=optional/sample-code
OLD_SAMPLE_DIR=optional/old-sample-code

prog_name="${0}"

# Check the command-line arguments
for arg ; do
	if [ "${arg#$CMD_ESC}" = "${arg}" ] ; then
		# The argument is not a tag
		volume="${arg}"
	fi
done

# Check to see if we should do anything
if [ -z "${volume}" ] ; then
	echo "Usage: ${prog_name} VOLUME_NAME"
	exit "${ERR_BAD_CMD}"
fi

# Check to see if the disk exists and is mounted
if ! cd /"${volume}" &>/dev/null ; then
	echo "${prog_name}: \"${volume}\" not mounted"
	exit "${ERR_NO_VOL}"
fi

#
# make some indices on the target volume
#
mkindex -t int "_trk/qrylastchange"

#
# Move things their new location
#
mv apps/Metrowerks/stationary develop/BeIDE

# Delete obsolete files
rm -f home/config/be/Applications/{Connect,IconWorld,PlaySound,simple-midi}
rm -f {beos/,}apps/{Connect,IconWorld,PlaySound,simple-midi}
rm -f home/config/be/Preferences/{Audio,Sound,Video,Serial,AddPrinter,SelectPrinter}
rm -f {beos/,}preferences/{Audio,Sound,Video,Serial,AddPrinter,SelectPrinter}
rm -f beos/bin/lilo*
rm -rf home/config/be/Demos/{3dsound,Dominos,Life,SoftwareValet}
rm -rf home/config/be/SoftwareValet
rm -f home/config/settings/kernel_settings
rm -rf home/config/settings/Media
rm -f beos/system/add-ons/kernel/drivers/dev/{joystick,midi,audio/mix,audio/mux,audio/old,audio/raw}/emu10k
rm -fr home/config/settings/Epson
rm -f "home/config/add-ons/Print/Epson Stylus Driver"

# obsolete files deleted for R5
rm -f beos/system/add-ons/Tracker/Backgrounds	# replaced by Background-B
rm -f beos/bin/cardmgr # made obsolete by the watcher
rm -fr demos/{3dsound,Chart,FontDemo,GLTeapot,Minesweeper} # 3dsound is now 3dmiX, in apps...the others are now links
rm -f home/config/add-ons/kernel/file_systems/{cdda,ext2,iso9660,ntfs} # now standard
rm -fr beos/etc/ppd/*
rm -fr "beos/system/add-ons/Print/Apple Laserwriter Compatible"
catattr "Driver Name" /boot/home/config/settings/printers/* | grep "Apple Laserwriter Compatible" | awk -F ' : ' {'print $1'}  | while read line; do addattr "Driver Name" "PostScript" "$line"; done 

# superceded by Be-supplied translator
rm -f home/config/add-ons/Translators/PNGTranslator

rm -f beos/system/add-ons/drive_setup/fs/{cdda-fs,ext2,ntfs}

# replaced by misc/mempool
rm -f beos/system/add-ons/kernel/drivers/{dev,bin}/memtypes

# obsolete files deleted for EXP
rm -f home/config/lib/{libGL.so,libglide*.so}
rm -f beos/system/add-ons/kernel/drivers/{dev/graphics,bin}/3dfx
# replaced by ne2k driver
rm -f beos/system/add-ons/kernel/drivers/{dev/net,bin}/{ether,etherpci,etherpcmcia,etherpnp}
# replaced by gameaudio drivers
rm -f beos/system/add-ons/kernel/drivers/{dev/audio/old,bin}/sonic_vibes,i801
rm -f beos/system/add-ons/kernel/drivers/{dev/audio/,bin}/emu10k
# new fonts for old
rm -fr beos/etc/fonts/*
# many fonts are incorrectly hinted, causing FontFusion to crash.  be safe.
mv home/config/fonts home/config/old-fonts
mkdir -p home/config/fonts/ttfonts
# we know that these particular fonts are okay
for i in	Arial.ttf ArialBlack.ttf ArialBlack_Italic.ttf \
			ArialNarrow.ttf ArialNarrow_Bold.ttf ArialNarrow_BoldItalic.ttf \
			ArialNarrow_Italic.ttf Arial_Bold.ttf Arial_BoldItalic.ttf Arial_Italic.ttf \
			BookAntiqua.ttf BookAntiqua_Bold.ttf BookAntiqua_BoldItalic.ttf BookAntiqua_Italic.ttf \
			BookmanOldStyle.ttf BookmanOldStyle_Bold.ttf BookmanOldStyle_BoldItalic.ttf BookmanOldStyle_Italic.ttf \
			CenturyGothic.ttf CenturyGothic_Bold.ttf CenturyGothic_BoldItalic.ttf CenturyGothic_Italic.ttf \
			Comic.ttf Comic_Bold.ttf \
			GEORGIA.TTF GEORGIAB.TTF GEORGIAI.TTF GEORGIAZ.TTF \
			Garamond.ttf Garamond_Bold.ttf Garamond_Italic.ttf \
			GobePronunciation.ttf \
			Impact.ttf "Lucida Console.ttf" Tahoma.ttf Tahoma_Bold.ttf \
			Times.ttf Times_Bold.ttf Times_BoldItalic.ttf Times_Italic.ttf \
			Trebuchet.ttf Trebuchet_Bold.ttf Trebuchet_BoldItalic.ttf Trebuchet_Italic.ttf \
			Verdana.ttf Verdana_Bold.ttf Verdana_BoldItalic.ttf	Verdana_Italic.ttf \
			Webdings.ttf Wingding.ttf chromohv.ttf chromolt.ttf cyberbit.ttf
do
	if [ -f "home/config/old-fonts/ttfonts/$i" ] mv "home/config/old-fonts/ttfonts/$i" "home/config/fonts/ttfonts/$i"
done

if [ "$BE_HOST_CPU" = "ppc" ]; then
	rm -f `query "name = mergeres || name = mwbres || name = mwdres"`
	rm -f home/config/be/Preferences/Joysticks
	rm -f {beos/,}preferences/Joysticks 
else
	rm -f `query "name = mwbres"`
fi
rm -rf beos/system/add-ons/media
rm -rf apps/Metrowerks
rm -rf develop/headers/{posix,cpp}
rm -rf "beos/documentation/Shell Tools"
if [ -f beos/system/kernel_intel ]; then
	rm -f beos/bin/{ld,cc,ar,as}

	rm -f {beos/,}preferences/Boot
	rm -f home/config/be/Preferences/Boot
	rm -f home/config/settings/Is_Default_Volume

	# R4 and R4.5 installed the following link in the wrong place
	rm -f beos/system/add-ons/kernel/drivers/dev/ports/config

	rm -f beos/system/add-ons/kernel/drivers/dev/misc/1394
	rm -f beos/system/add-ons/kernel/drivers/dev/bus/1394
	rm -f beos/system/add-ons/media/dvcapture.media_addon

	if [ -d home/config/settings/config_manager ]
	then
		mv home/config/settings/config_manager home/config/settings/kernel/config_manager
	fi
fi

rm -f home/config/settings/kernel/drivers/sample/*.sample


#delete Tracker settings that were broken in R4
rm -rf home/config/settings/Tracker/DefaultQueryTemplates/text_x-email

#
#	Save the old sample code so the new code won't
#	Clobber it.
#
#	check to see if the pre-Genki sample-code
#	heirarchy is in place. In Genki + the
#	HelloWorld directory is at
#	NEW_SAMPLE_DIR/intro/HelloWorld
if [ -d "$NEW_SAMPLE_DIR/HelloWorld" ]
then
	if ! [ -d $OLD_SAMPLE_DIR ]
	then
		mv $NEW_SAMPLE_DIR $OLD_SAMPLE_DIR
	fi
fi

# font names have become more descriptive
rm -f beos/etc/fonts/ttfonts/tt*
rm -f beos/etc/fonts/ttfonts/profont*

#rm -f home/config/settings/fonts_list   # Remove font list & cache since they
#rm -f home/config/settings/fonts_cache  # have been known to cause problems...
#rm -f home/config/settings/fonts_status

# Get rid of old app_server settings
#rm -f home/config/settings/Desktop_settings
#rm -f home/config/settings/Screen_data	# Solve off-screen Screen problem
#rm -f home/config/settings/Screen_settings
#rm -f home/config/settings/Workspace_data


# Remove the obsolete links in the be menu
# Basically, the "-L home/config/be/apps" tests to see if there is
# indeed an item in home/config/be/apps and it is indeed a link.
# Lest someone remove the default links and create their own which
# point elsewhere, we must check to see that they point to the right
# spot. That's what that "/bin/ls ... | /bin/awk ..." thing is all
# about. We give the -Fl arguments to ls to print out a long format
# with the item to which the link points. We have to also give it
# --full-time to insure that it has a consistent number of fields
# (which it normally would not). Now we pipe it to awk which checks
# to make sure if the twelfth field is "->" (which it should be if
# the line item is a symbolic link) and if the last field is the
# the thirteenth field (which it would not be if there were spaces
# in the path to which the link points) and if it resolves to the
# original. This avoids the problem where we would remove the link
# if a user had done something like:
#     rm -fr ~/config/be/apps
#     mkdir -p ~/foo\ /boot/apps
#     ln -s ~/foo\ /boot/apps ~/config/be/apps
# Without the field check in awk, we would remove the above link.
if [ -L home/config/be/apps ] \
	&& [ `/bin/ls -Fl --full-time home/config/be/apps | /bin/awk '{ if ($12 == "->" && NF == 13) print $NF }'` = "/boot/apps/" ] ; then
	rm -f home/config/be/apps;
fi

if [ -L home/config/be/demos ] \
	&& [ `/bin/ls -Fl --full-time home/config/be/demos | /bin/awk '{ if ($12 == "->" && NF == 13) print $NF }'` = "/boot/demos/" ] ; then
	rm -f home/config/be/demos;
fi

if [ -L home/config/be/preferences ] \
	&& [ `/bin/ls -Fl --full-time home/config/be/preferences | /bin/awk '{ if ($12 == "->" && NF == 13) print $NF }'` = "/boot/preferences/" ] ; then
	rm -f home/config/be/preferences;
fi

# precompiled headers now have their own folder
if mv "develop/headers/be/BeHeaders.pch++" "${TEMP_DIR}" &>/dev/null ; then
	echo "Moved old precompiled headers to \"`pwd`/${TEMP_DIR}\"." >>"${TEMP_DIR}"/"${README}"
	moved_something='1'
fi

if mv "develop/headers/be/BeHeaders" "${TEMP_DIR}" &>/dev/null ; then
	echo "Moved old precompiled headers to \"`pwd`/${TEMP_DIR}\"." >>"${TEMP_DIR}"/"${README}"
	moved_something='1'
fi

# mount BeOS_Tools ISO/HFS partition for shared files
mountvolume -ro BeOS_Tools

exit "${NO_ERR}"
