#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-ldso
SRC=/devel/manpagesrc
INFO=/devel/info-pages/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/_ldso.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 "| ld.so-1.9.5 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/ld.so-1.9.5.tar.gz
cd ld.so-1.9.5
# These are pre-built by trustworthy individuals, so there's really no
# need to recompile them...
# make clean
# make
cat README > $PKG/usr/doc/ldso/README
cat COPYRIGHT > $PKG/usr/doc/ldso/COPYRIGHT
cat d-link/ld-linux.so > $PKG/lib/ld-linux.so.1.9.5
strip d-link/libdl/libdl.so
cat d-link/libdl/libdl.so > $PKG/lib/libdl.so.1.9.5
cat d-link/libdl/dlfcn.h > $PKG/usr/include/dlfcn.h
strip util/ldd
cat util/ldd > $PKG/usr/bin/ldd
strip util/ldconfig
cat util/ldconfig > $PKG/sbin/ldconfig
strip ld-so/ld.so
cat ld-so/ld.so > $PKG/lib/ld.so.incoming
cat man/ldd.1 | gzip -9c > $PKG/usr/man/man1/ldd.1.gz
cat man/ldconfig.8 | gzip -9c > $PKG/usr/man/man8/ldconfig.8.gz
cat man/ld.so.8 | gzip -9c > $PKG/usr/man/man8/ld.so.8.gz
cat man/dlopen.3 | gzip -9c > $PKG/usr/man/man3/dlopen.3.gz

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

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