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

set -ef

TEMPFILE=`tempfile -m 644`
CONFFILE="/etc/default/ud"

fixargs() {
	sed -e s?OPTIONS=\"[^\"]*\"?OPTIONS=\""$1"\"? $CONFFILE > $TEMPFILE
	mv $TEMPFILE $CONFFILE
}

if [ `id -u` != "0" ]; 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/ud/template.html ]; then
  echo "Cant\'t find /etc/ud/template.html! 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/ud/template.html just press return now.
If not enter the path of the desired template.

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

/etc/init.d/ud restart

echo ""
echo "Configuration updated."

exit 0
