#!/bin/sh

files="shibboleth2.xml"

# Determine whether to use echo -n or \c to echo without a trailing newline.
case "`echo -n`" in
-n)
  n=''
  c='\c'
  ;;
*)
  n='-n'
  c=''
  ;;
esac

echononewline()
{
  echo $n "$@$c"
}

canonicalize_hostname()
{
  case "$1" in
  *.*)
    name="$1"
    ;;    
  *)
    name="$1.mit.edu"
    ;;
  esac
  echo "$name" | tr '[:upper:]' '[:lower:]'
}

# Get the path for the server key file.
get_full_path()
{
  prompt="$1"
  path=
  while [ -z "$path" ]; do
    echo "" 1>&2
    echononewline "Enter the full path for the $prompt: " 1>&2
    read path
    case "$path" in
    /*)
      ;;
    *)
      echo "Please enter a full path" 1>&2
      path=
      ;;
    esac
    if [ -n "$path" -a ! -f "$path" ]; then
      echo "$path is not a valid file" 1>&2
      path=
    fi
  done
  echo "$path"
}

default_hostname=`hostname`
default_hostanme=`canonicalize_hostname "$default_hostname"`

# Make sure we are in the right place.
pkgsysconfdir=`pwd`
case "$pkgsysconfdir" in
/etc/shibboleth)
  prefix=/usr
  ;;
*/etc/shibboleth)
  prefix=`echo "$pkgsysconfdir" | sed -e 's:\(.*\)/etc/shibboleth$:\1:'`
  ;;
*)
  echo "Cannot determine Shibboleth install prefix." 1>&2
  echo "Please cd to the Shibboleth configuration directory to run this" 1>&2
  echo "(e.g. /etc/shibboleth or \$prefix/etc/shibboleth)." 1>&2
  exit 1
  ;;
esac

if [ ! -e shibboleth2.xml.in ]; then
  echo "Please download the shibboleth2.xml.in template file." 1>&2
  exit 1
fi

while [ -z "$hostname" ]; do
  echo ""
  echononewline "Enter the web server host name: [$default_hostname] "
  read hostname
  if [ -z "$hostname" ]; then
    hostname="$default_hostname"
  fi
  hostname=`canonicalize_hostname "$hostname"`
done

case `uname` in
SunOS)
  files="$files shibd shibd-wrapper"
  if [ -d "/usr/athena/lib" ]; then
    default_ssldir=/usr/athena/lib
  elif [ -d "/usr/local/ssl/lib" ]; then
    default_ssldir=/usr/local/ssl/lib
  fi
  while [ -z "$ssldir" ]; do
    echo ""
    echononewline "Enter the OpenSSL library directory: [$default_ssldir] "
    read ssldir
    if [ -z "$ssldir" ]; then
      ssldir="$default_ssldir"
    fi
    if [ ! -d "$ssldir" ]; then
      echo "$ssldir is not a valid directory" 1>&2
      ssldir=
    fi
  done
  ;;
Linux)
  if [ -d "/usr/athena/lib" ]; then
    ssldir=/usr/athena/lib
  else
    ssldir=/lib
  fi
  ;;
esac

certpath=`get_full_path "server certificate file"`
keypath=`get_full_path "server private key file"`

echo ""
echononewline "Always use SSL for Shibboleth handler? [Y] "
read handlerSSL
handlerSSL=`echo $handlerSSL | tr '[:upper:]' '[:lower:]'`
case $handlerSSL in
true|y|yes|"")
  handlerSSL=true
  ;;
*)
  handlerSSL=false
  ;;
esac

echo ""
echononewline "Will this server be joining the InCommon Federation? [N] "
read incommon
incommon=`echo $incommon | tr '[:upper:]' '[:lower:]'`
case $incommon in
true|y|yes)
  begin_incommon='<!-- Begin InCommon addition -->'
  end_incommon='<!-- End InCommon addition -->'
  if [ ! -e incommon.pem ]; then
    url="https://wayf.incommonfederation.org/bridge/certs/incommon.pem"
    echo "Downloading InCommon metadata signing certificate..."
    wget -q "$url" || {
      echo "Warning -- Failed to download $url" 1>&2
      echo "You must download this certificate in order to validate" 1>&2
      echo "the published InCommon metadata." 1>&2
    }
    echo ""
  fi
  ;;
*)
  begin_incommon='<!--'
  end_incommon='-->'
  ;;
esac

echo "Using prefix $prefix..."
pkgxmldir=$prefix/share/xml/shibboleth
libexecdir=$prefix/libexec
# XXX
varrundir=/var/run

for file in $files ; do
  tmpfile=$file.$$
  sed -e "s:%%HOSTNAME%%:$hostname:" \
      -e "s:%%SSLDIR%%:$ssldir:" \
      -e "s:%%KEYPATH%%:$keypath:" \
      -e "s:%%CERTPATH%%:$certpath:" \
      -e "s:%%HANDLERSSL%%:$handlerSSL:" \
      -e "s:%%BEGIN_INCOMMON%%:$begin_incommon:" \
      -e "s:%%END_INCOMMON%%:$end_incommon:" \
      -e "s:@-PKGXMLDIR-@:$pkgxmldir:" \
      -e "s:@-PKGSYSCONFDIR-@:$pkgsysconfdir:" \
      -e "s:@-LIBEXECDIR-@:$libexecdir:" \
      -e "s:@-VARRUNDIR-@:$varrundir:" \
      -e "s:@-PREFIX-@:$prefix:" \
    < $file.in > $tmpfile || exit 1
  if [ -f "$file" ]; then
    echo "$file already exists, saving previous version as $file.old" 1>&2
    mv $file $file.old
  fi
  case $file in
  shibd-wrapper)
    mv $tmpfile $prefix/sbin/$file
    chmod 755 $prefix/sbin/$file
    ;;
  *)
    mv $tmpfile $file
    chmod 644 $file
    ;;
  esac
done
