#!/bin/bash

############################################################################
# This script should be run inside ~/armedslack-current/source
# 
# Script : labrador
# Purpose: Retrieve the contents of the real Slackware 'tagfiles'
#          from the Slackware binary package tree and build
#          the tagfiles for your port of Slackware.
#          Packages listed in the real Slackware's tagfiles that
#          are not found in your port's tree will be excluded
#          from the newly generated tagfiles and documented in a
#          file named 'missingpackages.txt' within the series' directory.
#          Additional packages can be included by creating the
#          file:
#              source/<pkgseries>/tagfile.addpkgs 
#          The contents of such files should be in the same format
#          as the normal tagfile files.
# 13-Aug-2006:
#          This script only generates part data for the 'maketag' script:
#          the data for the 'dialog' utility is made.  To complete the
#          task, you need to run 'maketag-build' (a modified unreleased
#          Slackware script) which is run from 'sync_to_bourbon'.
#          The reason we need to generate this data is because we have
#          additional or excluded packages (not in Slackware), so 
#          we can't just copy their files.
#          The maketag-build script generates the actual shel script.
#
#          (I forgot what this script actually did and needed to re-read it
#           to understand :-) )
#          You only need to run this script when you add new packages;
#          rename or remove packages.
#          In theory it'd make more sense to modify this to make the shell
#          script versions of the maketag stuff at the same time.
#          But I just can't be bothered now :-)
#        
# Author : Stuart Winter <mozes@armedslack.org>
# Date...: 01-Jan-2006
############################################################################

# Drag in the build kit.  We need this to find out where our
# real Slackware source tree is.
source /usr/share/slackdev/buildkit.sh

# List each package series here:
PKGSERIESLIST="a ap d e f k kde kdei l n t tcl x xap y"

# Files list:
TMPFILE=/tmp/packagetag.tmp
TMPFILEMISSINGPKGS=/tmp/packagesmissing.tmp
TMPMAKETAG=/tmp/maketag.tmp

# Locations:
# $PKGSTORE is specified in a config file inside /usr/share/slackdev. It's sourced
# by buildkit.sh (above).
PORTSOURCE=$PKGSTORE/../source/ # location of ~armedslack-current/source

