#!/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
}
if [ $# -ne 1 ]; then usage; fi
if [ $1 -ne 32 ] && [ $1 -ne 64 ]; then usage; fi
if [ $1 -eq 32 ]; then
	MIRRORSALIX=/storage/salix/i486/$SLACKVERSION/salix
	MIRROREXTRA=/storage/salix/i486/extra-$SLACKVERSION/salix
else
	MIRRORSALIX=/storage/salix/i486/$SLACKVERSION/salix
	MIRROREXTRA=/storage/salix/x86_64/extra-$SLACKVERSION/salix
fi
SFX=$(echo $1|grep 64)
SLINTREPO=../..
MIRRORSLACKWARE=/storage/slackware${SFX}-$SLACKVERSION
DATESTAMP=`date +%Y%m%d`
#if [ ! $UID -eq 0 ]; then
#  printf "%b"  "Please execute this script as root.\n"
#  exit
#fi
ISO=/tmp/iso$SFX
rm -rf $ISO
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/
for i in $(<pkg_lists/$1/$DATESTAMP/slackware); do
  cp $i $ISO/slint
done
# packages under kdei/ and extra/aspell-word-lists/
for i in $(<pkg_lists/$1/$DATESTAMP/slackware_locales); do
  cp $i $ISO/slint/locales
done
printf "%b" "Remove the obsolete packages...\n"
# packages replaced by those in patches/
for i in $(<pkg_lists/$1/$DATESTAMP/patches); do
	FILE=$(basename $i|sed "s/[^-]*-[^-]*-[^-]*$//")
	find $ISO/slint -name ${FILE}*t?z -exec rm -f '{}' \;
done
# packages in patches/
printf "%b" "Install the upgrades...\n"
for i in $(<pkg_lists/$1/$DATESTAMP/patches); do
	cp $i $ISO/slint
done
printf "%b" "Remove the replaced packages...\n"
# The replaced packages are those found in /slint and some in /salix
# They are not all shipped in Slackware but the script won't complain
for i in $(<pkg_lists/$1/$DATESTAMP/replaced); do
	FILE=$(basename $i|sed "s/[^-]*-[^-]*-[^-]*$//")
	find $ISO/slint -name ${FILE}*t?z -exec rm -f '{}' \;
done
# Packages from the Salix repository
printf "%b" "Install the Salix packages...\n"
for i in $(<pkg_lists/$1/$DATESTAMP/salix); do
	cp $i $ISO/slint
	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"
for i in $(<pkg_lists/$1/$DATESTAMP/salix_locales); do
  cp $i $ISO/slint/locales
	j=$(echo $i|sed "s/\.t.z/.dep/")
	if [ -r $j ]; then
    	cp $j $ISO/slint
	fi
done
printf "%b" "Install the extra packages...\n"
for i in $(<pkg_lists/$1/$DATESTAMP/salix_extra); do
	cp $i $ISO/slint
	j=$(echo $i|sed "s/\.t.z/.dep/")
	if [ -r $j ]; then
    	cp $j $ISO/slint
	fi
done
printf "%b" "Install the Slint packages...\n"
# Grab the packages from the Slint repository
cp -r $SLINTREPO/slint/*.t?z $ISO/slint
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."
