#! /bin/bash
#
# call this script in this order under root or user login:
#
#   become user
#     build compile
# or
#   maybe root
#     build compile diff
#     build compile patch
#   become user
#     build compile rcs
#
# finaly
#
#   become root
#     build install
#   maybe root or user
#     build tar
#

MODE="$1"
COMPILE=$2

if [ "$COMPILE" = "" ]; then
  COMPd=diff
  COMPp=patch
  COMPr=rcs
else
  COMPd=$COMPILE
  COMPp=$COMPILE
  COMPr=$COMPILE

fi

# ----------------------- diffutils ------------------------------

if [ "$MODE" = "compile" ]; then

if [ "$COMPd" = "diff" ]; then
  cd diffutils-2.7

  if [ -f Makefile ]; then
    make distclean
    rm -f core *.log
  fi

  if ! grep ORIG_DIFFUTIL_2_7_BUG diff3.c  1>/dev/null 2>&1; then
    patch -p0 <diff3.patch
  fi

  export LDFLAGS="-static -O"
  ./configure 2>&1 | tee configure.log

  sed -e 's/DIFF_PROGRAM =.*/DIFF_PROGRAM = diff/' Makefile >Makefile.tmp
  mv -f Makefile.tmp Makefile

  make prefix=/usr 2>&1 | tee compile.log
  cd ..
fi

# ----------------- patch-2.1 --------------------------

if [ "$COMPp" = "patch" ]; then
  cd patch-2.1
  if [ -f Makefile ]; then
    make distclean
    rm -f core *.log
  fi
  ./configure 2>&1 | tee configure.log
  make prefix=/usr "LDFLAGS=-static -O" 2>&1 | tee compile.log
  cd ..
fi


# -----------------  RCS ---------------------------

if [ "$COMPr" = "rcs" ]; then
  cd rcs-5.7
  if [ -f Makefile ]; then
    make distclean
    rm -f core *.log
  fi
  export LDFLAGS="-static -O"
  ./configure --prefix=/usr 2>&1 | tee configure.log
  sed -e 's;RCSPREFIX =.*/;RCSPREFIX=;' \
      -e 's/DIFF3_BIN =.*/DIFF3_BIN = 1/' \
      -e 's/$(DIFF)/diff/g' \
      -e 's/$(DIFF3)/diff3/g' src/Makefile >src/Makefile.tmp

  if ! grep DIFFFLAGS src/Makefile | grep a >/dev/null 2>&1; then
    sed -e 's/DIFFFLAGS = -/DIFFFLAGS = -a/' src/Makefile.tmp >src/Makefile
  else
    mv -f src/Makefile.tmp src/Makefile
  fi

  make 2>&1 | tee compile.log
  cd ..
fi

fi  # end of compile

# --------------------------------------------------------------------------

if [ "$MODE" = "install" ]; then
  rm -f install/doinstall.sh
  cd diffutils-2.7
  make prefix=/usr install 2>&1 | tee install.log
  cd ../patch-2.1
  make prefix=/usr install 2>&1 | tee install.log
  cd ../rcs-5.7
  make install 2>&1 | tee install.log
  cd ..

  for i in `grep /usr/bin tar-list`; do
    echo " stripping $i"
    strip $i
  done

  # check if man or info files have to be gziped.
  if ls /usr/man/man1/*.gz >/dev/null 2>&1; then
    for i in ` awk '/\/usr\/man/ || /\/usr\/info\// {print}' tar-list`; do
      rm -f ${i}.gz
      echo "rm -f ${i}" >> install/doinstall.sh
      gzip -9 ${i}
    done
  fi

fi

# -------------------------------------------------------------


if [ "$MODE" = "tar" ]; then

  # check if man or info files have to be gziped.
  TARLIST=tar-list
  if ls /usr/man/man1/*.gz >/dev/null 2>&1; then 
    sed -e 's;/usr/man/.*;&.gz;' \
        -e 's;/usr/info/.*;&.gz;' tar-list >tar-list~
    TARLIST=tar-list~
  fi

  echo "makeing bin-dist"
  tar -cf cvsaux-1.0-bin.tar -C / -T $TARLIST
  tar -rf cvsaux-1.0-bin.tar install/doinstall.sh
  rm -f cvsaux-1.0-bin.tar.gz
  gzip -9 cvsaux-1.0-bin.tar
  rm -f *~
  echo "... done"
fi
