#!/bin/sh
#
# Freedom remailer installation script, using many ideas from
# Adam Shostack's Install-Mix, contributed by Ulf Moeller 1997
# Changes by Johannes Kroeger 1997-1998

REM_ODIR=$PWD
REM_SRC=$REM_ODIR/Src
REM_DEST0=${FREEDOM:-$HOME/freedom}

#whereis program default-path
whereis()
{
  #echo "Looking for $1..."
  found=""
  for i in $* `which $1 2>&1`
  do
    if [ -f "$i" ]
    then
      found=$i
    fi
  done
  if [ "$found" = "" ]
  then
    found=$2
#    echo "$1 not found. Using $found."
#  else
#    echo "$1 is at $found."
  fi
}

whereis gmake make
MAKE=$found

if echo -n | grep n >/dev/null
then
 echo1=""
 echo2="\c"
else
 echo1="-n"
 echo2=""
fi

readln()
{
  echo $echo1 "$1 [$2] $echo2"
  read ans
  if [ -z "$ans" ]
  then
    ans="$2"
  fi
}

inst_files()
{
  cd $REM_ODIR
  touch install.$$
  if [ ! -f $_RDIR/install.$$ ]
  then
    for i in $1
    do
      cp $i $_RDIR
    done
  fi
  rm -f install.$$
}

conf_file()
{
sed -e "s/%USE_SYSLOG/$_USE_SYSLOG/g" \
    -e "s/%USE_STATS/$_USE_STATS/g" \
    -e "s/%USE_MIX/$_USE_MIX/g" \
    -e "s/%ALLOW_PGP/$_ALLOW_PGP/g" \
    -e "s/%ALLOW_GPG/$_ALLOW_GPG/g" \
    -e "s/%ALLOW_POST/$_ALLOW_POST/g" \
    -e "s/%ALLOW_WWW/$_ALLOW_WWW/g" \
    -e "s/%ALLOW_LTIME/$_ALLOW_LTIME/g" \
    -e "s/%SIZE_LIMIT/$_SIZE_LIMIT/g" \
    -e "s#%RDIR#$_RDIR#g" \
    -e "s#%MDIR#$_MDIR#g" \
    -e "s#%QDIR#$_QDIR#g" \
    -e "s#%PDIR#$_PDIR#g" \
    -e "s#%GDIR#$_GDIR#g" \
    -e "s#%MBOX#$_MBOX#g" \
    -e "s#%MAIL#$_MAIL#g" \
    -e "s#%NEWS#$_NEWS#g" \
    -e "s#%PGP#$_PGP#g" \
    -e "s#%GPG#$_GPG#g" \
    -e "s#%WGET#$_WGET#g" \
    -e "s/%RMN/$_RMN/g" \
    -e "s/%RAN/$_RAN/g" \
    -e "s/%ORG/$_ORG/g" \
    -e "s/%RMA/$_RMA/g" \
    -e "s/%RAA/$_RAA/g" \
    -e "s/%M2N/$_M2N/g" \
    -e "s/%CA/$_CA/g" <$REM_ODIR/$1.in >$1
}

if [ -d $REM_SRC ]
then
  remailer=$REM_SRC/remailer
else
  if [ -f remailer ]
  then
    remailer=$REM_ODIR/remailer
  else
    echo "Cannot find remailer source directory."
    exit 1
  fi
fi

NAME=`whoami`
if [ "$NAME" = root ]
then
  echo "Installing the remailer as root is not recommended.
