#!/bin/sh -
#
# This script builds _and checks_ all the distribution files from my source
# directory. It's very selective because I've got a lot of historical and
# other 'junk' in the same directory. (85Mb!)
#

trap "exit 1" 1 2 3 15

DIR="`pwd`"
TMPDIR=/tmp/Linux-86
TMPSRC=linux86
ARCDIR="$DIR"/dev86arc

SRCDIRS='bcc unproto as ar ld copt man elksemu dis88 tests libbsd bin86'
DISTFILES='Libc_version Makefile README COPYING MAGIC Changes Contributors
           mkcompile mkcompile2 later.c GNUmakefile libcompat
	   ifdef.c makefile.in Mk_dist'

TMPDIST=$TMPDIR/$TMPSRC
rm -rf ${TMPDIR}
mkdir -p ${TMPDIST}

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

echo Checking version

make -s -C libc Libc_version
make -s -C bin86 ungrab
VER=`cat Libc_version`

echo Copying most program files.
cp -a $DISTFILES $SRCDIRS ${TMPDIST}

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

echo Copying libc.

LIBC_FILES='Makefile Make.defs crt0.c
      	    README COPYING KERNEL
      	    New_subdir Pre_main Config_sh Config.dflt'

mkdir ${TMPDIST}/libc

(
  cd libc
  LIBC_DIRS="`for i in */Makefile */Config; do dirname $i; done | sort -u`"
  cp -a $LIBC_FILES include $LIBC_DIRS ${TMPDIST}/libc/.
)

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

echo Copying bootblocks and doselks.
make -s -C bootblocks distribution
make -s -C doselks distribution

mkdir ${TMPDIST}/bootblocks
( cd ${TMPDIST}/bootblocks ; tar xzf /tmp/bootblocks.tar.gz )

mkdir ${TMPDIST}/doselks
( cd ${TMPDIST}/doselks  ; tar xzf /tmp/doselks.tar.gz )

[ "`id -un`" = "root" ] && {
  chown -R root:root ${TMPDIST}
  chmod -R og=u-w ${TMPDIST}
}

rm -f /tmp/bootblocks.tar.gz /tmp/doselks.tar.gz /tmp/libc-8086-$VER.tar.gz

echo Extracting previous version
rm -f $ARCDIR/Dev86src-$VER.tar.gz

mkdir ${TMPDIST}.tmp
( cd ${TMPDIST}.tmp
  tar xzf `ls -tr $ARCDIR/Dev86src*.0.tar.gz | tail -1`
  mv * ${TMPDIST}.old
)
rmdir ${TMPDIST}.tmp

# ARCDIR=${TMPDIR}/arc ; mkdir -p ${ARCDIR}
cd ${TMPDIST}
echo COPY COMPLETE -- Switched to ${TMPDIST}, ARCDIR now $ARCDIR
echo

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

echo 'Ensuring clean tree'

cd ${TMPDIST}
  make -s clean realclean
  mv as/obj1 ${TMPDIST}-obj1
  EXCL="`find .         -name '*.o'    -o -name '*.obj'	\
                     -o -name '*.lib'  -o -name '*.bak'	\
		     -o -name '*~'     -o -name '*.exe'	\
		     -o -name '*.orig' -o -name '*.rej'	\
		     `"
  mv ${TMPDIST}-obj1 as/obj1
  rm -f $EXCL
  rm -f `find . -type l`

echo Generating patch against previous .0 version.

cd ${TMPDIR}

mv ${TMPSRC}.old/bootblocks boot.old
mv ${TMPSRC}/bootblocks boot

diff -Nurd ${TMPSRC}.old ${TMPSRC} > ${ARCDIR}/Dev86src-$VER.patch

mv boot.old ${TMPSRC}.old/bootblocks
mv boot ${TMPSRC}/bootblocks

diff -Nurd ${TMPSRC}.old/bootblocks ${TMPSRC}/bootblocks >> ${ARCDIR}/Dev86src-$VER.patch

gzip -f9 ${ARCDIR}/Dev86src-$VER.patch

echo Creating full source archive.
ln -s ${TMPSRC} dev86-$VER
tar cf ${ARCDIR}/Dev86src-$VER.tar dev86-$VER/*
gzip -f9 ${ARCDIR}/Dev86src-$VER.tar

echo Creating as86 source archive.
ln -s ${TMPSRC}/as as86-$VER
cp -p ${TMPSRC}/man/as86.1 as86-$VER/as86.1
cp -p ${TMPSRC}/COPYING as86-$VER/COPYING
tar cf ${ARCDIR}/as86-$VER.tar `find as86-$VER/* -prune -type f`
gzip -f9 ${ARCDIR}/as86-*.tar

echo Creating bin86 source archive.
make -s -C ${TMPSRC}/bin86 grab
ln -s ${TMPSRC}/bin86 bin86-$VER
tar chf ${ARCDIR}/bin86-$VER.tar bin86-$VER
make -s -C ${TMPSRC}/bin86 ungrab
gzip -f9 ${ARCDIR}/bin86-*.tar

###################################################################

echo Compile up the binaries.

cd ${TMPDIR} || exit 1
mkdir -p ${TMPDIST}.ins
make -C ${TMPDIST} install ARFLAGS=q DIST=${TMPDIST}.ins ELKSSRC=/dev/null || 
    exit
make -C ${TMPDIST} other || exit
tar cf ${ARCDIR}/Dev86bin-$VER.tar -C ${TMPDIST}.ins .
rm -f ${ARCDIR}/Dev86clb-$VER.zip Bcc
ln -s ${TMPDIST} Bcc

zip -9rpk ${ARCDIR}/Dev86clb-$VER.zip \
 Bcc/lib/crt0.o Bcc/lib/libc.a Bcc/lib/libbsd.a \
 Bcc/lib/libdos.a Bcc/lib/libc_f.a Bcc/lib/libc_s.a \
 Bcc/lib/i386/crt0.o Bcc/lib/i386/libc.a  || exit
rm Bcc

gzip -9f ${ARCDIR}/Dev86bin-$VER.tar || exit

echo Process completed.
