# This shell script installs a new version of MINIX (e.g., 1.6.2x).
# To use it, first copy the original 1.5 distribution to a scratch
# directory, say, /usr/src/1.5.  Copy all the update files (*.s*) to $old,  
# along with libc.a (the old library).  The cd to that directory.
# To perform the update, type either
#
#	Install kr		# if using the old K&R compiler
# or
#	Install ansi		# if using an ANSI compiler

# Definitions
nl="posix other string curses end" 	# libs that will be totally replaced
nc="aal advent bc elle elvis ic mined scripts"	# cmds that will be replaced

case $1 in
kr)	;;
ansi)	;;
*)	echo Usage: Install ansi "    or   "  Install kr
	exit
esac

echo "Be sure that you have the 1993 version of unshar and a version of patch"
echo "with at least 50K stack.  If you haven't checked, hit DEL immediately."
sleep 5
echo Begin installation

top=`pwd`

# Check if the original directories and files exist
echo Checking if all V1.5 directories exist
f=0
if test ! -d kernel; then echo $top/kernel directory missing; f=1; fi
if test ! -d fs; then echo $top/fs directory missing; f=1; fi
if test ! -d mm; then echo $top/mm directory missing; fi
if test ! -d tools; then echo $top/tools directory missing; fi
if test ! -d test; then echo $top/test directory missing; fi
if test ! -d lib; then echo $top/lib directory missing; fi
if test ! -d lib/ansi; then echo $top/lib/ansi directory missing; fi
if test ! -d lib/posix; then echo $top/lib/posix directory missing; fi
if test ! -d lib/other; then echo $top/lib/other directory missing; fi
if test ! -d lib/string; then echo $top/lib/string directory missing; fi
if test ! -d lib/ibm; then echo $top/lib/ibm directory missing; fi
if test -d command; then mv command commands; fi
if test ! -d commands; then echo $top/commands directory missing; fi
if test $f = 1; then exit; fi

# Check if the update files are present
echo Checking if all update files exist
f=0
if test ! -f advent.sh; then echo advent.sh missing; f=1; fi
if test ! -f ansi.sh; then echo ansi.sh missing; f=1; fi
if test ! -f bawk.sh; then echo bawk.sh missing; f=1; fi
if test ! -f bc.sh; then echo bc.sh missing; f=1; fi
if test ! -f curses.sh; then echo curses.sh missing; f=1; fi
if test ! -f de.sh; then echo de.sh missing; f=1; fi
if test ! -f dis88.sh; then echo dis88.sh missing; f=1; fi
if test ! -f elle.sh; then echo elle.sh missing; f=1; fi
if test ! -f elvis.sh; then echo elvis.sh missing; f=1; fi
if test ! -f end.sh; then echo end.sh missing; f=1; fi
if test ! -f fs.sh; then echo fs.sh missing; f=1; fi
if test ! -f ibm.kr.sh; then echo ibm.kr.sh missing; f=1; fi
if test ! -f ibm.ansi.sh; then echo ibm.ansi.sh missing; f=1; fi
if test ! -f ibm.sh; then echo ibm.sh missing; f=1; fi
if test ! -f ic.sh; then echo ic.sh missing; f=1; fi
if test ! -f include.sh; then echo include.sh missing; f=1; fi
if test ! -f indent.sh; then echo indent.sh missing; f=1; fi
if test ! -f kermit.sh; then echo kermit.sh missing; f=1; fi
if test ! -f kernel.sh; then echo kernel.sh missing; f=1; fi
if test ! -f lib.sh; then echo lib.sh missing; f=1; fi
if test ! -f m4.sh; then echo m4.sh missing; f=1; fi
if test ! -f make.sh; then echo make.sh missing; f=1; fi
if test ! -f mined.sh; then echo mined.sh missing; f=1; fi
if test ! -f minix.sh; then echo minix.sh missing; f=1; fi
if test ! -f mm.sh; then echo mm.sh missing; f=1; fi
if test ! -f nroff.sh; then echo nroff.sh missing; f=1; fi
if test ! -f other.sh; then echo other.sh missing; f=1; fi
if test ! -f patch.sh; then echo patch.sh missing; f=1; fi
if test ! -f posix.sh; then echo posix.sh missing; f=1; fi
if test ! -f scripts.sh; then echo scripts.sh missing; f=1; fi
if test ! -f sh.sh; then echo sh.sh missing; f=1; fi
if test ! -f simple.sh; then echo simple.sh missing; f=1; fi
if test ! -f stdio.sh; then echo stdio.sh missing; f=1; fi
if test ! -f string.sh; then echo string.sh missing; f=1; fi
if test ! -f sys.sh; then echo sys.sh missing; f=1; fi
if test ! -f syscall.ansi.s*; then echo syscall.ansi.s* missing; f=1; fi
if test ! -f syscall.kr.sh; then echo syscall.kr.sh missing; f=1; fi
if test ! -f test.sh; then echo test.sh missing; f=1; fi
if test ! -f tools.sh; then echo tools.sh missing; f=1; fi
if test ! -f zmodem.sh; then echo zmodem.sh missing; f=1; fi
if test $f = 1; then exit; fi

