#!/bin/sh
#
# Generates an environment for multiple developer packages.
# This script installs or removes a setup where you can choose between
# different developer software releases just by flipping a symbolic link.
# Before you start the script, you must install OS4.0 Developer and unpack
# the NS3.2 Developer packages into the directory /Dev-3.2. Unpacking can
# be done with the command
#
#	mkdir /Dev-3.2; cd /Dev-3.2
# 	zcat /NEXTSTEP_Dev_3.2/NextCD/Packages/XXX/XXX.tar.Z |	\
#		/NextAdmin/Installer.app/installer_bigtar xvf -
#
# XXX denotes the name of the package.
# You may want to thin out the executables and libraries by hand (this is
# normally done by Installer.app during the installation).
#
# Author: Christian Starkjohann <cs@ds1.kph.tuwien.ac.at>
# Copyright: Public Domain
# There is absolutely no warranty etc., etc. .....
#

# Change this to 3.3 if you want to share 3.3 Developer with 4.0 Dev.
# If you use 3.3, you _must_ remove /Dev-3.3/usr/lib/dyld before you call this
# script!
OLD_DIR="/Dev-3.2"

NEW_DIR="/Dev-4.0"

if [ "$1" = "remove" ]; then
	install=no
else
	if [ "$1" != "install" ]; then
		echo "usage: $0 <mode>"
		echo "where mode may be either install or remove"
		exit
	fi
	install=yes
fi

if [ -d $OLD_DIR ]; then
	cd $OLD_DIR
	FILES=`echo bin/* lib/* NextDeveloper usr/lib/* usr/ucb/*`
else
	echo "fatal: You must install the old developer packages in $OLD_DIR"
	exit
fi

cd /

if [ $install = "yes" -a -d $NEW_DIR ]; then
	echo "fatal: Dual developer mode has already been installed."
	exit
fi

if [ $install = "yes" ]; then
	mkdir $NEW_DIR
	mkdir $NEW_DIR/bin
	mkdir $NEW_DIR/lib
	mkdir $NEW_DIR/usr
	mkdir $NEW_DIR/usr/lib
	mkdir $NEW_DIR/usr/ucb
	chmod -R a+rX $NEW_DIR
fi

if [ -h /Dev-current ]; then
	echo "info: Current developer directory exists, leaving it alone."
else
	rm -f /Dev-current
	ln -s $NEW_DIR /Dev-current
fi

for i in $FILES; do
	if [ $install = "yes" ]; then
		if [ -r "/$i" -o -h "/$i" ]; then
			mv "/$i" "$NEW_DIR/$i"
		else
			echo "info: ->/$i<- does not exist in Developer 4.0"
		fi
		ln -s "/Dev-current/$i" "/$i"
	else
		if [ -h "/$i" ]; then
			rm "/$i"
			if [ -r "$NEW_DIR/$i" -o -h "$NEW_DIR/$i" ]; then
				mv "$NEW_DIR/$i" "/$i"
			else
				echo "info: ->/$i<- does not exist in Developer 4.0"
			fi
		else
			echo "fatal: something must be wrong: /$i is not a link"
		fi
	fi
done

if [ $install != "yes" ]; then
	echo "please examine $NEW_DIR and delete the directory by hand."
fi