Please create a new user, e.g. \`remailer'."
  readln "Continue anyway?" n
  if [ "$ans" = y ]
  then
    NAME=remailer
    root=1
  else
    exit 1
  fi
fi

########################################################################

readln "Freedom remailer directory?" $REM_DEST0
_RDIR="$ans"

umask 077

if [ ! -d $_RDIR ]
then
  echo "Creating directory $_RDIR"
  mkdir $_RDIR
fi

if [ ! -d $_RDIR ]
then
  echo "Cannot create $_RDIR"
  exit 1
fi

if [ -x $_RDIR/remailer ]
then
  echo "Upgrading from previous remailer installation..."
fi

_USE_MIX=0
whereis mixmaster $HOME/Mix/mixmaster $MIXPATH/mixmaster
if [ -f "$found" ]
then
 i=y
else
 i=n
fi
readln "Are you going to run the remailer as the Type I remailer under Mixmaster?" $i
if [ "$ans" = y ]
then
 readln "Mixmaster directory?" `echo $found | sed 's#/mixmaster$##'`
 _MDIR="$ans"
 if [ ! -f $_MDIR/mixmaster ]
 then
   echo "Please install Mixmaster first."
   exit
 else
  readln "Do you want to support transparent remixing?" $i
  if [ "$ans" = y ]
  then
    _USE_MIX=2
  else
    _USE_MIX=1
  fi
 fi
fi


cd $_RDIR

if [ "$_USE_MIX" = 0 ]
then
  if [ ! "$root" = 1 ]
  then
    readln "Do you have cooperation from root in installing the remailer?" n
    if [ "$ans" = y ]
    then
      NAME=remailer
      root=1
    fi
  fi

  readln "The e-mail address of your remailer:" $NAME@`hostname`
  _RMA=$ans

  if [ "$root" = 1 ]
  then
    _MBOX="${_RDIR}/mbox"
  else
    NAME2=`echo $_RMA | sed 's/@.*//'`
    MBOX=`echo ${MAIL:-/usr/spool/mail/$NAME} | sed "s/$NAME/$NAME2/"`
    if touch $MBOX 2>/dev/null
    then
      _MBOX="$MBOX"
    else
      _MBOX="${_RDIR}/mbox"
    fi
  fi

  readln "An address to appear in the \`From:' line of anonymous messages:" `echo $_RMA | sed 's/.*@/nobody@/'`
  _RAA="$ans"

  readln "Address for complaints to be sent to:" `whoami`@`echo $_RMA | sed 's/.*@//'`
  _CA="$ans"
  echo "Choose a name for your remailer.  It will appear in remailer status messages."
  readln "Long name:" "Anonymous Remailer"
  _RMN="$ans"

  echo "Choose a name to be used in the \`From:' line of remailed messages."
  readln "Anon long name:" "Anonymous"
  _RAN="$ans"

  whereis sendmail /usr/lib/sendmail /usr/sbin/sendmail
  _SENDMAIL=$found
  _MAIL="$_SENDMAIL -t"
  readln "Name and path of sendmail program:" "$_MAIL"
  _MAIL="$ans"


  _ALLOW_POST=0
  echo "Do you want to allow posting to Usenet? Newsgroups can be restricted
using the blocked.newsgroup file.
  y = Yes, post locally;  m = Use mail-to-news gateway;  n = No."
  readln "Allow posting to Usenet?" m
  if [ "$ans" = y ]
  then
    whereis inews /usr/lib/news/inews
    _INEWS=$found
    _NEWS="$_INEWS -h"
    readln "News posting software:" "$_NEWS"
    _NEWS="$ans"
    _ALLOW_POST=1
    readln "Organization line for anonymous Usenet posts:" "Anonymous Posting Service"
    _ORG="$ans"
  fi
  if [ "$ans" = m ]
  then
    readln "Mail-to-news gateway:" mail2news@nym.alias.net
    _M2N="$ans"
    _ALLOW_POST=2
  fi

else
  cd $_MDIR
  _MAIL=`grep '^SENDMAIL' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _RMA=`grep '^REMAILERADDR' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _RAA=`grep '^ANONADDR' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _CA=`grep '^COMPLAINTS' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _RMN=`grep '^REMAILERNAME' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _RAN=`grep '^ANONNAME' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _NEWS=`grep '^NEWS' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _ORG=`grep '^ORGANIZATION' mixmaster.conf | sed 's/^[A-Z]*[ 	]*//'`
  _M2N=`grep '^MAILtoNEWS' mixmaster.conf | sed 's/^[A-Za-z]*[ 	]*//'`
  _MBOX=/dev/null
  if [ "$_NEWS" = mail-to-news ]
  then
    _ALLOW_POST=2
  else
    _ALLOW_POST=1
  fi
  cd $_RDIR
