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

PKG=$TMP/package-shadow

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

echo "+===============+"
echo "| shadow-980529 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/shadow-980529.tar.gz
cd shadow-980529
./configure --prefix=/usr --with-md5crypt --disable-shared
make
mkdir -p $PKG/usr/lib
cat lib/libshadow.a > $PKG/usr/lib/libshadow.a
mkdir -p $PKG/usr/include/shadow
cp -a lib/pwauth.h $PKG/usr/include/shadow
cp -a $CWD/shadow.h $PKG/usr/include/shadow
chmod 644 $PKG/usr/include/shadow/*
chown root.root $PKG/usr/include/shadow/*
#
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 grpconv grpunconv ; 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 limits.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 \
shadowconfig.8 sulogin.8 useradd.8 userdel.8 usermod.8 vipw.8 ; do
  gzip -9c $file > $PKG/usr/man/man8/$file.gz
done
cd ..

# Build the package:
cd $PKG
tar czvf $TMP/shadow.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
  rm -rf $TMP/shadow-980529
  rm -rf $PKG
fi