# Make new directories needed
rm -rf lib/syscall* lib/ibm* lib/end
mkdir lib/stdio lib/syscall.ansi lib/syscall.kr lib/syscall
mkdir lib/ibm.ansi lib/ibm.kr lib/ibm lib/end lib/curses
mkdir commands/simple commands/scripts
mkdir commands/aal commands/advent commands/bc commands/elle >/dev/null 2>&1

# Some directories will be replaced entirely.  Remove the old files from them.
for i in $nl; do rm -f lib/$i/*; done
for i in $nc; do rm -f commands/$i/*; done

# Move the update files into position
for i in include kernel mm fs tools test; do mv $i.sh $i; done
for i in minix sys; do mv $i.sh include/$i; done
mv lib.sh lib/lib.sh
for i in posix ansi other stdio string syscall.kr end ibm.ansi ibm.kr curses
do mv $i.sh lib/$i
done
mv syscall.ansi.s* lib/syscall.ansi
mv ibm.sh commands/ibm/ibm.sh
for i in aal advent bawk bc de dis88 elle elvis ic indent kermit m4 \
				make mined nroff patch scripts simple sh zmodem
do if test ! -d commands/$i; then mkdir commands/$i; fi
   mv $i.sh commands/$i
done

# Everything is present. Unpack the shar files
echo Begin unpacking files
for i in include kernel fs mm tools test
do cd $i; 
   unshar $i.sh >log;  unshar $i.cd >>log
   for j in *.d; do patch <$j  >>log 2>&1; done
   rm -f *~ *.orig *.cd *.d *.sh
   cd ..
done

for i in minix sys
do cd include/$i
   unshar $i.sh >log;  unshar $i.cd >>log
   for j in *.d; do patch <$j  >>log 2>&1; done
   rm -f *~ *.orig *.cd *.d *.sh
   cd ../..
done
echo Operating System unpacked

cd lib; unshar lib.sh >>log; cd ..
for i in posix ansi other stdio string syscall.ansi syscall.kr end \
            ibm.ansi ibm.kr curses
do cd lib/$i
   unshar $i.sh >log
   if test -f $i.cd
      then unshar $i.cd >>log
	   for j in *.d; do patch <$j >>log 2>&1; done
   fi
   rm -f *~ *.orig *.cd *.d *.sh
   cd ../..
done
echo Libraries unpacked

cd commands
mv *.c simple
for i in aal advent bawk bc de dis88 elle elvis ibm ic indent kermit m4 \
				make mined nroff patch scripts simple sh zmodem
do cd $i
   unshar $i.sh >log
   if test -f $i.cd
      then unshar $i.cd >>log
           for j in *.d
	   do ls -l $j >>log  
	      patch `basename $j .d` $j >>log 2>&1
	      echo $j finished >>log
	   done
   fi
   rm -f *~ *.orig *.cd *.d $i.sh
   cd ..
done

echo Commands unpacked

# Remove other files that are no longer needed
rm -rf *.sh ibm/term.c Makefile ibm/Makefile simple/chgrp.c simple/cpdir.c
find . -name Makefile.68K -exec rm {} \;
cd $top/lib/posix
rm -rf [a-z]*
cd $top
rm include/amoeba.h include/amparam.h include/host_os.h fs/putc.c mm/putc.c
rm kernel/cstart.c kernel/protect1.c kernel/wini.c kernel/klib88.x 
rm kernel/mpx88.x kernel/start.x tools/menu* tools/build.c tools/bootblok*
rm test/test0.c

# Fix up ANSI vs. K&R stuff
case $1 in
kr)
	echo "Unpacking complete.  Now install files for K&R compiler"
	suffix=a
	archiver=ar
	cp include/ctype.h.kr include/ctype.h
	cp include/setjmp.h.kr include/setjmp.h
	cp include/stdio.h.kr include/stdio.h
	cp fs/Makefile.kr   fs/Makefile
	cp mm/Makefile.kr   mm/Makefile
	cp kernel/Makefile.kr kernel/Makefile
	cp tools/bootblk.s.kr tools/bootblk.s
	cp tools/monhead.s.kr tools/monhead.s
	cp tools/Makefile.kr  tools/Makefile
	cp kernel/Makefile.kr kernel/Makefile
	cp kernel/klib88.x.kr kernel/klib.x
	cp kernel/mpx88.x.kr  kernel/mpx.x
	cp kernel/rs2.x.kr   kernel/rs2.x
	cp kernel/sconst.h.kr kernel/sconst.h
	cp lib/Makefile.kr        lib/Makefile
	cp lib/other/Makefile.kr  lib/other/Makefile
	cp lib/posix/Makefile.kr  lib/posix/Makefile
	cp lib/ansi/Makefile.kr  lib/ansi/Makefile
	cp lib/curses/Makefile.kr  lib/curses/Makefile
	cp lib/syscall.kr/*    lib/syscall
	cp lib/ibm.kr/*        lib/ibm
        ;;

ansi)
	echo Unpacking complete.  Now install files for ANSI compiler
	suffix=o
	archiver=aal
	cp include/ctype.h.ansi include/ctype.h
	cp include/setjmp.h.ansi include/setjmp.h
	cp include/stdio.h.ansi include/stdio.h
	cp fs/Makefile.ansi   fs/Makefile
	cp mm/Makefile.ansi   mm/Makefile
	cp kernel/Makefile.ansi kernel/Makefile
	cp tools/bootblk.s.ansi tools/bootblk.s
	cp tools/monhead.s.ansi tools/monhead.s
	cp tools/Makefile.ansi  tools/Makefile
	cp kernel/Makefile.ansi kernel/Makefile
	cp kernel/klib88.x.ansi kernel/klib.x
	cp kernel/mpx88.x.ansi  kernel/mpx.x
	cp kernel/rs2.x.ansi   kernel/rs2.x
	cp kernel/sconst.h.ansi kernel/sconst.h
	cp lib/Makefile.ansi      lib/Makefile
	cp lib/other/Makefile.ansi  lib/other/Makefile
	cp lib/posix/Makefile.ansi  lib/posix/Makefile
	cp lib/ansi/Makefile.ansi  lib/ansi/Makefile
	cp lib/curses/Makefile.ansi  lib/curses/Makefile
	cp lib/syscall.ansi/* lib/syscall
	cp lib/ibm.ansi/* lib/ibm
	;;

esac

echo Installation completed.
