#!/bin/sh

########################
# NOTES:
# 
########################
# Binkd Fido Mailer pakage for Slamd64 
# Download latest verison of and remame in bottom in unzip
# You could do it automatically if uncomment this line:  
# wget ftp://cvs.happy.kiev.ua/pub/fidosoft/mailer/binkd/binkd098.zip 
# If you are using newest version of binkd pls change 
# name of zip file below. 
#
########################

# Set initial variables:

NAME=binkd			# Program name
PNAME=binkd			# Package name (Useful for changing case, removing "-"s, etc.)
VERSION=1.0.0		# Program version
PVERSION=		# Package version (Useful for removing "-"s, CVS builds, etc.)
ARCH=${ARCH:-x86_64}	# Package architecture
BUILD=${BUILD:-fido_my1}	# Build number plus packager initials (use your own)

CWD=`pwd`
TMP="/tmp"	# Location to compile the source
PKG=$TMP/package-$NAME	# Location to build the package (use "package-$NAME" to avoid poss. conflicts)

# Defining pakage documentation files 

PDOC="!README !README.FIX !SRIF.TXT COPYING binkd.txt readme.ND"

# Compiler flags based on intended architecture:
# Can easily expand this for alternate compilations

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG		# Get rid of any leftovers
mkdir -p $PKG		# Make sure $PKG and $TMP (-p switch) exist

cd $TMP
rm -rf binkd-$VERSION

# Correct Binkd zip file name if you are using fresh version. 
unzip $CWD/binkd100.zip


cd binkd-$VERSION
cp $TMP/binkd-$VERSION/mkfls/unix/* $TMP/binkd-$VERSION/
chmod +x $TMP/binkd-$VERSION/mkinstalldirs

# Classic "./configure && make && make install":

CFLAGS="$SLKCFLAGS" \
sh configure \
  --prefix=/usr/local \
  --sysconfdir=/usr/local/etc \
  --localstatedir=/var/lib \
  --with-https -with-bwlim --with-perl \
  $ARCH-slackware-linux
make
#make PREFIX=/usr/local install

mkdir -p /usr/local/sbin
mkdir -p /usr/local/bin
mkdir -p /usr/local/etc
mkdir -p /usr/local/man
mkdir -p /home/fido

/usr/bin/install -c -D binkd /usr/local/sbin/binkd
/usr/bin/install -c -D -m 644 binkd.8 /usr/local/share/man/man8/binkd.8
/usr/bin/install -c -D -m644 binkd.conf /usr/local/etc/binkd.conf-dist


# Compress man pages:

find /usr/local/man -type f -exec gzip -9 {} \;

# Create package docs:
# Probably should define the package documentation with
# a variable ($PDOCS) up top and replace the names with it

mkdir -p /usr/local/doc/$PNAME-$VERSION
cp -a $PDOC /usr/local/doc/$PNAME-$VERSION/
mkdir -p /usr/local/doc/fidoip
cat $CWD/documentation.txt > /usr/local/doc/fidoip/documentation.txt
chmod 644 /usr/local/doc/$PNAME-$VERSION/*
chmod 644 /usr/local/doc/fidoip/*


if [ -e $CWD/binkd.cfg ]; then

mkdir -p /usr/local/etc
mkdir -p /usr/local/etc/fidoip
cat $CWD/binkd.cfg > /usr/local/etc/binkd.cfg
cat $CWD/binkd.cfg > /usr/local/etc/fidoip/binkd.cfg.template
fi

if [ -e $CWD/recv ]; then
cat $CWD/recv > /usr/local/bin/recv
cat $CWD/recv > /usr/local/etc/fidoip/recv.template
chmod +x /usr/local/bin/recv
chmod +x /usr/local/etc/fidoip/recv.template
fi

if [ -e $CWD/send ]; then
cat $CWD/send > /usr/local/bin/send
cat $CWD/send > /usr/local/etc/fidoip/send.template
chmod +x /usr/local/bin/send
chmod +x /usr/local/etc/fidoip/send.template
fi

if [ -e $CWD/binkdstat.pl ]; then
  mkdir -p /usr/local
  mkdir -p /usr/local/bin
  cat $CWD/binkdstat.pl > /usr/local/bin/binkdstat.pl
  chmod +x /usr/local/bin/binkdstat.pl
  cat $CWD/stat-binkd.pl > /usr/local/bin/stat-binkd.pl
  chmod +x /usr/local/bin/stat-binkd.pl
  cat $CWD/binkd_yesterday_stat.sh > /usr/local/bin/binkd_yesterday_stat.sh
  chmod +x /usr/local/bin/binkd_yesterday_stat.sh
  cat $CWD/binkd_weekly_stat.sh > /usr/local/bin/binkd_weekly_stat.sh
  chmod +x /usr/local/bin/binkd_weekly_stat.sh 
  cat $CWD/binkd_monthly_stat.sh > /usr/local/bin/binkd_monthly_stat.sh
  chmod +x /usr/local/bin/binkd_monthly_stat.sh
fi

if [ -e $CWD/binkd.log ]; then
  mkdir -p /home
  mkdir -p /home/fido
  mkdir -p /home/fido/outbound
  mkdir -p /home/fido/protinb
  mkdir -p /home/fido/inbound
  mkdir -p /home/fido/tempinb
  mkdir -p /home/fido/tempoutb
  mkdir -p /home/fido/filebox
  mkdir -p /home/fido/badarea 
  mkdir -p /home/fido/carbonarea
  mkdir -p /home/fido/msgbasedir
  mkdir -p /home/fido/netmailarea
  mkdir -p /home/fido/localinb
  mkdir -p /home/fido/uudecode
  mkdir -p /home/fido/outfile
  mkdir -p /home/fido/nodelist
  mkdir -p /home/fido/2uplink
  mkdir -p /home/fido/public
  mkdir -p /home/fido/flags
  mkdir -p /home/fido/magic
  mkdir -p /home/fido/announce
 cat $CWD/binkd.log > /home/fido/binkd.log
fi


# Clean up the extra stuff:

if [ "$1" = "--cleanup" ]; then
  rm -rf $PKG
fi
