#!/bin/bash

shopt -s extglob # needed to find package names

#   echo "Syntax: slackmp [-p]"
# is prepend symlink creation code.

if [ -z "$SLACKPACKAGE" ]; then
   echo "Variable SLACKPACKAGE is not set"
   exit 1
fi

if [ -z "$PKGSERIES" ]; then
   echo "Variable PKGSERIES is not set"
   exit 1
fi
if [ -z "$PKGSTORE" ]; then
   echo "Variable PKGSTORE is unset"
   exit 1
fi

# If the PKGSERIES is set to 'extra', unset it because
# the PKGSTORE is already correctly set by the build kit.
[ "$PKGSERIES" = "extra" ] && unset PKGSERIES

# Delete old package from the t?z stash?
if [ "${DELETEOLDPKGS}" = "Yes" ]; then
   echo "Removing old versions of $PKGNAM from $PKGSTORE/$PKGSERIES"
   rm -fv $PKGSTORE/$PKGSERIES/$PKGNAM-+([^-])-+([^-])-+([^-]).{t?z,txt,asc}
fi

echo "Running makepkg $1 -l y -c n $PKGSTORE/$PKGSERIES/$SLACKPACKAGE"
mkdir -vpm755 $PKGSTORE/$PKGSERIES
makepkg $1 -l y -c n $PKGSTORE/$PKGSERIES/$SLACKPACKAGE

if [ -s install/slack-desc ]; then
   echo "Installing package description file..."
   grep -Ev '^($|#| *\|)' install/slack-desc > $PKGSTORE/$PKGSERIES/${SLACKPACKAGE%.*}.txt
 else
   echo "ERROR: install/slack-desc is missing for this package!"
fi

if [ "$PKGSERIES" = "/" ]; then
    echo "**** YOUR PACKAGE NEEDS TO BE MANUALLY MOVED FROM $PKGSTORE ****"
 fi
