#!/bin/sh
#
# Try to create the Pike crypto module needed for SSL support in Roxen
#
# Fredrik Hallenberg <hallon@debian.org>
#

# Some stuff taken from
# $Id: build-PACKAGE,v 1.2 1998/03/20 12:07:42 phil Exp $
#
# Written by Philip Hands <phil@hands.com>
# Copyright (C) 1998 Free Software Foundation, Inc.
# Copying: GPL

set -e

# 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
}

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

pikeversion=0.6.110
package=${0##*build-}

NEWDIR=/tmp/$package

ROXENSSLSOURCE=/tmp/roxen_1.2.46-0.5-withSSL3.tar.gz
PIKESOURCE=/tmp/pike_${pikeversion}.orig.tar.gz

TMPPIKE=${NEWDIR}/build-dir
PIKEDEB=${NEWDIR}/pike-crypto
ROXEN=${NEWDIR}/roxen_1.2.46
PIKE=${NEWDIR}/pike-0.6.110.orig

cat <<EOF 

Due to  export restrictions the Pike  strong crypto module  can not be
distributed. This module is needed if you want to use SSL with Roxen.

This package  will try to  build the pike-crypto package  from sources
that you will have to download from http://www.roxen.com/download

The source for Pike 0.6.110 is also needed. debget is used to download
this source from a Debian mirror so this should not be a problem.

You will need to get atlest this file your self
    roxen_1.2.46-0.5-withSSL3.tar.gz

(and if  you do not have  the debget package installed,  you also need
the  file  /tmp/pike_0.6.110.orig.tar.gz, that  can  be  found in  the
.../sources/devel/ directory in any Debian FTP mirror)

EOF

if ! ask_user "Do you wish to start the buildprocess now ? [Yn] " 0
then
    echo ""
    echo "Aborting. Reconfigure this package to start the buildprocess later."
    exit 1
fi

if [ -d $NEWDIR ]; then
  ask_user "$NEWDIR already exists, should I use it anyway ? [yN] " || {
    echo -e "\nPlease fix it and run $0 again\n"
    exit 1
  }
else
    mkdir $NEWDIR
    chmod 700 $NEWDIR
fi

if [ ! -f $ROXENSSLSOURCE ]; then
  echo "Can't find $ROXENSSLSOURCE"
  exit 1
fi

if [ ! -f $PIKESOURCE ]; then
  if [ -x /usr/bin/debget ]; then
    echo "Fetching source for Pike 0.6"
    cd `dirname $PIKESOURCE`
    if ! debget pike; then
      echo "debget isn't working"
      exit 1
    fi
  else
    echo "Can't find $PIKESOURCE"
    exit 1
  fi
fi

if [ -d $TMPPIKE ]; then
  echo "Please move $TMPPIKE out of the way"
  exit 1
fi

if [ -d $ROXEN ]; then
  echo "Please move $ROXEN out of the way"
  exit 1
fi

if [ -d $PIKE ]; then
  echo "Please move $PIKE out of the way"
  exit 1
fi

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

echo ""
echo -n "Unpacking Roxen... "

cd `dirname $ROXEN`
tar xfz $ROXENSSLSOURCE

if [ ! -d ${ROXEN}/pike/0.5/lib/modules/SSL.pmod ]; then
  echo ""
  echo "Can't find SSL directory in ${ROXEN}."
  echo "Is ${ROXENSSLSOURCE} a proper Roxen-SSL archive?"
  rm -rf $ROXEN
  exit 1
fi
echo "done."

echo -n "Unpacking Pike... "

cd `dirname $PIKE`
tar xfz $PIKESOURCE
echo "done."

echo "Patching Pike..."

cd ${PIKE}/src/modules
rm -rf Gdbm Gmp Gz Image MIME Msql Mysql Pipe Postgres Regexp Ssleay Yp readline spider math system sprintf files call_out _Image_* _Charset _Lobotomized_Crypto
cp -r ${ROXEN}/pike/0.5/src/modules/_Crypto . 

cd _Crypto
patch <<EOF
--- /home/hallon/paket/_Crypto/sha.c    Tue Nov 18 20:43:03 1997
+++ sha.c       Thu Mar 18 17:08:40 1999
@@ -16,2 +16,3 @@
 #include "module_support.h"
+#include "las.h"
 
EOF

echo ""
echo "Configuring..."

cd ${PIKE}/src
./configure --prefix=$TMPPIKE
# Remove -Rlib from Makefiles
find -name Makefile -exec perl -pi -e 's/-R[^ ]* //g' \{\} \;

echo ""
echo "Making module..."

# Some ugliness to avoid building too much stuff
cp /usr/bin/pike .
touch pike
chmod a+x pike
touch pike-module
for f in `find -name "*.c" -maxdepth 1`; do
  touch `echo $f | sed s/\.c/.o/`
done

make module_objects

echo "Installing module..."

make -i install

if [ ! -e ${TMPPIKE}/lib/pike/modules/_Crypto.so ]; then
  echo "That didn't work. Please report this to <hallon@debian.org>."
  echo "Include as much information and error messages as possible."
  exit 1
fi

mkdir -p ${PIKEDEB}
cp ${TMPPIKE}/lib/pike/modules/_Crypto.so ${PIKEDEB}
cp -r ${ROXEN}/pike/0.5/lib/modules/SSL.pmod ${PIKEDEB}
rm -Rf ${PIKEDEB}/SSL.pmod/CVS

if [ -d ${PIKEDEB} -a -f /usr/doc/pike-crypto-src/pike-crypto-debian-dir.tgz ]; then
    cd ${PIKEDEB}
    tar xzf /usr/doc/pike-crypto-src/pike-crypto-debian-dir.tgz
else
    echo "Can't find the ${PIKEDEB} directory OR the file:"
    echo "/usr/doc/pike-crypto-src/pike-crypto-debian-dir.tgz"
    exit 1
fi

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

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 ""
echo "Building ${package}.deb..."

cd ${PIKEDEB}
$BUILDROOT debian/rules binary-arch binary-indep
cd ..

blah="`find . -name ${package}\*.deb -maxdepth 1`"
if [ "${blah}" ]
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 ${TMPPIKE} ${PIKEDEB} ${ROXEN} ${PIKE}
  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}-src now? [yN] " 1
  then
    $SU dpkg --purge ${package}-src || true
  fi
else
  echo ""
  echo "You can now remove package ${package}-src running as root"
  echo ""
  echo "        dpkg --remove ${package}-src"
fi

echo ""
echo "Remember that you can install `echo ${package}*.deb`"
echo "in other computers so you don't need to compile it again."
echo ""
echo 'Good luck!'
echo ""

exit 0
