#!/bin/sh 
#
# Interactively configure fidogate for Debian
#
# Marco Budde <Budde@tu-harburg.d400.de>

DIALOG_OPT="--backtitle fidogate"


PATH="/usr/sbin:/usr/bin:/sbin:/bin"

reload="yes"
umask 022

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

while [ $# -gt 0 ]
do
    case "$1" in
	--no-reload)
	    reload=""
	    shift
	;;

	*)
	    echo "Usage: $0 [--no-reload]" >&2
	    exit 1
	;;
    esac
done

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


function input() {
    local q="$1" def=$(eval "echo -n \$$2")

    echo -n "$q? [$def] "
    read REPLY
    REPLY=$(echo -n "$REPLY" | sed -e 's/^[ 	]*//' -e 's/[ 	]*$//')
    test -n "$REPLY" || REPLY="$def"

    if [ NONE = "$3" ]
    then
	test NONE != "$REPLY" || REPLY=""
    fi

    eval "$2=\"$REPLY\""
}

function testconfig() {
    CONFIG=$(grep "$2" "$1" 2>/dev/null)
}

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

function parseaka() {
  AKA2=`echo $AKA | sed -e 's/:/ /' | sed -e 's/\// /' | sed -e 's/\./ /'`
  Z=`echo $AKA2 | awk -F ' ' '{ print $1 }'`
  N=`echo $AKA2 | awk -F ' ' '{ print $2 }'`
  F=`echo $AKA2 | awk -F ' ' '{ print $3 }'`
  P=`echo $AKA2 | awk -F ' ' '{ print $4 }'`
}


function sendmailcf() {
  if [ ! -f /usr/sbin/sendmail ]; then
    echo "Please install sendmail!"
    return
  fi 

  grep -s -q mailertable /etc/mail/sendmail.mc
  if [ $? -ne 0 ]; then
    echo "FEATURE(mailertable)" >> /etc/mail/sendmail.mc
  fi

  grep -s -q "MAILER(ftn)" /etc/mail/sendmail.mc
  if [ $? -ne 0 ]; then
    echo "MAILER(ftn)" >> /etc/mail/sendmail.mc
  fi

  dialog ${DIALOG_OPT} --infobox \
  "Generating /etc/mail/sendmail.cf from /etc/mail/sendmail.mc ..." 3 70

  m4 /usr/lib/sendmail.cf/m4/cf.m4 /etc/mail/sendmail.mc  \
      > /etc/mail/sendmail.cf.new
  mv -f /etc/mail/sendmail.cf.new /etc/mail/sendmail.cf

  if [ "$reload" ] 
  then
    dialog ${DIALOG_OPT} --infobox "Reloading sendmail ..." 3 60
    /etc/init.d/sendmail reload >/dev/null
  fi
}

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

function inndcf () {
  if [ ! -f /usr/sbin/innd ]; then
    "Please install innd!"
    return
  fi

  
  echo "# a FIDO feet, all fido.* newsgroups are send to to the gate," >> /etc/news/newsfeeds 
  echo "# please read the fidogate documentation carefully to avoid" >> /etc/news/newsfeeds
  echo "# dupes" >>/etc/news/newsfeeds
  echo "#" >> /etc/news/newsfeeds
  echo -n "# fidogate/" >> /etc/news/newsfeeds

  echo $AKA | sed -e 's/:/ /' | sed -e 's/\// /' | sed -e 's/\./ /' | \
   awk -F ' ' '{ print "p"$4".f"$3".n"$2".z"$1".fidonet.org \\" }' \
   >> /etc/news/newsfeeds


  echo "#       :fido.* \\" >> /etc/news/newsfeeds
  echo "#       :Tf,Wnb:" >> /etc/news/newsfeeds

  dialog ${DIALOG_OPT} --msgbox "You have to edit the newsfeed file!" 5 60
}

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

### dialog installed? ###

if [ ! -x /usr/bin/dialog ]; then
  echo "Please install dialog"
  exit
fi

### run fidogateconfig? ###

dialog ${DIALOG_OPT} --yesno "Using this script you can set up a \
simple Fido Point. Do you want to configure FIDOGATE now?" 6 70

if [ $? = 1 ]; then
  exit
fi

### Fido AKA ###

testconfig /etc/fido/config.gate Address
AKA=$(echo $CONFIG | awk -F ' ' '{ print $2 }')

dialog ${DIALOG_OPT} --title "Fido AKA" \
--inputbox "Enter your main Fido Point AKA (my main AKA is for example \
2:240/5202.15)" 9 70 ${AKA} 2> /tmp/$$.tmp

AKA=`cat /tmp/$$.tmp`
parseaka 

