#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-lthreads
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/_linuxthreads.tar.gz

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

echo "+====================+"
echo "| linuxthreads-0.7.1 |"
echo "+====================+"
cd $TMP
tar xzvf $CWD/linuxthreads-0.7.1.tar.gz
cd linuxthreads-0.71
make
ranlib libpthread.a
cat libpthread.a > $PKG/usr/lib/libpthread.a
strip libpthread.so.0.7
cat libpthread.so.0.7 > $PKG/lib/libpthread.so.0.7
cat semaphore.h > $PKG/usr/include/semaphore.h
cat pthread.h > $PKG/usr/include/pthread.h.lthreads
cat sched.h > $PKG/usr/include/sched.h.lthreads
cd man
for page in `ls *.3thr | cut -d. -f1` ; do
  gzip -9c $page.3thr > $PKG/usr/man/man3/$page.3.gz
done
cd ..
cp Changes Examples FAQ.html LICENSE README README.Xfree3.2 README.Xfree3.3 \
$PKG/usr/doc/linuxthreads-0.7.1

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