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

# ===> Change only these
pikeversion=0.6.131
roxenversion=1.3.122

ROXENSSLSOURCE=/tmp/roxen_${roxenversion}-0.6.tar.gz
PIKESOURCE=/tmp/pike_${pikeversion}.orig.tar.gz
# ===> end of changes

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

try_aptget() {
    if [ -x /usr/bin/apt-get ]; then
	echo ". I'll try apt-get instead..."

	if ! apt-get -d source pike > /dev/null; then
	    echo -n "apt-get didn't work ether"

	    if ! `grep -q ^deb-src /etc/apt/sources.list`; then
		echo " and there is no deb-src entry in /etc/apt/sources.list."
		echo "Please add the line [1mdeb-src ftp://ftp.debian.org/debian potato main contrib non-free[0m"
		echo "to the file /etc/apt/sources.list and run apt-get update, then try running this script again."
		echo
	    else
		echo
		echo ". I'm stumped. You have to download the pike sources manually,"
		echo "and rerun this script when you are done."
		echo
	    fi

	    exit 1
	else
	    echo " Oki, that worked. Found the pike sources..."
	fi
    else
	echo " and I can't find apt-get."
	exit 1
    fi
}

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

package=${0##*build-}
NEWDIR=/tmp/$package

TMPPIKE=${NEWDIR}/build-dir
PIKEDEB=${NEWDIR}/pike-crypto
ROXEN=${NEWDIR}/roxen_${roxenversion}
PIKE=${NEWDIR}/pike-${pikeversion}.orig

DEBIAN_DIR_ARCHIVE=/usr/share/doc/pike-crypto-build/pike-crypto-debian-dir.tgz

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 $pikeversion 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 yourself
    $ROXENSSLSOURCE

And if you do not have the debget package installed, you also need the
file   /tmp/pike_$pikeversion.orig.tar.gz,  that  can   be  found   in  the
dists/potato/main/source/interpreters/  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 $pikeversion"
    cd `dirname $PIKESOURCE`

    if ! debget pike > /dev/null; then
      echo -n "debget wasn't working"
      try_aptget
    else
	echo "Found the pike sources..."
    fi
  else
    echo -n "Can't find $PIKESOURCE nor debget"
    cd `dirname $PIKESOURCE`
    try_aptget
  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

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

if [ -f $PIKESOURCE ]; then
    echo -n "Unpacking Pike... "
    cd `dirname $PIKE`
    tar xfz $PIKESOURCE
    echo "done."
else
    if [ -f /tmp/pike_*.orig.tar.gz ]; then
	cd /tmp && \
	  VER=`echo pike_*.orig.tar.gz | sed -e 's/pike_//' -e 's/\.orig\.tar\.gz//'`

	echo ""
	echo "I found a pike version, but not the correct one."
	echo "The one I found was of version $VER."
	echo "You have to find the pike source manually and put it in /tmp."
	exit 1
    else
	echo ""
	echo "Can't find $PIKESOURCE"
	exit 1
    fi
fi

echo ""
echo -n "Unpacking Roxen... "
cd `dirname $ROXEN`
tar xfz $ROXENSSLSOURCE

if [ ! -d ${ROXEN}/pike/0.6/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 "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.6/src/modules/_Crypto . 

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.6/lib/modules/SSL.pmod ${PIKEDEB}
rm -Rf ${PIKEDEB}/SSL.pmod/CVS

if [ -d ${PIKEDEB} -a -f $DEBIAN_DIR_ARCHIVE ]; then
    cd ${PIKEDEB}
    tar xzf $DEBIAN_DIR_ARCHIVE
else
    echo "Can't find the ${PIKEDEB} directory OR the file:"
    echo "$DEBIAN_DIR_ARCHIVE"
    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}-build now? [Yn] " 0
  then
    $SU dpkg --purge ${package}-build || true
  fi
else
  echo ""
  echo "You can now remove package ${package}-build running as root"
  echo ""
  echo "        dpkg --remove ${package}-build"
fi

cat <<EOF

Remember that this package is illegal to distribute, since it requires
a  licence from  Idonex/Swedish government  to obtain  the  Roxen SSL3
package outside the European Community!

Good luck!

EOF

exit 0
