#!/bin/bash -e
#
# build-<package>
#
# $Id: build-PACKAGE,v 1.5 1998/05/01 08:35:36 phil Exp $
#
# Written by Philip Hands <phil@hands.com>
# Modified by Adam McKenna <adam@debian.org> 3/5/2002
# Copyright (C) 1998 Free Software Foundation, Inc.
# Copying: GPL

# ask_user ---  function prompts the user with y/n style prompt
#
# It's behaviour is controlled via the parameters:
#  1  -  the initial prompt
#  2  -  default return value (set to 0, 1 or "none")
#  3  -  the patern match for acceptable Yes responses
#  4  -  the patern match for acceptable No responses
#  5  -  the error prompt, displayed when the user fails to provide valid input
#
# e.g.  ask_user  "Foo, or Bar [fb] " none '[fF]*' '[bB]*' "try again"

ask_user() {
  P=${1:-'Should I do this ? [yN] '}
  D=${2:-1}
  Y=${3:-'[yY]*'}
  N=${4:-'[nN]*'}
  E=${5:-'\nPlease enter either y)es or n)o, followed by <RETURN>\n'}

  while true ; do
    echo -ne "$P"
    read response
    case "$response" in
      ${Y} ) return 0 ;;
      ${N} ) return 1 ;;
      "" ) [ "$D" = 0 -o "$D" = 1 ] && return $D ;;
    esac
    echo -e $E
  done
}

package=daemontools
version="0.76"
NEWDIR=/tmp/$package

cat <<!END!

This script unpacks the ${package} source into a directory, and
compiles it to produce a binary ${package}*.deb file.

The directory where this is done will end up containing the source
and package files for the $package binary package, along with a
directory containing the unpacked source.

!END!

line=kjfdsh
read  -e -p "Enter a directory where you would like to do this [$NEWDIR] " line
NEWDIR="${line:-$NEWDIR}"

if test -d $NEWDIR
then
  echo -e "$NEWDIR already exists, please remove it and run $0 again\n"
  exit 1
else
  mkdir $NEWDIR
fi

cd $NEWDIR
tar zxf /usr/src/${package}-installer/${package}_${version}.orig.tar.gz
cd admin/${package}-${version}
tar zxf /usr/src/${package}-installer/debian.tar.gz

CANBEROOT=no
SU=""
if [ 0 = `id -u` ]
then
  BUILDROOT=""
  CANBEROOT=yes
else
  hash fakeroot 2>/dev/null && HAVE_FAKEROOT=fakeroot || HAVE_FAKEROOT=""
  hash sudo     2>/dev/null && HAVE_SUDO=sudo         || HAVE_SUDO=""
  if [ -n "$HAVE_FAKEROOT" -a -n "$HAVE_SUDO" ]
  then
    echo ""
    if ask_user "Should I use sudo or fakeroot to build the package ? [sF] " 1 '[sS]*' '[fF]*' "\nPlease enter either s)udo or f)akeroot, followed by <RETURN>\n"
    then
      BUILDROOT=sudo
    else
      BUILDROOT=fakeroot
    fi
  elif [ -n "$HAVE_FAKEROOT" -o -n "$HAVE_SUDO" ]
  then
    BUILDROOT="${HAVE_FAKEROOT:-$HAVE_SUDO}"
  else
    # sanity check, dependencies should provide one of them
    echo ""
    echo 'oops! you have not installed fakeroot or sudo!'
    echo ""
    exit 1
  fi
  
  if [ -n "$HAVE_SUDO" ]
  then
    cat<<!END!

In addition to building the package, there are certain other actions that
you may want me to do (i.e. installing the new package) that need real root
access.  If you want, I can a achieve this by use of sudo.

In each case, you will be prompted before I attempt one of these actions

!END!
    if ask_user "Should I use sudo to gain real root access when required ? [Yn] " 0
    then
      SU=sudo
      CANBEROOT=yes
    fi
  fi
fi

echo ""
cd $NEWDIR
cd admin/${package}-${version}

echo ""
echo "Please select package format"
cat<<EOT
 The "djb" format is the way that daemontools would be installed if you
 downloaded the source yourself and compiled it.  This will create several
 new top-level directories such as /package and /command.  It will also
 use /service as the service directory.
 .
 The "fhs" format is an FHS-compatible format which installes all binaries
 in /usr/bin, and uses /var/lib/svscan as the service directory.  This format
 is NOT recommended as it will most likely not be supported by DJB or any
 of his mailing lists.
 .
 *IMPORTANT* if you choose the DJB version, a new directory called /service
 will be created.  If you are upgrading from an old release, you may or may
 not have a symlink pointing from /service to /var/lib/svscan.  Either way,
 before upgrading I suggest you remove this symlink and then copy the existing
 data from /var/lib/svscan to /service after the upgrade.

EOT

    if ask_user "Which format would you like to use? [fD] " 0 '[dD]*' '[fF]*' "\nPlease enter either d)jb or f)hs, followed by <RETURN>\n"
    then
      FORMAT=djb
    else
      FORMAT=fhs
    fi

echo ""
echo "Binary package $package will be compiled now"
echo "This can take long time, depending on your machine"
echo ""
echo "If you want me to apply any patches, please make sure that"
echo "they are located in /usr/src/daemontools-installer/patches/."
echo ""
echo -n "Press ENTER to continue..."
read blah

echo "Attempting to apply patches located in /usr/src/daemontools-installer/patches..."
for i in /usr/src/daemontools-installer/patches/*; do
 echo $i
 patch -p0 < $i
done

if [ $FORMAT = "djb" ]; then
	$BUILDROOT debian/rules binary-arch
else
	$BUILDROOT debian/rules binary-arch-fhs
fi

cd ..

if [ -f ${package}*.deb ]
then
  echo ""
  echo "It seems that all went ok"
  echo ""
else
  echo ""
  echo "Some error happened, stopping here."
  echo ""
  exit 1
fi

if ask_user "Do you want to remove all files in $NEWDIR,\nexcept `echo ${package}*.deb` now? [Yn] " 0
then
  [ "$BUILDROOT" = sudo ] && ROOT4RM=sudo
  echo -n "Removing files... "
  $ROOT4RM rm -rf ./${package}-*/
  $ROOT4RM rm -f ./${package}*{.dsc,.diff.gz,.orig.tar.gz}
  echo "done"
fi

echo ""

if [ "$CANBEROOT" = yes ] 
then
  if ask_user "Do you want to install `echo ${package}*.deb` now? [Yn] " 0
  then
    $SU dpkg --install ${package}*.deb || true
  fi
else
  echo "`echo ${package}*.deb` is in $NEWDIR"
  echo "You have to execute the following to install it, being root:"
  echo ""
  echo "        dpkg --install `echo ${package}*.deb`"
fi

echo ""

if [ "$CANBEROOT" = yes ]
then
  if ask_user "Do you want to purge ${package}-installer now? [yN] " 1
  then
    $SU dpkg --purge ${package}-installer || true
  fi
else
  echo ""
  echo "You can now remove package ${package}-installer running as root"
  echo ""
  echo "        dpkg --remove ${package}-installer"
fi

echo ""
echo 'Good luck!'
echo ""

exit 0