fi

  _ALLOW_LTIME=1
  readln "Do you want to queue messages with Latent-Time: header?" y
  if [ "$ans" = y ]
  then
    readln "Queue directory:" $_RDIR/queue
    _QDIR="$ans"
    if [ ! -d $_QDIR ]
    then
      mkdir $_QDIR
    fi
  else
    _ALLOW_LTIME=0
  fi

  _SIZE_LIMIT=0
  readln "Do you want to set a size limit for the remailer?" n
  if [ "$ans" = y ]
  then
    readln "Maximal message size in bytes (0 means no limit):" 0
    _SIZE_LIMIT="$ans"
  fi

  _USE_STATS=1
  readln "Enable statistics-keeping in the remailer?" y
  if [ "$ans" = n ]
  then
    _USE_STATS=0
  fi

  _USE_SYSLOG=0
  readln "Enable logging with syslog?" n
  if [ "$ans" = y ]
  then
    _USE_SYSLOG=1
  fi

  whereis pgp
  if [ -f "$found" ]
  then
    _PGP="$found"
    i=y
  else
    _PGP=/usr/bin/pgp
    i=n
  fi

echo "Do you want to support PGP?
  y = Yes; e = Accept only encrypted messages; n = No."
  readln "PGP support:" y
  if [ "$ans" != n ]
  then
    if [ "$ans" = e ]
    then
      _ALLOW_PGP=2
    else
      _ALLOW_PGP=1
    fi
    readln "Path to PGP:" $_PGP
    _PGP="$ans"
    readln "PGP keyring directory:" $_RDIR/.pgp
    _PDIR="$ans"
    echo "Pass phrase for your remailer:"
    read _PASS
  else
    _ALLOW_PGP=0
  fi

  if [ "$_ALLOW_PGP" != 0 ]
  then
    whereis wget
    if [ -f "$found" ]
    then
      _WGET="$found"
      i=y
    else
      _WGET=/usr/bin/wget
      i=n
    fi
    readln "Do you want to support anonymous WWW downloading?" $i
    if [ "$ans" = y ]
    then
      echo "URLs can be restricted using the blocked.url file."
      _ALLOW_WWW=1
      readln "Path to GNU Wget utility:" $_WGET
      _WGET="$ans"
    else
      _ALLOW_WWW=0
    fi
  fi

  whereis gpg
  if [ -f "$found" ]
  then
    _GPG="$found"
    i=y
  else
    _GPG=/usr/local/bin/gpg
    i=n
  fi

  echo "Do you want to support GPG (GNU Privacy Guard)?
  y = Yes; n = No."
  echo "Please read http://www.d.shuttle.de/isil/crypt/gnupg.html
for more information about this new encryption program."
  readln "GPG support:" n
  if [ "$ans" = y ]
  then
  readln "Path to GPG:" $_GPG
  _GPG="$ans"
  readln "GPG keyring directory:" $_RDIR/.gnupg
  _GDIR="$ans"
  else
    _ALLOW_GPG=0
  fi

  readln "Is this correct?" y
  if [ "$ans" = n ]
  then
   exit 1
  fi

  cd $_RDIR
  conf_file freedom.conf
  rm -f key.asc

  if [ -f $_RDIR/remailer ]
  then
    echo "Moving old remailer binary to $_RDIR/remailer.bak"
    mv -i $_RDIR/remailer $_RDIR/remailer.bak
  else
    inst_files "blocked.destination blocked.source blocked.newsgroup blocked.url headers.del"
  fi

