#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-shadow
SRC=/devel/manpagesrc
INFO=$PKG/usr/info
TEX=/devel/texinfo-docs

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/_shadow.tar.gz

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+===============+"
echo "| shadow_970616 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/shadow_970616.tar.gz
cd shadow-970616
./configure --prefix=/usr --with-md5crypt
make
mkdir -p $PKG/usr/lib
cat lib/libshadow.a > $PKG/usr/lib/libshadow.a
#
cd src
mkdir -p $PKG/bin
chgrp bin $PKG/bin
mkdir -p $PKG/sbin
chgrp bin $PKG/sbin
cp login $PKG/bin/login
cp su $PKG/bin/su
chmod 755 $PKG/bin/login
chmod 4711 $PKG/bin/su
cp sulogin $PKG/sbin/sulogin
chmod 755 $PKG/sbin/sulogin
#
mkdir -p $PKG/usr/sbin
for file in chpasswd dpasswd faillog groupadd groupdel groupmod grpck \
lastlog logoutd mkpasswd newusers pwck useradd userdel usermod \
pwconv pwunconv ; do
  cp $file $PKG/usr/sbin
  chmod 755 $PKG/usr/sbin/$file
done
#
mkdir -p $PKG/usr/bin
for file in chage chfn chsh expiry gpasswd newgrp passwd ; do
  cp $file $PKG/usr/bin
  chmod 4711 $PKG/usr/bin/$file
done
cd ..
cp contrib/adduser.sh /sbin/adduser
cd doc
mkdir -p $PKG/usr/doc/shadow
cp -a ANNOUNCE CHANGES HOWTO LICENSE README README.limits \
  README.linux WISHLIST $PKG/usr/doc/shadow
chown root.root $PKG/usr/doc/shadow/*
cd ../man
mkdir -p $PKG/usr/man/man1
for file in chage.1 chfn.1 chsh.1 gpasswd.1 groups.1 id.1 login.1 newgrp.1 \
passwd.1 su.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/$file.gz
done
mkdir -p $PKG/usr/man/man3
for file in pw_auth.3 shadow.3 ; do
  gzip -9c $file > $PKG/usr/man/man3/$file.gz
done
mkdir -p $PKG/usr/man/man5
for file in faillog.5 login.access.5 login.defs.5 passwd.5 porttime.5 \
shadow.5 suauth.5 ; do
  gzip -9c $file > $PKG/usr/man/man5/$file.gz
done
mkdir -p $PKG/usr/man/man8
for file in chpasswd.8 dpasswd.8 faillog.8 groupadd.8 groupdel.8 groupmod.8 \
grpck.8 lastlog.8 logoutd.8 mkpasswd.8 newusers.8 pwauth.8 pwck.8 pwconv.8 \
pwunconv.8 sulogin.8 useradd.8 userdel.8 usermod.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/$file.gz
done
cd ..

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

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/shadow-970616
  rm -rf $PKG
fi