#!/bin/sh

# [$1] -- source html
# [$2] -- new Documentroot
# [$3] -- target html

HSHARE="/usr/share/doc"
HROOT="/alt-docs/indexhtml"
HDIR="$HSHARE$HROOT"
DROOT="/var/www/apache2/html"
OTHER="${1:-$HDIR/first.html}"
NROOT="${2:-default}"
INDEX="${3:-index0.html}"

sed    '/<div class="header">/,$d' "$HDIR/index.html" > "$HDIR/$INDEX"
sed -n '/<div class="header">/,$p' "$OTHER" >> "$HDIR/$INDEX"

if [ "$NROOT" != "-" ]; then
  mkdir -p "$DROOT/$NROOT"
  sed -i 's@DocumentRoot ".*"@DocumentRoot "'"$DROOT/$NROOT"'"@
  s@<Directory "'"$DROOT"'.*">@<Directory "'"$DROOT/$NROOT"'>@
  ' /etc/httpd2/conf/sites-available/default.conf
  # There is no Directory $DROOT in default_https.conf; is that right?
  sed -i 's@DocumentRoot ".*"@DocumentRoot "'"$DROOT/$NROOT"'"@
  s@<Directory "'"$DROOT"'.*">@<Directory "'"$DROOT/$NROOT"'>@
  ' /etc/httpd2/conf/sites-available/default_https.conf
  cat > "$DROOT/$NROOT"/index.html <<@@@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <meta http-equiv="refresh" content="0; URL=$HROOT/$INDEX">
    </head>
    <body></body>
</html>
@@@
fi

if [ "`service httpd2 status`" = "httpd2 is running" ]; then
  service httpd2 restart
fi
