#!/bin/sh

NO_AUTO_BACKUP=x
. ./scripts/parse-config

echo -n "Testing for perl ..."
if ! perl -e 'print " ok.\n"'
then
	echo 'ERROR !'
	echo
	echo "The ./scripts/Puzzle script needs Perl5."
	echo
	exit 1
fi

echo "Create Documentation/{FAQ,LSM} ..."
#
cd Documentation/Developers/FAQ.src
./buildit.sh > ../../FAQ
rockver="`echo $rockver | sed 's,-DEV,,'`"
cat > ../../LSM << EOT
Begin3
Title:          ROCK Linux
Version:        $rockver
Entered-date:   `date`
Description:    ROCK Linux is a Linux distribution designed for highly skilled
                Linux Users and Administrators. There is no configuration
                utility - the prefered way of system administration in ROCK
                Linux is to edit the config files directly. ROCK Linux is
                built by some shell scripts. These scripts can download all
                the source from the internet, (cross-)compile them, build the
                package files and create a CD-ROM image.
                ROCK Linux is a small Linux Distribution, but is't not a "mini
                distribution". It comes with over 400 Packages including X11
                and the GNOME Desktop.
                The ROCK Linux Homepage is http://www.rocklinux.org/.
Keywords:       Distribution Linux Admin
Author:         god@clifford.at (Clifford Wolf)
Primary-site:   gd.tuwien.ac.at opsys/linux/ROCK/
Platforms:      ix86 PCs, Alpha, PPC
Copying-policy: GPL
End
EOT

echo "Parsing cpp files in arch-conf/ ..."
#
cd $base/arch-conf ; ./cleanup.sh
for x in */*.cpp ; do
	echo "Parsing $x"
	{ echo -en "#\n# This file is auto-generated by $0. Make your "
	echo -e "modifications\n# in arch-conf/$x and re-run $0.\n#"
	#cpp -I`dirname $x` -Ishare $x | egrep -v '^# |^#?$' | \
	$base/scripts/includer.pl -I`dirname $x` -Ishare $x |
	sed 's,^#,# ,' ; } > `echo $x | sed 's,\.cpp$,,'`
done

echo "Running build scripts in arch-conf/ ..."
#
for x in */buildit.sh ; do sh $x || exit 1 ; done

cd $base
echo "Creating scripts/package and pkg-archive/INDEX ..."
#

cat << 'EOT' > pkg-archive/INDEX 
# This directory has to contain the package sources. They don't come with the
# ROCK Linux sourcecode distribution - so you need to download them. To do so
# just use the command './scripts/Download -base'. To check if all packages
# are downloaded correctly enter './scripts/Download -check -base'.
#
# Type './scripts/Help Download' for details.
#
# This file is autogenerated from the .desc files

EOT

(for x in  pkg-config/*/*.desc ; do
	pri=`grep "^\[P\] " $x | sed "s,\[P\] ,,"`
	pkg=${x%.desc} ; pkg=`basename $pkg`
	ver=`grep "^\[V\] " $x | sed "s,\[V\] ,,"`
	
	grep "^\[D\] " $x | cut -d ' ' -f 3,4 >> pkg-archive/INDEX
	
	echo "$pri $pkg $ver"
done) | sort +3 +4 > scripts/packages

echo "Reformating pkg-archive/INDEX ..."
#
for x in pkg-archive ; do
	grep -v '^###' $x/INDEX | \
		perl -w ./scripts/reformat-index.pl > $x/INDEX-x
	mv $x/INDEX-x $x/INDEX
done

echo "Creating opt-archive/INDEX ... "
cat << 'EOT' > opt-archive/INDEX 
# This directory has to contain the package sources. They don't come with the
# ROCK Linux sourcecode distribution - so you need to download them. To do so
# just use the command './scripts/Download -base'. To check if all packages
# are downloaded correctly enter './scripts/Download -check -base'.
#
# Type './scripts/Help Download' for details.
#
# This file is autogenerated from the .desc files

EOT
cat scripts/unsorted.pz >> opt-archive/INDEX
