#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

PKG=$TMP/package-yptools

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_yptools.tar.gz

# Find the size of a file:
filesize() {
 SIZE=`ls -l -d -G $1 | cut -b23-32`
 echo -n $SIZE
}

echo "+==============+"
echo "| yp-tools-2.5 |"
echo "+==============+"
cd $TMP
# Make sure we have a working etc/nsswitch.conf:
zcat $CWD/nsswitch.conf.gz > $PKG/etc-incoming/nsswitch.conf
# Add etc/nsswitch.conf-nis as a full NIS+ example config file:
zcat $CWD/nsswitch.conf-nis.gz > $PKG/etc-incoming/nsswitch.conf-nis
tar xyvf $CWD/yp-tools-2.5.tar.bz2
cd yp-tools-2.5
mkdir -p $PKG/usr/doc/yp-tools-2.5
cp -a ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README THANKS TODO \
  $PKG/usr/doc/yp-tools-2.5
chown root.root $PKG/usr/doc/yp-tools-2.5/*
chmod 644 $PKG/usr/doc/yp-tools-2.5/*
./configure --prefix=/usr --disable-domainname i386-slackware-linux
make CFLAGS=-O2 -j 2
cat etc/nicknames > $PKG/var/yp/nicknames
mkdir -p $PKG/usr/share/locale/de/LC_MESSAGES
cat po/de.gmo > $PKG/usr/share/locale/de/LC_MESSAGES/yp-tools.mo
cd src
strip ypcat ypchfn ypchsh ypmatch yppasswd yppoll ypset ypwhich
cat ypcat > $PKG/usr/bin/ypcat
cat ypmatch > $PKG/usr/bin/ypmatch
cat yppasswd > $PKG/usr/bin/yppasswd
cat ypwhich > $PKG/usr/bin/ypwhich
cat yppoll > $PKG/usr/sbin/yppoll
cat ypset > $PKG/usr/sbin/ypset
cd ../man
cat nicknames.5 | gzip -9c > $PKG/usr/man/man5/nicknames.5.gz
for file in *.1 ; do
  cat $file | gzip -9c > $PKG/usr/man/man1/$file.gz
done
for file in *.8 ; do
  cat $file | gzip -9c > $PKG/usr/man/man8/$file.gz
done

echo "+============+"
echo "| ypbind-3.3 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/ypbind-3.3.tar.gz
cd ypbind-3.3
zcat $CWD/ypbind-3.3-glibc5.diff.gz | patch -p1 --backup --verbose --suffix=.orig
zcat $CWD/ypbind-3.3-glibc-2.2.2.diff.gz | patch -p1 --backup --verbose --suffix=.orig
./configure --prefix=/usr \
            i386-slackware-linux
# Ugly...
make -i
#cp yp.conf.example $PKG/etc/yp.conf-example
mkdir -p $PKG/usr/doc/ypbind-3.3
cp -a COPYING INSTALL README TODO yp.conf.example $PKG/usr/doc/ypbind-3.3
chown root.root $PKG/usr/doc/ypbind-3.3/*
chmod 644 $PKG/usr/doc/ypbind-3.3/*
cat ypbind > $PKG/usr/sbin/ypbind-3.3
cat ypbind.8 | gzip -9c > $PKG/usr/man/man8/ypbind-3.3.8.gz

echo "+===============+"
echo "| ypbind-mt-1.8 |"
echo "+===============+"
cd $TMP
tar xyvf $CWD/ypbind-mt-1.8.tar.bz2
cd ypbind-mt-1.8
./configure --prefix=/usr \
            i386-slackware-linux
make
cat etc/yp.conf > $PKG/etc/yp.conf-example
mkdir -p $PKG/usr/doc/ypbind-mt-1.8
cp -a ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README THANKS TODO \
  $PKG/usr/doc/ypbind-mt-1.8
chown root.root $PKG/usr/doc/ypbind-mt-1.8/*
chmod 644 $PKG/usr/doc/ypbind-mt-1.8/*
strip src/ypbind
cat src/ypbind > $PKG/usr/sbin/ypbind
cat man/yp.conf.5 | gzip -9c > $PKG/usr/man/man5/yp.conf.5.gz
cat man/ypbind.8 | gzip -9c > $PKG/usr/man/man8/ypbind.8.gz
mkdir -p $PKG/usr/share/locale/de/LC_MESSAGES
cat po/de.gmo > $PKG/usr/share/locale/de/LC_MESSAGES/ypbind-mt.mo

echo "+=============+"
echo "| ypmake-0.11 |"
echo "+=============+"
cd $TMP
tar xyvf $CWD/ypmake-0.11.tar.bz2
cd ypmake-0.11
mkdir -p $PKG/usr/doc/ypmake-0.11
cp -a CHANGES README TODO $PKG/usr/doc/ypmake-0.11
chmod 644 $PKG/usr/doc/ypmake-0.11/*
chown root.root $PKG/usr/doc/ypmake-0.11/*
./configure
make
install -o root -g bin -m 755 ypmake $PKG/usr/sbin
install -o root -g root -m 644 aliases arrays automount config ethers group \
  gshadow hosts netgroup netid networks passwd protocols publickey rpc \
  services shadow ypservers $PKG/usr/lib/yp/ypmake
install -o root -g root -m 644 ypmake.conf.sample $PKG/var/yp
mkdir -p $PKG/usr/man/man5
mkdir -p $PKG/usr/man/man8
cat ypmake.conf.man | gzip -9c > $PKG/usr/man/man5/ypmake.conf.5.gz
cat ypmake.man | gzip -9c > $PKG/usr/man/man8/ypmake.8.gz

echo "+===============+"
echo "| ypserv-1.3.12 |"
echo "+===============+"
cd $TMP
tar xyvf $CWD/ypserv-1.3.12.tar.bz2
cd ypserv-1.3.12
zcat $CWD/ypserv-1.3.12.diff.gz | patch -p1 --verbose --backup --suffix=.orig -E
mkdir -p $PKG/usr/doc/ypserv-1.3.12
cp -a BUGS COPYING ChangeLog INSTALL NEWS README README.secure \
    TODO $PKG/usr/doc/ypserv-1.3.12
chmod 644 $PKG/usr/doc/ypserv-1.3.12/*
chown root.root $PKG/usr/doc/ypserv-1.3.12/*
# --with-ndbm=yes 
./configure --enable-tcp-wrapper \
            --enable-fqdn \
            --enable-yppasswd \
            i386-slackware-linux
make -j 2
strip create_printcap makedbm match_printcap mknetid pwupdate \
  revnetgroup yphelper ypinit ypxfr ypxfr_1perday ypxfr_1perhour \
  ypxfr_2perday
for file in create_printcap makedbm match_printcap mknetid pwupdate \
  revnetgroup yphelper ypinit ypxfr ypxfr_1perday ypxfr_1perhour \
  ypxfr_2perday ; do
  cat $file > $PKG/usr/lib/yp/$file
done
for file in ypserv rpc.yppasswdd rpc.ypxfrd yppush ; do
  cat $file > $PKG/usr/sbin/$file
done
cat ypMakefile > $PKG/var/yp/Makefile
cat etc/securenets > $PKG/var/yp/securenets
echo "# This file is part of the YP server package -- see 'man netgroup'" \
 > $PKG/etc-incoming/netgroup
cat etc/netgroup >> $PKG/etc-incoming/netgroup
for file in netgroup.5 ypserv.conf.5 ; do
  gzip -9c $file > $PKG/usr/man/man5/$file.gz
done
for file in makedbm.8 mknetid.8 pwupdate.8 revnetgroup.8 rpc.yppasswdd.8 \
  rpc.ypxfrd.8 ypinit.8 yppasswdd.8 yppush.8 ypserv.8 ypxfr.8 ypxfrd.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/$file.gz
done

( cd $PKG/usr/sbin ; strip * )

# Build the package:
cd $PKG
tar czvf $TMP/yptools.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
 for dir in \
  ypserv-1.3.12 yp-tools-2.5 ypbind-3.3 ypmake-0.11 ; do
  rm -rf $TMP/$dir
 done
 rm -rf $PKG
fi
