#!/bin/bash
#
# Copyright (C) 1999 Florian La Roche
#
# This script compiles the jurix source packages into binaries.
# - environment JURIX will change between libc5/glibc version
# - environment CHROOT may contain a path for the chroot-environment
# - param "--time" will recompile the oldest package first
#
# search for XXX to find the places where you might want to adjust things...
#
# TODO:
# - --time currently doesn't work, fix it
# - fast complete recompile: compile all base packages first
#   and then all ADDPACKAGE
#
# "-mieee" should be used?
# "-fpermissive" might be needed for gcc 2.95 and C++ progs
#
# You might search the logfiles for the following strings:
# "mt" "gnuplot" "libtool" "dump"
# "File format not recognized"  (error from "strip")
# "^cp: ", "-rpath", "-m[3456]86", "-fomit-frame-pointer",
# "-O[013-9]", "-O ", "warn-common", "pthread.h", "Error", "***"
# "-malign-loops", "-malign-jumps", "-malign-functions"
# "-ansi", "-funroll-loops", "-fno-force-mem", "-fno-gcse"
# "-liberty", "libiberty.a"
# "--with-included-gettext"


# JURIX can either be "libc5" or "glibc"
if test -z "$JURIX" ; then
	JURIX=glibc
fi
JURIXARCH=`arch | sed 's/i[3456]86/i386/g'`
export JURIX JURIXARCH

# source tree of jurix Linux
SOURCE=/local/ftp/pub/jurix/source/chroot
# we might define another chroot path in the environment
if test -z "$CHROOT" ; then
	# we can build a chroot-environment here
	CHROOT=/hda7/chroot.$JURIX
fi
FILELIST=$SOURCE/../filelist

FORCE=0

