#!/bin/sh

if [ "`id -u`" = "0" ]
 then
 HTTP_USER=""

 cat << __EOF__
You are running the test auto-configuration script as root. Older version
of this script produced very insecure configuration as the result (as
it was pointed out in the online documentation), and this is an
attempt of making something reasonable for that case.

You most likely will have to replace the result of this
auto-configuration by something better, however the result of this script or
root-fhttpd.conf files will be reasonable as starting points.

Now you need to enter the username of the user (most likely it will be
"nobody", "www" or "ftp") that HTTP server should use for file access.

__EOF__

 while [ "${HTTP_USER}" = "" ]
  do 
  echo -n "Enter the username for file and CGI access: "
  read HTTP_USER
  if [ "${HTTP_USER}" != "" ]
   then
   HTTP_USER_REAL=`id -un "${HTTP_USER}"`
   if [ "${HTTP_USER_REAL}" = "" ]
    then
    HTTP_USER=""
    else
    if [ "${HTTP_USER_REAL}" != "${HTTP_USER}" ]
     then
     echo -n "User ${HTTP_USER} is really ${HTTP_USER_REAL}, Ok ? [Y/n] :"
     read ANSWER
     if [ "${ANSWER}" != "" -a "${ANSWER}" != "y" -a "${ANSWER}" != "Y" ]
      then HTTP_USER=""
      fi
     fi
    fi
   fi
  done
 else
 HTTP_USER=`whoami`
 cat << __EOF__
You are running the test auto-configuration script as regular user
${HTTP_USER}. This will make usable configuration for fhttpd running as the
same user, and if you need to run fhttpd as root and handle multiple users or
use it as FTP server, you will need to modify the configuration file
significantly or use root-fhttpd.conf - the example fhttpd.conf file for
fhttpd running as root.

__EOF__
 fi

HTTPPORT=9000
FTPPORT=0
PROCESSPORT=9001
CONFIGFILENAME=`pwd`/fhttpd.conf
HTTP_ROOT=`pwd`
BINDIR=`(cd ..;pwd)`
USER=`whoami`

export HTTP_USER HTTPPORT FTPPORT PROCESSPORT
export CONFIGFILENAME HTTP_ROOT BINDIR USER

echo "Configuring..."
if ./localnames.sh 
 then
 cat << __EOF__

${CONFIGFILENAME}
is configured for HTTP server at the port ${HTTPPORT},
processes port ${PROCESSPORT}, no FTP support, HTTP root directory
${HTTP_ROOT},
binaries at
${BINDIR},
server accessing files and CGI as the user ${HTTP_USER}
with the public access and private access for the user ${USER}.

Run ./runserver.sh to start fhttpd.
__EOF__

 else
 echo "Auto-configuration failed."
 fi