# Work our way through each package series:
for SERIES in $PKGSERIESLIST ; do
    if [ -d $SERIES ]; then
       ( echo -e "\n******************** Series $SERIES ********************\n"
         cd $SERIES
         rm -f $TMPFILE $TMPFILEMISSINGPKGS $TMPMAKETAG # clean these files

         # Determine if we're 'missing' certain packages from our
         # port that are listed in Slackware's series/tagfile file.
         # These packages may be missing because they are not required for
         # this port, or because they've been forgotten about!
         if [ -f $SLACKSOURCE/../slackware/$SERIES/tagfile ]; then
            grep -v ^# $SLACKSOURCE/../slackware/$SERIES/tagfile | while read PKG ; do
               THISPKG="$( echo $PKG | awk -F: '{print $1}' )"
               THISOPT="$( echo $PKG | awk -F: '{print $2}' )"
               echo "Investigating package: $THISPKG"
               echo -e "\t Install option:$THISOPT"
               # Is it in our port's slackware/ tree?
               echo -en "\t Found in ${PKGSTORE}/${SERIES}: "
               if [ -f $PKGSTORE/$SERIES/$THISPKG-[0-9]*-*.tgz ]; then
                  echo "Yes"
                  # We have ported it so add it to the tagfile:
                  echo "$THISPKG:$THISOPT" >> $TMPFILE
                  # As part of the process, we create data for the dialog script.
                  # This 'maketag' file we produce is incomplete data - we use another
                  # (unreleased) Slackware script to make the final shell script version.
                  # This process here just helps automate things.
                  # The format of these lines is as follows: 
                  # "tar" "GNU tar archive utility -- REQUIRED" "on" \
                  grep "^\"$THISPKG\"" $SLACKSOURCE/../slackware/$SERIES/maketag >> $TMPMAKETAG
                 else
                  # We didn't port it or it's missing, so we won't place it 
                  # into our new tagfile but we'll record it for later:
                  echo "$THISPKG:$THISOPT" >> $TMPFILEMISSINGPKGS
                  echo "No"
               fi
               done

            # If we are missing any packages from this series then we'll add
            # a comment at the head of the removed.packages.txt file indicating
            # why this may have happened.
            if [ -s $TMPFILEMISSINGPKGS ]; then
cat << EOF > $PKGSTORE/$SERIES/missingpackages.txt

---------------------------------------------------------------
Automatically generated: $( date )
---------------------------------------------------------------

This file lists those packages which have been excluded from this
port of Slackware.

There may be a number of reasons for this; some of which may be:

 - the package is not applicable to this architecture 

 - the package has been deleted by accident

 - the package was simply forgotten about!

In the root directory of armedslack-<version>/armedslack-notes/Removed_packages.txt
for hints about why this package may be missing.

If you think this is a problem then please email mozes@armedslack.org

Thanks!

Stuart Winter

----- List of packages excluded -----

EOF
               # Now add the list of excluded packages to that comment file:
               cat $TMPFILEMISSINGPKGS >> $PKGSTORE/$SERIES/missingpackages.txt
            fi
           
            # Do we have any additional packages for this series that are present
            # in the port but not in Slackware?  Such packages are usually architecture
            # specific (such as a/acorn-fdisk).
            echo -n "Additional packages to add to $SERIES/tagfile: "
            if [ -f $PORTSOURCE/$SERIES/tagfile.addpkgs ]; then
               echo "Yes"
               # Append these to the new tagfile:
               grep -v '^#' $PORTSOURCE/$SERIES/tagfile.addpkgs >> $TMPFILE
              else
               echo "No"
            fi

            # Do we have any additional packages that need to be added to the 
            # list that the maketag script will parse?
            # These are packages that are not architecture specific.
            echo -n "Additional packages to add to $SERIES/maketag: "
            if [ -f $PORTSOURCE/$SERIES/maketag.addpkgs ]; then
               echo "Yes"
               grep '^"' $PORTSOURCE/$SERIES/maketag.addpkgs >> $TMPMAKETAG
              else
               echo "No"
            fi

            # Put the maketag package list information into alphabetical order:
            # Once this maketag file is created, we use Patrick's maketag-build
            # script to create a real shell script version.  This file we create now
            # is data for the 'dialog' utility only.
            sort $TMPMAKETAG > $PKGSTORE/$SERIES/maketag

        #    # This isn't correct but for the moment I'm going to symlink maketag.ez to maketag.
        #    # The 'ez' (easy) tag files are all identical to their maketag counterparts
        #    # apart from in series 'a'.
        #    # So I'm going to cheat for the moment:    
        #    #( cd $PKGSTORE/$SERIES/
        #    #  rm -f maketag.ez 
        #    #  # Copy it: the scripts check for files rather than symlinks, iirc:
        #    #  cp -f maketag maketag.ez )

            # Now we'll create the new tag file inside our port's directory.
            # First we'll copy the comment stuff from the head of the real tagfile
            grep '^#' $SLACKSOURCE/../slackware/$SERIES/tagfile > $PKGSTORE/$SERIES/tagfile
            # Sort the package list into order.  If we added extra packages using the 'tagfile.addpkgs' file
            # then it's likely to be out of order.  I don't know if the installer cares about order
            # (or whether it sorts it itself), but it looks nice if it's already ordered anyway.
            sort $TMPFILE >> $PKGSTORE/$SERIES/tagfile
         else
            # We didn't find a tagfile in the real Slackware source.  Your source tree
            # is broken?
            echo "Unable to find Slackware tagfile: $SLACKSOURCE/../slackware/$SERIES/tagfile"
            echo "This is unusual - there should be one there."
            echo "*** Aborted for series $SERIES ***"
          fi )
      else
        # Unable to find the series directory?
        echo "Could not find the series directory: $SERIES"
   fi
done
