#!/bin/bash
# Slackware distribution script
# Copyright 2003, 2004 Stefano Falsetto <falsetto@gnu.org>
# Copyright 2008, 2010 David Egan Evans <sinuhe@gnu.org>
#
# This program is free software.  You can redistribute it, or modify it,
# or both, under the terms of the GNU General Public License version 3
# (or any later version) as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses>.
#

if test $UID != 0
then
	echo "You must be root!"
	exit
fi

if test $# -lt 2
then
	echo 'Missing owner and group arguments!'
	exit
else
	OWNER="$1"
	GROUP="$2"
fi

. ./VERSION

PRG="src/rottlog src/log2rot"
DOC="COPYING AUTHORS NEWS ChangeLog README INSTALL TODO VERSION"
RC="rc/rc rc/weekly"
#MANUALS="$(cat FILES|grep "^man/"|grep -v "texinfo"|cut -d'/' -f2|while read a; do echo -n "$a "; done)"
#MANSECTS="$(cat FILES|grep "^man/"|grep -v "texinfo"|cut -d'/' -f2|cut -d'.' -f2|sort|uniq|tr '\n' ' ')"
SROOT="tmp/Slackware"
PROGRAM="rottlog-$VERSION"
PRJDIR="$PWD/tar/Slackware"

rm -Rf "$SROOT"

echo "Making Slackware package..."
mkdir -p $PRJDIR
mkdir -p $SROOT/etc/rottlog
chown root.root $SROOT/etc/rottlog
chmod 0700 $SROOT/etc/rottlog

#mkdir -p $SROOT/usr/doc/$PROGRAM
mkdir -p $SROOT/usr/doc/$PROGRAM/rc
mkdir $SROOT/usr/sbin
mkdir -p $SROOT/var/lib/rottlog
chown root.root $SROOT/var/lib/rottlog
chmod 0700 $SROOT/var/lib/rottlog
mkdir -p $SROOT/usr/info

#for sect in $MANSECTS; do
#  mkdir -p $SROOT/usr/man/man${sect}
#  for i in man/*.$sect; do
#    bi=$(basename $i)
#    if [ $(expr "$MANUALS" : ".*$bi.*") -ne 0 ]; then
#      cp man/$bi $SROOT/usr/man/man${sect}
#    fi
#  done
#done

cp $PRG $SROOT/usr/sbin
cp $DOC $SROOT/usr/doc/$PROGRAM
cp $RC $SROOT/usr/doc/$PROGRAM/rc
chmod -R +r $SROOT/usr/doc/$PROGRAM

for INFOFILE in ./doc/*.info
do
	cp $INFOFILE $SROOT/usr/info
done

for i in rottlog log2rot; do
	chmod 755 $SROOT/usr/sbin/$i
	chown root.root $SROOT/usr/sbin/$i
done

mkdir -p $SROOT/install
cat > $SROOT/install/slack-desc <<EOF
rottlog: rottlog (a log management tool)
rottlog: 
rottlog: GNU Rot[t]Log v.$VERSION
rottlog: The GNU log management tool: archive and compress system logs.
rottlog: 
rottlog: 
rottlog: 
rottlog: 
rottlog: 
rottlog: 
rottlog: 
EOF

cat > $SROOT/install/doinst.sh <<EOF
echo
cat install/slack-desc

sed '/PACKAGE DESCRIPTION/r /install/slack-desc' < /var/log/packages/$PROGRAM >slack-desc.tmp
mv slack-desc.tmp /var/log/packages/$PROGRAM
rm install/slack-desc

EOF

cd $SROOT
makepkg -l y -c n $PRJDIR/$PROGRAM.txz
if test $? -ne 0
then
	echo "An error occurred during package creation!"
	exit 1;
fi
chown $OWNER.$GROUP $PRJDIR/$PROGRAM.txz
cd -
rm -Rf $SROOT
echo "Slackware package creation finished."