### Archiver ###

dialog --title "Archiver" \
--menu "Which archiver should we use for "${AKA}"?" 13 70 5 \
arc "Archiver arc" rar "Archiver rar" tgz "Archiver tar/gz" zip \
"Archiver zip" 2> /tmp/$$.tmp

ARC=`cat /tmp/$$.tmp`

### config.common ###

cat << EOT > /etc/fido/config.common
Zone  default   .fidonet.org            fidonet         -
Zone    1       .fidonet.org            fidonet         out.001
Zone    2       .fidonet.org            fidonet         out.002
Zone    3       .fidonet.org            fidonet         out.003
Zone    4       .fidonet.org            fidonet         out.004
Zone    5       .fidonet.org            fidonet         out.005
Zone    6       .fidonet.org            fidonet         out.006
Zone   21       .gernet.heise.de        gernet          out.015
EOT

if [ -f /etc/news/organization ]; then
  ORG=`cat /etc/news/organization`
  echo 'Organization    "'${ORG}'"' >> /etc/fido/config.common
fi

echo "Origin          \"Debian GNU/Linux\"" >> /etc/fido/config.common
echo "HostName        "`hostname`  >> /etc/fido/config.common
echo "Domain          "`hostname -d` >> /etc/fido/config.common

### config.gate ###

echo "include 		/etc/fido/config.common" > /etc/fido/config.gate
echo "EchoMail4D"   >> /etc/fido/config.gate
echo "NetMail8bit"  >> /etc/fido/config.gate
echo 'FTNInSendMail "/usr/sbin/sendmail -oi -odq -oee -f%s -oMs$RELAY -oMr$PROTO -t"' >> /etc/fido/config.gate
echo 'FTNInrnews "/usr/bin/rnews"' >> /etc/fido/config.gate
echo "Address " $AKA >> /etc/fido/config.gate
echo "Uplink  " $AKA >> /etc/fido/config.gate

### config.main ###

echo "include         /etc/fido/config.common" > /etc/fido/config.main
echo "AreasBBS        /etc/fido/areas.bbs" >> /etc/fido/config.main
echo "DupeCheck" >> /etc/fido/config.main
echo "KillDupes" >> /etc/fido/config.main
echo "Address		" $AKA >> /etc/fido/config.main

### routing ###

echo "NetMail" > /etc/fido/routing
echo "route    hold"  $AKA >> /etc/fido/routing
echo "route    normal" ${Z}":"${N}"/"${F}  "1:* 2:* 3:* 4:* 5:* 6:*" >> /etc/fido/routing
echo "EchoMail" >> /etc/fido/routing
echo "sendmove hold"  $AKA >> /etc/fido/routing
echo "sendmove normal *" >> /etc/fido/routing

### packing ###

echo 'arc  zip    "/usr/bin/zip -9gkjq %s %s"' > /etc/fido/packing
echo 'arc  tgz    "/usr/bin/tar cvfz %s %s"' >> /etc/fido/packing
echo 'arc  rar    "/usr/bin/rar a -ep -s -m3 -inul %s %s"' >> /etc/fido/packing
echo 'arc  arc    "/usr/bin/arc an %s %s"' >> /etc/fido/packing
echo "pack noarc " $AKA >> /etc/fido/packing
echo "pack "${ARC}" *" >> /etc/fido/packing

### passwd ###

touch /etc/fido/passwd

### fidopoll.conf ###

echo "UPLINK=f"${F}".n"${N}".z"${Z}  > /etc/fido/fidopoll.conf

### /etc/mailertable ###

if [ -f /etc/mailertable ]; then
  grep -v ftn: /etc/mailertable > /etc/mailertable.new
fi

echo $AKA | sed -e 's/:/ /' | sed -e 's/\// /' | sed -e 's/\./ /' | \
  awk -F ' ' '{ print ".fidonet.org   ftn:p"$4".f"$3".n"$2".z"$1".fidonet.org" }' >> /etc/mailertable.new
mv /etc/mailertable.new /etc/mailertable

if [ -f /usr/sbin/makemap ]; then
  /usr/sbin/makemap hash /etc/mailertable < /etc/mailertable
fi

### /etc/sendmail.cf ###

sendmailcf

### /etc/news/newsfeeds ###

inndcf

### clear ###

rm -f /tmp/$$.tmp

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

#if yesno "Do you receive Internet e-mails or news from a Internet to Fido
#gateway?" N 
#then
#  testconfig /etc/fido/config.gate Gateway
#  GATEWAY=$CONFIG
#  input "Gateway Fido AKA" GATEWAY
#  echo "Gateway " $GATEWAY >> config.gate
#fi
