#!/bin/bash
#
# Configure uptime daemon.
#
# Fredrik Hallenberg <hallon@debian.org>
#

set -ef

TEMPFILE=`tempfile -m 700`

fixargs() {
sed /etc/init.d/ud -e s#ARGS=\".*\"#ARGS=\""$1"\"# > $TEMPFILE
mv /etc/init.d/ud /etc/init.d/ud.old
mv $TEMPFILE /etc/init.d/ud
chmod 0755 /etc/init.d/ud
rm /etc/init.d/ud.old
}

if [ `whoami` != "root" ]; then
  echo "This script must be run by root."
  exit 1
fi

if [ ! -f /etc/init.d/ud ]; then
  echo "Can\'t find /etc/init.d/ud! Please reinstall the ud package or"
  echo "rename /etc/init.d/ud.old to /etc/init.d/ud"
  exit 1
fi

if [ ! -f /etc/template.ud ]; then
  echo "Cant\'t find /etc/template.ud! Please reinstall the ud package"
  exit 1
fi

cat <<EOF
The Uptime Daemon has the ability to generate a HTML page with your current
uptime status. This page is generated by using a template.

EOF

read -p "Do you want ud to generate a HTML page? [yN]: " KEY
case "$KEY" in
  y|Y)
    read -p "Path of the HTML file: " HTMLFILE
    if [ "$HTMLFILE" = "" ]; then
      echo "No changes made."
      exit 0
    fi
    cat <<EOF

If you want to use the default template file /etc/template.ud just press return now.
If not enter the path of the desired template.

EOF
    read -p "Path to template [/etc/template.ud]: " TEMPLATE
    if [ "$TEMPLATE" = "" ]; then
      TEMPLATE="/etc/template.ud"
    fi
    fixargs "-of ${HTMLFILE} -if ${TEMPLATE}"
    ;;
  *)
    fixargs "-s"
    ;;
esac

/etc/init.d/ud restart

echo ""
echo "Configuration updated."

exit 0
