#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# Before running this script, adapt MIRROR* and SLINTREPO below to your
# installation.
#
CWD=$(pwd)
SLACKVERSION="14.2"
SLINTVERSION="14.2"
usage() {
  printf %b "Usage: $0 32|64\n"
  exit
}
ARCH=$(uname -m)
if ! echo $ARCH | grep -q 64; then
	OLD="/old"
    echo "OLD=$OLD"
fi
MIRROR=http://slackware.uk/salix
if [ $# -ne 1 ]; then usage; fi
if [ $1 -ne 32 ] && [ $1 -ne 64 ]; then usage; fi
if [ $1 -eq 32 ]; then
    SLINTREPO=$OLD/home/didier/Slint/repo/i586/slint-$SLINTVERSION
	MIRROREXTRA=$MIRROR/i486/extra-$SLACKVERSION
else
    SLINTREPO=$OLD/home/didier/Slint/repo/x86_64/slint-$SLINTVERSION
	MIRROREXTRA=$MIRROR/x86_64/extra-$SLACKVERSION
fi
SFX=$(echo $1|grep 64)
MIRRORSLACKWARE=/storage/slackware${SFX}-$SLACKVERSION
DATESTAMP=`date -u +%Y%m%d`
LOG=log$SFX$DATESTAMP
ISO=/tmp/iso$SFX
rm -rf $ISO
rm -f $LOG
mkdir -p $ISO/slint/locales
# First step install the packages and write the metadata
# ======================================================
#
printf "%b" "Install the Slackware packages...\n"
# packages under slackware/ but in kdei/
echo "*** Slackware ***" >> $LOG
for i in $(<pkg_lists/$1/$DATESTAMP/slackware); do
  cp $i $ISO/slint
  echo $(basename $i) >> $LOG
done
# packages under kdei/ and extra/aspell-word-lists/
echo "*** Slackware locales and word lists ***" >> $LOG
for i in $(<pkg_lists/$1/$DATESTAMP/slackware_locales); do
  cp $i $ISO/slint/locales
  echo $(basename $i) >> $LOG
done
printf "%b" "Remove the obsolete packages...\n"
echo "*** Patched to remove ***" >> $LOG
# packages replaced by those in patches/
for i in $(<pkg_lists/$1/$DATESTAMP/patches); do
	PKGNAME=$(basename $i|sed "s/-[^-]*-[^-]*-[^-]*$//")
    REMOVE=$(sed -n "/PACKAGE NAME/{/ ${PKGNAME}-[[:digit:]]/s/.* //p}" $MIRRORSLACKWARE/PACKAGES.TXT)
    rm $ISO/slint/$REMOVE
    echo "$REMOVE replaced by $i" >> $LOG
done
# packages in patches/
printf "%b" "Install the upgrades...\n"
echo "*** Patches ***" >> $LOG
for i in $(<pkg_lists/$1/$DATESTAMP/patches); do
	cp $i $ISO/slint
    echo $(basename $i) >> $LOG
done
printf "%b" "Remove packages from Slackware...\n"
echo "*** Packages from Slackware to remove ***" >> $LOG
# The replaced packages are those found in /slint and some in /salix
# Here is the current state:
cd $ISO/slint
	CURRENT=$(mktemp)
	ls -1 > $CURRENT
cd $CWD
echo "CURRENT is  $CURRENT"
for i in $(<pkg_lists/$1/$DATESTAMP/replaced); do
    echo $i
    REMOVE=$(sed -n /^${i}-[[:digit:]]/p $CURRENT)
    echo $REMOVE
	if [ ! "$REMOVE" = "" ]; then
    	rm $ISO/slint/$REMOVE
    	echo "$REMOVE replaced by $i" >> $LOG
	else
		echo "$i not found, not removed" >> $LOG   
	fi
done
rm -f $CURRENT
# Packages from the Salix repository
printf "%b" "Install the Salix packages...\n"
echo "*** Salix ***" >> $LOG
for i in $(<pkg_lists/$1/$DATESTAMP/salix); do
	cp $i $ISO/slint
    echo $(basename $i) >> $LOG
	j=$(echo $i|sed "s/\.t.z/.dep/")
	if [ -r $j ]; then
    	cp $j $ISO/slint
	fi
done
printf "%b" "Install the Salix locales...\n"
echo "*** Salix locales ***" >> $LOG
for i in $(<pkg_lists/$1/$DATESTAMP/salix_locales); do
    cp $i $ISO/slint/locales
    echo $(basename $i) >> $LOG
	j=$(echo $i|sed "s/\.t.z/.dep/")
	if [ -r $j ]; then
    	cp $j $ISO/slint
	fi
done
(cd $ISO/slint
printf "%b" "Install the extra packages...\n"
echo "*** Extra ***" >> $LOG
for i in $(<$CWD/pkg_lists/$1/$DATESTAMP/salix_extra); do
    wget -a $CWD/log -nv $MIRROREXTRA/$i
    echo $(basename $i) >> $LOG	   
	j=$(echo $i|sed "s/\.t.z/.dep/")
	wget -a $CWD/log -nv $MIRROREXTRA/$j 2>/dev/null
done
)
printf "%b" "Install the Slint packages...\n"
echo "*** Slint ***" >> $LOG
# Grab the packages from the Slint repository
for i in $SLINTREPO/slint/*.t?z; do
	cp $i $ISO/slint
    echo $(basename $i) >> $LOG
done
cp -r $SLINTREPO/slint/*.dep $ISO/slint
cp $SLINTREPO/slint/DONOTREMOVEME.TXT $ISO/slint
printf "%b" "Write the metadata. This can take a while...\n"
# Write the metadata
(cd $ISO
printf "%b" "Running metagen all...\n"
$CWD/metagen.sh all
printf "%b" "Running metagen clean...\n"
$CWD/metagen.sh clean
printf "%b" "Running metagen md5...\n"
$CWD/metagen.sh md5
)
echo "All done."