if [ -d $REM_SRC ]
then
  cd $REM_SRC
  echo "Now building remailer. Please wait."

  $MAKE FREEDOM=$_RDIR

  if [ ! -x remailer ]
  then
    echo "As you can probably see, the build failed.  Read the docs."
    exit 1
  else
    cp remailer $_RDIR
  fi
fi

if [ "$_ALLOW_PGP" != 0 ]
then
  cp $REM_ODIR/repgp-keys.pgp $_RDIR
  chmod 644 $_RDIR/repgp-keys.pgp
  if [ ! -f $_RDIR/passphrase ]
  then
    echo $_PASS > $_RDIR/passphrase
    chmod 600 $_RDIR/passphrase
  fi
  if [ ! -d $_PDIR ]
  then
    mkdir $_PDIR
  fi
  if ! PGPPATH=$_PDIR pgp -kv "$_RMA"
  then
#    cd $_PDIR
    echo
    echo "-----------------------------------------------------------------------------"
    echo "Please generate the PGP key."
    echo "User ID: $_RMN <$_RMA>"
    echo "Pass phrase: $_PASS"
    PGPPATH=$_PDIR pgp -kg
    chmod 600 $_PDIR/randseed.bin $_PDIR/secring.*
    chmod 644 $_PDIR/pubring.*
  fi
  PGPPATH=$_PDIR pgp -kxa "$_RMA" $_RDIR/key.asc
  (cd $_RDIR
  conf_file freedom-key
  cat key.asc >> freedom-key)
fi

if [ "$_ALLOW_GPG" != 0 ]
then
  if [ ! -d $_GDIR ]
  then
    mkdir $_GDIR
    cp $REM_ODIR/pubring.gpg $_GDIR
  fi
  if ! GNUPGHOME=$_GDIR gpg -k "$_RMA"
  then
    echo
    echo "----------------------------------------------------------------------
-------"
    echo "Please generate the GPG key."
    echo "Real name: $_RMN"
    echo "E-mail address: $_RMA"
    echo "Pass phrase: $_PASS"
    GNUPGHOME=$_GDIR gpg --gen-key
    chmod 600 $_GDIR/secring.*
    chmod 644 $_GDIR/pubring.*
  fi
  echo "" >> $_RDIR/key.asc
  GNUPGHOME=$_GDIR gpg -k "$_RMA" >> $_RDIR/key.asc
  echo "" >> $_RDIR/key.asc
  GNUPGHOME=$_GDIR gpg -a --export "$_RMA" >> $_RDIR/key.asc
  echo "" >> $_RDIR/freedom-key
  GNUPGHOME=$_GDIR gpg -k "$_RMA" >> $_RDIR/freedom-key
  echo "" >> $_RDIR/freedom-key
  GNUPGHOME=$_GDIR gpg -a --export "$_RMA" >> $_RDIR/freedom-key
fi

  cd $REM_ODIR
  cat help.in |
  if [ "$_SIZE_LIMIT" != 0 ]
  then
    cat
  else
    sed '/^%%BEGIN_LIMIT/,/^%%END_LIMIT/d'
  fi |
  if [ "$_ALLOW_PGP" != 0 ]
  then
    (cd $_RDIR; sed '/^%%INSERT_PGP_KEY/r key.asc')
  else
    sed '/^%%BEGIN_PGP/,/^%%END_PGP/d'
  fi |
  if [ "$_ALLOW_WWW" = 1 ]
  then
    cat
  else
    sed '/^%%BEGIN_WWW/,/^%%END_WWW/d'
  fi |
  if [ "$_ALLOW_POST" != 0 ]
  then
    cat
  else
    sed '/^%%BEGIN_POST/,/^%%END_POST/d'
  fi |
  if [ "$_USE_MIX" != 0 ]
  then
    cat
  else
    sed '/^%%BEGIN_MIX/,/^%%END_MIX/d'
  fi |
  sed 's/^%%.*//' >freedom-help.in

  cd $_RDIR
  conf_file freedom-help
  rm $REM_ODIR/freedom-help.in

