#!/bin/bash

set -e

TXZ=$(head -2 ChangeLog.txt|cut -f1 -d:|cut -f2 -d/|tail -1)
VERSION=$(echo $TXZ|cut -f2 -d-)
BUILD=2alien
echo "Building $TXZ"

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/slackpkg+-$VERSION

OUTPUT=${OUTPUT:-$TMP}

if [ repositories.txt -nt repositories.lst ];then
  echo "WARNING: repositories.txt is newer than repositories.lst"
  echo "         remember to run ./checkrepos.sh repositories.txt > repositories.lst"
  sleep 5
fi

INST=$(ls /var/log/packages/slackpkg+-$VERSION-noarch-* 2>/dev/null||true)
if [ ! -z "$INST" ];then
  BINST=$(echo $INST|sed 's/.*-//')
  NBUILD=$(echo $BUILD|sed "s/[a-z].*//")
  NBINST=$(echo $BINST|sed "s/[a-z].*//")
  if [ "$BINST" == "$BUILD" ];then
    echo "WARNING: $INST seems to be installed"
    echo "         remember to change \$VERSION or \$BUILD"
    sleep 5
  elif [ $NBUILD -le $NBINST ];then
    echo "NOTICE:  $(basename $INST) is installed"
    echo "         remember to change \$VERSION or \$BUILD"
    sleep 5
  fi
fi

SPKGPLUS_VERSION=$(grep 'SPKGPLUS_VERSION="' slackpkgplus.sh|cut -f2 -d'"')
if [ "$SPKGPLUS_VERSION" != "$VERSION" ];then
  echo "WARNING: slackpkg+ version in ChangeLog.txt ($VERSION) does not match with"
  echo "         slackpkg+ version in slackpkgplus.sh ($SPKGPLUS_VERSION)"
  echo "         please remember to update both version"
  sleep 5
fi

if [ -e ../ChangeLog.txt ];then if ! diff ../ChangeLog.txt ChangeLog.txt;then
  echo "WARNING: ChangeLog in src/ and git root does not match; please update both"
  sleep 5
fi;fi


rm -rf $PKG
mkdir -p $PKG

cd $PKG
mkdir -p etc/slackpkg/
mkdir -p usr/libexec/slackpkg/functions.d/
mkdir -p usr/doc/slackpkg+-$VERSION/
mkdir -p usr/man/man5
mkdir -p usr/man/it/man5
mkdir -p usr/man/fr/man5
mkdir -p install/

cp $CWD/slackpkgplus.sh usr/libexec/slackpkg/functions.d/
cp $CWD/README usr/doc/slackpkg+-$VERSION/
cp $CWD/CHANGES usr/doc/slackpkg+-$VERSION/
cp $CWD/ChangeLog.txt usr/doc/slackpkg+-$VERSION/
cp $CWD/repositories.txt usr/doc/slackpkg+-$VERSION/
cp $CWD/repositories.lst usr/doc/slackpkg+-$VERSION/
cp $CWD/setupmultilib.sh usr/doc/slackpkg+-$VERSION/
cp $CWD/checkrepos.sh usr/doc/slackpkg+-$VERSION/
cp $CWD/zchangelog.sh usr/libexec/slackpkg/functions.d/zchangelog.sh
cp $CWD/zlookkernel.sh usr/libexec/slackpkg/functions.d/zlookkernel.sh
cp $CWD/slackpkg+.SlackBuild usr/doc/slackpkg+-$VERSION/
cp $CWD/slackpkgplus.*.sample usr/doc/slackpkg+-$VERSION/
cp $CWD/greylist etc/slackpkg/greylist.new
cp $CWD/slack-desc install/
gzip -9c $CWD/slackpkgplus.conf.5 > usr/man/man5/slackpkgplus.conf.5.gz
gzip -9c $CWD/slackpkgplus.conf.5.it > usr/man/it/man5/slackpkgplus.conf.5.gz
gzip -9c $CWD/slackpkgplus.conf.5.fr > usr/man/fr/man5/slackpkgplus.conf.5.gz
cat $CWD/doinst.sh|sed "s/SLPVERSION/$VERSION/" > install/doinst.sh
cat $CWD/notifymsg.conf > etc/slackpkg/notifymsg.conf.new

chmod +x usr/libexec/slackpkg/functions.d/slackpkgplus.sh
chmod +x usr/libexec/slackpkg/functions.d/zchangelog.sh
chmod +x usr/libexec/slackpkg/functions.d/zlookkernel.sh
chmod +x usr/doc/slackpkg+-$VERSION/setupmultilib.sh


makepkg -l y -c y $OUTPUT/slackpkg+-$VERSION-noarch-$BUILD.txz



