#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# Before running this script
# Before running :
# 1) set MIRRORSLACKWARE line #8
# 2) if it is a local repo, sync it with an up to date remote one
# If need be modify buildrepo and SLINTREPO.
CWD=$(pwd)
SLACKVERSION="14.2"
SLINTVERSION="14.2.1"
usage() {
  printf %b "Usage: $0 stable|testing\n"
  exit
}
if [ $# -ne 1 ]; then usage; fi
if [ ! "$1" = "stable" ]; then usage; fi 
SLINTREPO=/storage/repo/x86_64/slint-$SLINTVERSION/slint
SFX=64
MIRRORSLACKWARE=/data/slackware${SFX}-$SLACKVERSION
DATESTAMP=`date +%Y%m%d`
rm -rf pkg_lists/$1/$DATESTAMP
mkdir -p pkg_lists/$1/$DATESTAMP
echo "writing the slackware list"
for SERIES in a ap d e f k kde l n t tcl x xap xfce y; do
  for i in $(find $MIRRORSLACKWARE/slackware$SFX/$SERIES/ -name "*t?z"|grep -v nosmp-sdk|grep -v kernel); do
    echo $i >> pkg_lists/$1/$DATESTAMP/slackware
  done
done
## Don't do that, the kernels are now in the slint repo, put there by
## 10-update-kernel
#echo "writing the kernels list"
#for SERIES in a d k; do
#  for i in $(find $MIRRORCURRENT/slackware$SFX/$SERIES/ -name "kernel-*t?z"|grep -e $KERNELVERSION -e $FIRMWAREVERSION); do
#    echo $i >> pkg_lists/$1/$DATESTAMP/kernels
#  done
echo "writing the slackware_locales list"
for i in $(find $MIRRORSLACKWARE/slackware$SFX/kdei/ -name "*t?z"); do
  echo $i >> pkg_lists/$1/$DATESTAMP/slackware_locales
done
for i in $(find $MIRRORSLACKWARE/extra/aspell-word-lists/ -name "*t?z"); do
  echo $i >> pkg_lists/$1/$DATESTAMP/slackware_locales
done
echo "writing the slint list"
for i in $(find $SLINTREPO -name "*t?z"|grep -v installer/| grep -v -e lxde/ -e locales/ -e mbrola-voice -e flite-voice); do
    echo $i >> pkg_lists/$1/$DATESTAMP/slint
done
#echo "writing the extra list"
#for i in $(find $SLINTREPO/extra -name "*t?z"|grep -v installer/| grep -v -e lxde/ -e locales/ -e mbrola-voice -e flite-voice); do
#    echo $i >> pkg_lists/$1/$DATESTAMP/slint
#done
echo "writing the slint locales list"
for i in $(find $SLINTREPO/locales -name "*t?z"); do
    echo $i >> pkg_lists/$1/$DATESTAMP/slint_locales
done
echo "writing the patches list"
for i in $(find $MIRRORSLACKWARE/patches/packages -name "*t?z"|grep -v nosmp-sdk); do
    echo $i >> pkg_lists/$1/$DATESTAMP/patches
done
echo "writing the flite voices list"
for i in $(cat $CWD/docs/flite_voices_included_in_the_ISO); do
    find $SLINTREPO/voices/flite -name "${i}*t?z" -exec echo '{}' >> pkg_lists/$1/$DATESTAMP/flite_voices ';'
done
echo "writing the mbrola voices list"
for i in $(cat $CWD/docs/mbrola_voices_included_in_the_ISO); do
    find $SLINTREPO/voices/mbrola -name "${i}*t?z" -exec echo '{}' >> pkg_lists/$1/$DATESTAMP/mbrola_voices ';'
done
( cd pkg_lists/$1/$DATESTAMP
  echo "***checking patches against slackware***"
  # patches in Slackware should not come from other Slackware directories.
  for i in $(cat patches); do
      SHORTNAME=$(basename $i|sed "s/-[^-]*-[^-]*-[^-]*$//")
	  if grep -wq "$SHORTNAME" slackware; then
		echo "${SHORTNAME} found in patches removed from slackware"
  	  	sed "\/${SHORTNAME}-[^-]*-[^-]*-[^-]*$d" slackware > tmpfile
		mv tmpfile slackware
	  fi
  done
  # Slint packages found in slackware should not come from Slackware
  # nor Slackware patches.
 echo "***checking slint against slackware and patches***"
  for i in $(cat slint); do
      SHORTNAME=$(basename $i|sed "s/-[^-]*-[^-]*-[^-]*$//")
	  if grep -wq "$SHORTNAME" slackware; then
	    echo "${SHORTNAME} found in slint removed from slackware"
  	  	sed "\/${SHORTNAME}-[^-]*-[^-]*-[^-]*$d" slackware > tmpfile
		mv tmpfile slackware
	  fi
	  if grep -wq "$SHORTNAME" patches; then
	  echo "${SHORTNAME} found in slint removed from patches"
  	  	sed "\/${SHORTNAME}-[^-]*-[^-]*-[^-]*$d" patches > tmpfile
		mv tmpfile patches
	  fi
  done
  echo "Consistency checks passed."
)
echo "All done."
