#-------------------------------------------------------------------------
# /etc/rc.d/rc430.httpd - start Webserver
# was file /etc/rc.d/rc350.httpd
#
# Creation:     14.11.2000  tp
# Last Update:  $Id: rc430.httpd 10948 2006-02-07 16:50:52Z hh $
#-------------------------------------------------------------------------

case $OPT_HTTPD in
    yes)
        begin_script HTTPD "Starting webserver ..."

        mkdir -p /etc/httpd

        # Make passwords for all users
        if [ "$HTTPD_USER_N" -gt 0 ]
        then
            > /etc/httpd/htpasswd
            > /etc/httpd/rights

            idx=1
            [ 0$HTTPD_USER_N -eq 0 ] || for idx in `seq 1 $HTTPD_USER_N`
            do
                eval user='$HTTPD_USER_'$idx'_USERNAME'
                eval pass='$HTTPD_USER_'$idx'_PASSWORD'
                eval rights='$HTTPD_USER_'$idx'_RIGHTS'
                echo "$user:$pass" >> /etc/httpd/htpasswd
                echo "$user=$rights" >> /etc/httpd/rights
            done
        fi

        # Create menu entries in dependece of available services
        # Only show summary, timetable and connections if imond is available
        case $START_IMOND in
            yes)
                httpd-menu.sh add status.cgi '$_MP_ov' '$_MT_stat'
                # check if there are circuits defined
                case `{ echo "circuits" ; echo "quit"; } | netcat localhost $IMOND_PORT | sed 's/.$//; s/OK //'` in
                    0) ;;
                    *) httpd-menu.sh add timetable.cgi '$_MP_timetb' '$_MT_stat' ;;
                esac
                case $IMOND_LOG in
                    yes) httpd-menu.sh add connections.cgi '$_MP_cnlst' '$_MT_log' ;;
                esac
            ;;
        esac
        # Only show calls if telmond is available
        case $OPT_TELMOND$TELMOND_LOG in
            yesyes) httpd-menu.sh -s add calls.cgi '$_MP_calls' '$_MT_log' ;;
        esac

        # Only show syslog, if a target is in a local directory
        case $OPT_SYSLOGD in
            yes) 
                found="no"
                while read target to
                do
                    if [ -f "$to" ]
                    then
                        found="yes"
                    fi
                done < /etc/syslog.conf
                case $found in yes) httpd-menu.sh add syslog.cgi "Syslog" '$_MT_log' ;; esac
            ;;
        esac

        # Add packet filter section
        httpd-menu.sh add pf.cgi '$_MP_pf' '$_MT_stat'

        # Add conntrack-table
        httpd-menu.sh add conntrack.cgi '$_MP_contrck' '$_MT_stat'

        # Add support info section
        httpd-menu.sh add support.cgi '$_MP_supp' '$_MT_hlp'

        # HTTPD options
        httpd_options='-D -c **.cgi'

        [ "$HTTPD_PORT" ] && httpd_options="$httpd_options -p $HTTPD_PORT"
        [ "$HTTPD_USER" ] && httpd_options="$httpd_options -u $HTTPD_USER" || httpd_options="$httpd_options -u root"

        # extract the active GUI Language and save it in HTTPD_GUI_LANG
        # all available languages are stored in the HTTPD_AVAIL_LANGS variable
        case $HTTPD_GUI_LANG in
            auto) HTTPD_GUI_LANG="$LOCALE" ;;
        esac
        HTTPD_AVAIL_LANGS=$HTTPD_GUI_LANG
        set -- $HTTPD_AVAIL_LANGS
        HTTPD_GUI_LANG="$1"
        
        # write httpd variables and all needed system variables to /var/run/mini_httpd.conf
        set | grep -e "^HTTPD" -e "^HOSTNAME" -e "^DOMAIN_NAME" \
                   -e "DNSDHCP_LEASES_DIR" -e "OPT_DNSDHCP" \
                   -e "^IMOND_PORT" -e "^TELMOND_PORT" > /var/run/mini_httpd.conf

        ln -s /etc/httpd/htpasswd /srv/www/admin/.htpasswd
        #ln -s /etc/httpd/htpasswd /srv/www/include/.htpasswd

        # to support legacy opts we have to create links
        [ -d /usr/local/htdocs ] && /etc/mklinks.sh /usr/local/htdocs /srv/www/admin
        cd /srv/www

        #-------------------------------------------------------------------------
        # start mini_httpd and restart it, if it is killed
        #-------------------------------------------------------------------------
        set -f
        service-restart.sh 0 /usr/sbin/mini_httpd $httpd_options &
        set +f
        cd /

        end_script
    ;;
esac