# exit with an error message
error()
{
	echo "Error in $1"
	exit 1
}
# output the standard packages that are unpacked in the chroot environment
std_packages()
{
	#echo "package default"
	for i in aaa_base base bash compress db devs file fileutil find gawk \
		gdbm gppshare groff gzip kernel less locale \
		netkita netkitb ncurses net_tool netcfg ps sh_utils shadow \
		shlibs sysvinit termcap texinfo textutil timezone util vim ; do
		echo "package base/$i.jur"
	done
	test $JURIX = libc5 && echo "package base/kernel22.jur"
	test $JURIX = glibc -a $JURIXARCH = i386 && \
		echo "package base/libc5.jur"
	test $JURIXARCH = i386 && echo "package base/ldso.jur"
	for i in bc ed jurix perl sharutil ssl ; do
		echo "package appl/$i.jur"; done
	for i in bzip2 ; do echo "package archiver/$i.jur"; done
	for i in lpr pgp ; do echo "package network/$i.jur"; done
	for i in mail sendmail ; do echo "package mail/$i.jur"; done
	for i in autoconf automake binutils bison diff flex gcc gpp \
		gettext libc libgpp linclude make patch rcs slang zlib; do
		echo "package develop/$i.jur"; done
	test $JURIXARCH = i386 && echo "package develop/bin86.jur"
	test $JURIX = libc5    && echo "package develop/linc22.jur"
	for i in lesstif xdevel xf86 xpm xshared ; do
		echo "package x/$i.jur"; done
	for i in libpng jpeg ; do echo "package xappl/$i.jur"; done
	echo "package news/inn.jur"
	for i in tcl dejagnu ; do echo "package tcl/$i.jur"; done
}
# output a config file to extract all packages
jurix_config()
{
	echo "start_directory $STARTDIR/"
	echo "nopackagelistcheck"
	# XXX the following makes porting to a new system
	#     with different /etc/{passwd,group} easier
	#echo "nocheckugids"
	std_packages
	test -n "$ADDPACKAGE" && for i in "" $ADDPACKAGE ; do
		echo "package $i.jur"
	done
}
# create a chroot-environment with the needed packages
make_chroot()
{
	umount -t proc `pwd`/proc 2> /dev/null
	rm -fr var/jurix
	mkdir -p var/jurix/{packages,unchanged}
	jurix_config > var/jurix/config
	jur-unpack -x -x -x -x -d 0
	mkdir -p {proc,var/lib/packages}
	mount -t proc `pwd`/proc `pwd`/proc || error mount
	chroot . ./sbin/ldconfig
	test -x usr/bin/texhash && chroot . ./usr/bin/texhash
}
compile_package()
{
	test -e NOT -o -e NOT.$JURIX -o -e NOT.$JURIXARCH && return
	COMPILE=`ls *.dif 2>/dev/null | sed 's/.dif$//'`
	ADDPACKAGE=
	test -f SPEC && . SPEC
	DOCOMPILE=1
	test $FORCE = 0 -a -f $LOGFILE && {
		DOCOMPILE=0
		for i in * ; do
			test $i -nt $LOGFILE && DOCOMPILE=1
		done
		for i in $PACKAGES ; do
			i="${i##*/}"
			if test -f $FILELIST/$i.$JURIXARCH ; then
				test $FILELIST/$i.$JURIXARCH -nt $LOGFILE \
					&& DOCOMPILE=1
			elif test -f $FILELIST/$i.$JURIX ; then
				test $FILELIST/$i.$JURIX -nt $LOGFILE \
					&& DOCOMPILE=1
			else
				test $FILELIST/$i -nt $LOGFILE \
					&& DOCOMPILE=1
			fi
		done
	}
	test $DOCOMPILE = 1 || return
	echo "COMPILE PACKAGE: $PACKAGE"
	rm -fr $CHROOT/src
	mkdir -p $CHROOT/src
	cp * $CHROOT/src/
	TMPLOGFILE=`mktemp /tmp/jurix.XXXXXX` || error no-tmp-file
	chmod 644 $TMPLOGFILE
	(cd $CHROOT
	make_chroot
	for i in $PACKAGES ; do
		i="${i##*/}"
		if test -f $FILELIST/$i.$JURIXARCH ; then
			cp $FILELIST/$i.$JURIXARCH var/lib/packages/$i
		elif test -f $FILELIST/$i.$JURIX ; then
			cp $FILELIST/$i.$JURIX var/lib/packages/$i
		else
			cp $FILELIST/$i var/lib/packages/
		fi
	done
	cd src
	( echo "set -e"
	echo "cd /src"
	for i in *.sh ; do
		test -f "$i" && echo "sh $i"; done
	test -n "$COMPILE" && for i in $COMPILE ; do
		echo "if test -f $i.tar.gz ; then tar xzf $i.tar.gz"
		echo "  else tar xIf $i.tar.bz2; fi"
		echo "cd $i; patch -s -p0 < ../$i.dif; cd .."
		echo "make compile -f Makefile.Linux -C $i < /dev/null"
		echo "grep '^check:' $i/Makefile.Linux > /dev/null && make check -f Makefile.Linux -C $i || :"
		echo "make install -f Makefile.Linux -C $i"
	done
	echo "Check > /dev/null"
	for j in $PACKAGES ; do
		k="${j##*/}"
		echo "p /var/lib/packages/$k"
		echo "mkdir new ; cd new ; tar xzplf ../$k.tgz"
		echo "test -f install/doinst.sh && { \\"
		echo "        sh install/doinst.sh; rm -fr install; }"
		echo "jur-pack -d . ../$k.jur"
		echo "cd .. ;  rm -fr new"
	done
	) | chroot .. /bin/bash || echo failed-in-chroot
	for j in $PACKAGES ; do
		k="${j##*/}"
		cp $k.tgz $STARTDI/$j.tgz  || error cp-tgz
		sleep 2			# XXX for better timestamps
		cp $k.jur $STARTDIR/$j.jur || error cp
	done
	(cd $STARTDIR && for i in */*.jur ; do echo $i; done | jur-index) || error jur-index
	cd ..
	test -d var/jurix || error wrong-dir	# sanity check
	jurix_config > var/jurix/config
	for j in $PACKAGES ; do
		echo "package $j.jur"
	done >> var/jurix/config
	rm -fr var/lib/packages usr/info/dir usr/X11R6/info/dir
	echo ------------------------------------------------
	jur-unpack -d 30 -x -x -x -x
	) < /dev/null > $TMPLOGFILE 2>&1
	if test $? -eq 0 ; then
		mkdir -p `dirname $LOGFILE`
		cp $TMPLOGFILE $LOGFILE
		rm -f $TMPLOGFILE
	else
		echo did-not-compile
	fi
}
check_if_compile()
{
	# sanity checks
	test -d $STARTDI   || error "no startdir1"
	test -d $STARTDIR  || error "no startdir"
	test -d $SOURCE    || error "no sourcedir"
	test -d "$PACKAGE" || return
	PACKAGES=$PACKAGE
	LOGFILE=$STARTDI/logs/$PACKAGES
	cd $PACKAGE
	compile_package
	cd ../..
}

# paths for the jurix binary distribution where new binaries are copied to
# XXX do not use hardcoded paths
if test $JURIX = libc5 ; then
	STARTDI=/local/ftp/pub/jurix/binary/tar
	STARTDIR=/hdc1/jurix/libc5
else
	STARTDI=/local/ftp/pub/jurix/$JURIXARCH/tar
	STARTDIR=/hdc1/jurix/$JURIX
	if test $JURIXARCH = alpha ; then
		STARTDI=/jurix/tar
		STARTDIR=/jurix/jur
	fi
fi

cd $SOURCE
if test "X$1" = X--time ; then
	# first compile any not-yet compiled packages
	for PACKAGE in */* ; do
		check_if_compile
	done
	# re-compile oldest package first
	FORCE=1
	while : ; do
		PACKAGE="`ls -tr */*/LOG.$JURIX | head -1`"
		PACKAGE=`dirname $PACKAGE`
		check_if_compile
	done
elif test "X$1" = X--base ; then
	KK=`std_packages | sed -e 's/^package //' -e 's/\.jur//g'`
	#KK="$KK `cat */*/SPEC | grep ADDPACK | sort -u`"
	# recompile KK
	echo $KK
else
	for PACKAGE in ${1:-*/*} ; do
		check_if_compile
	done
fi
echo Finished.