if [ "$_USE_MIX" != 0 ]
then
  cd $_MDIR
  if [ "$_ALLOW_PGP" != 0 ]
  then
    echo "Copying keyinfo.txt to keyinfo.txt.ori."
    cp keyinfo.txt keyinfo.txt.ori
    sed -e "#Bits/#,#^-----END PGP PUBLIC KEY BLOCK-----$#d" \
        -e "#^=-=-=-=-=-=-=-=-=-=-=-=#r $_RDIR/key.asc" <keyinfo.txt.ori >keyinfo.txt
    ./mixmaster -K
  fi
  echo "Copying mix.help to mix.help.ori."
  cp mix.help mix.help.ori
  cp $_RDIR/freedom-help mix.help
  echo "Copying mixmaster.conf to mixmaster.conf.org."
  cp mixmaster.conf mixmaster.conf.ori
  sed "s+^#TYPE1.*+TYPE1		$_RDIR/remailer+" <mixmaster.conf.ori >mixmaster.conf
  cd $REM_ODIR
fi
rm -f $_RDIR/key.asc

  echo "----------------------------------------------------------------------------"

  echo
  echo "Add the following line to your crontab"
  echo "*/10 * * * * ${_RDIR}/remailer -q"
  if crontab -l 2>/dev/null | grep remailer >/dev/null
  then
   echo '(The remailer already is registered in your crontab.)'
  else
    readln "Do that now?" y
    if [ "$ans" = y ]
    then
     (crontab -l 2>/dev/null
      echo "*/10 * * * * ${_RDIR}/remailer -q" ) >$REM_ODIR/inst
      if crontab $REM_ODIR/inst 2>&1 | grep usage >/dev/null 2>/dev/null
      then
        crontab -r $REM_ODIR/inst
      fi
    fi
  fi

  umask 033
  if [ "$_USE_MIX" = 0 ]
  then
  if [ "$root" = 1 ]
  then
    echo
    echo "Add the following line to /etc/aliases:"
    echo "`echo $_RMA | sed 's/@.*//'`: \"|${_RDIR}/remailer $LOG\""
  else
    # Assume that procmail is installed if .procmailrc exists or is called
    # from .forward. Otherwise call the remailer from .forward
    if grep procmail ~/.forward >/dev/null 2>/dev/null
    then
      touch ~/.procmailrc
    fi
    if [ -f ~/.procmailrc ]
    then
      echo
      echo "Add the following lines to .procmailrc:"
    ( echo
      echo ":0 f :remailer.lock"
      echo "|${_RDIR}/remailer $LOG" ) | tee $REM_ODIR/inst
      echo
      if grep remailer ~/.procmailrc >/dev/null
      then
        echo '(The remailer already is registered in your .procmailrc file.)'
      else
        readln "Do that now?" y
        if [ "$ans" = y ]
        then
          cat $REM_ODIR/inst >>~/.procmailrc
        fi
      fi
    else
      echo
      if [ -f ~/.forward ]
      then
        echo "Your current .forward is:"
        cat ~/.forward
      fi
      echo "Set .forward to the following line:"
      echo "\"|${_RDIR}/remailer $LOG\"" | tee $REM_ODIR/inst
      if [ -f ~/.forward ]
      then
        readln "Overwrite?" n
      else
        readln "Do that now?" y
      fi
      if [ "$ans" = y ]
      then
        echo "Mail will be delivered to $_MBOX."
        echo "Please make sure that this works."
        cat $REM_ODIR/inst >~/.forward
      fi
    fi
  fi
fi
echo "Remailer installation complete."
