#!/bin/sh
# ============================================================================
#
# This file is part of the 'anyterm zypkg for ZyXEL NSA-nnn'
# http://anyterm.org/
#
# This program is free software; you can redistribute it and/or modify it.
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Author: Mijzelf <Mijzelf@live.com>
#
# ============================================================================
PKGNAME="anyterm"
PKG_ROOT="/usr/local/zy-pkgs"
CONFIG_DIR="${PKG_ROOT}/config/${PKGNAME}"
CONFIG_FILE="${CONFIG_DIR}/config"
PORT=23456

# main start

GetLink()
{
    local ADDR=`/sbin/ifconfig egiga0 | grep "inet addr" | tr -s ' '`
    echo  "http://`echo ${ADDR} | cut -d ' ' -f 2 | cut -d ':' -f 2`/anyterm"
}

Status()
{
    if [ ! -f ${CONFIG_FILE} ] ; then
        mkdir -p ${CONFIG_DIR}
        echo "Disabled" >${CONFIG_FILE}
    fi
    cat ${CONFIG_FILE}
}

insert_line()
{
    local filename=$1
    local action=$2
    local tagline=$3
    local insert=$4
    local all=$5

    if [ "$action" = "before" ] ; then
	action=i
    else
	action=a
    fi

    local linenumbers=` grep -n "$tagline" $filename | sort -r | cut -d : -f 1 `

    [ "" = "$linenumbers" ] && return 1

    if [ "" = "$all" ] ; then
	linenumbers=` echo $linenumbers | tail -n 1 `
    fi

    local ret=0
    for l in $linenumbers
    do
	sed -i "${l}${action} $insert" $filename
	[ $? -ne 0 ] && ret=2
    done

    return $ret
}

Startup()
{
    local STATUS=` Status `
    if [ "$STATUS" != "Enabled" ] ; then 
	return 0
    fi

    local httpd_conf=/etc/service_conf/httpd.conf
    local proxy_conf=/etc/service_conf/proxy.conf
    local restart_apache=0

    if ! grep proxy.conf $httpd_conf 
    then
	insert_line $httpd_conf after httpd_special "Include $proxy_conf"
	restart_apache=1
    fi

    local add_lines=0
    if [ ! -f $proxy_conf ] ; then
	add_lines=1
    elif ! grep "localhost:$PORT" $proxy_conf ] ; then
	add_lines=1
    fi
    
    if [ $add_lines -eq 1 ] ; then
	echo "ProxyPass /$PKGNAME http://localhost:$PORT" >>$proxy_conf
	echo "ProxyPassReverse /$PKGNAME http://localhost:$PORT" >>$proxy_conf
	restart_apache=1
    fi

    /usr/local/zy-pkgs/bin/anytermd -c /bin/login -p $PORT -u nobody
    
    if [ $restart_apache -eq 1 ] ; then
	touch /tmp/restart_httpd
    fi
}

Shutdown()
{
    killall anytermd
}

Disable()
{
    mkdir -p ${CONFIG_DIR}
    echo "Disabled" >${CONFIG_FILE}
    Shutdown 
}

Enable()
{
    mkdir -p ${CONFIG_DIR}
    echo "Enabled" >${CONFIG_FILE}
    Startup 
}

case $1 in
    getlink)
	GetLink
	;;
    status)
	Status
	;;
    startup)
	Startup 
	;;
    shutdown)
	Shutdown
	;;
    enable)
	Enable
	;;
    disable)
	Disable
	;;
esac

exit 0
