#!/bin/sh

#
# Script for building GNOKII package
# Written by Pawe Kot <pkot@linuxnews.pl>
#

PN=gnokii
PV=0.3.4

ARCH="i386"
BUILD=1

DOCS1="Docs/*"
DOCS2="Docs/protocol Docs/sample"
CONF="Docs/sample/gnokiirc"

CWD=`pwd`

# There will be the sources placed
if [ "$TMP" = "" ]; then
	TMP=/tmp
fi
if [ ! -d $TMP ]; then
	mkdir -p $TMP
fi

# The package will be built there
PKG=$TMP/package-$PN-$PV
if [ ! -d $PKG ]; then
	mkdir -p $PKG
fi

# Untar the sources
cd $TMP
tar xzvf $CWD/$PN-$PV.tar.gz
cd $PN-$PV

# Configure and make package
./configure --prefix=/usr --with-xgnokiidir=/usr/X11R6 i386-slackware-linux
make
make prefix=$PKG/usr xgnokii_libdir=$PKG/usr/X11R6 install

# Copy the docs
mkdir -p $PKG/usr/doc/$PN-$PV
cp $DOCS1 $PKG/usr/doc/$PN-$PV
cp -r $DOCS2 $PKG/usr/doc/$PN-$PV

# and the configuration
mkdir -p $PKG/etc
sed "s|/usr/local/sbin|/usr/sbin|" < $CONF > $PKG/etc/gnokiirc.new

# Now, add gnokii group, set the permissions
GROUPADD=`which groupadd`
cd $PKG
mkdir install
cat > install/doinst.sh <<EOF
#!/bin/sh

$GROUPADD gnokii >/dev/null 2>&1
chown root.gnokii /usr/bin/gnokii
chmod 750 /usr/bin/gnokii
chown root.gnokii /usr/sbin/gnokiid
chmod 750 /usr/sbin/gnokiid
chown root.gnokii /usr/sbin/mgnokiidev
chmod 4750 /usr/sbin/mgnokiidev

if [ -f /etc/gnokiirc ]; then
	rm -f /etc/gnokiirc.new
else
	mv /etc/gnokiirc.new /etc/gnokiirc
fi
EOF

# and build the package answering "yes" twice
echo "y
y" | makepkg $TMP/$PN-$PV-$ARCH-$BUILD.tgz

# Cleanup
rm -rf $TMP/$PN-$PV
rm -rf $PKG
