#!/bin/sh
# Copyright 2002-2003 by Kai 'Oswald' Seidler, oswald@apachefriends.org, GPL-licensed
# 2004-02-03: Kocil
#  - add status (pls correct the de langguage)
#  - saved as httpd (keep the original)

function testport() {
	if netstat -an | egrep ":$1 .*LISTEN" > /dev/null
	then
		return 0
	else
		return 1
	fi
}

function testrun() {
	if test -f $1
	then
		pid=`cat $1`
		if ps -ax 2>/dev/null | egrep "^ *$pid.*$2" > /dev/null
		then
			return 0
		else
			rm $1
			return 1
		fi
	else
		return 1
	fi
}

function osguess() {
	if test -f /etc/redhat-release
	then
		if egrep "9 " /etc/redhat-release > /dev/null
		then
			echo "rh9"
			return 0
		fi
	else
		echo "unknown"
		return 0
	fi
}

# do we have that new red hat linux 9 with posix native threads?
if test "`osguess`" = "rh9"
then
	# for now disable PNTL. if PNTL gets more popular we will support it. - oswald [8apr3]
	export LD_ASSUME_KERNEL=2.2.5
	#echo "LAMPP: DISABLE PNTL..."
fi

export LD_LIBRARY_PATH=/opt/lampp/lib

de="false"
case $LANG in
        de*) de="true";;
esac

if test ! -f /opt/lampp/htdocs/xampp/lang.tmp
then
	#$de && echo -n "de" > /opt/lampp/htdocs/xampp/lang.tmp
	#$de || echo -n "en" > /opt/lampp/htdocs/xampp/lang.tmp
	touch /opt/lampp/htdocs/xampp/lang.tmp
	chown nobody /opt/lampp/htdocs/xampp/lang.tmp
	chmod 600 /opt/lampp/htdocs/xampp/lang.tmp
fi

iswebmin="false";
if test -d /opt/lampp/webmin
then
	iswebmin="true";
fi

version=`cat /opt/lampp/lib/VERSION`
bon="[1m"
boff="[m"
lc="/opt/lampp/etc/lampp"


case $1 in
	"start")
		$de && echo "Starte XAMPP fr Linux $version..."
		$de || echo "Starting XAMPP for Linux $version..."

		/opt/lampp/share/lampp/nogroupcheck

		$0 startapache %
		$0 startmysql %
		$0 startftp %
		$iswebmin && $0 startwebmin %

		$de && echo "XAMPP fr Linux gestartet."
		$de || echo "XAMPP for Linux started."
		;;

	"reload")
		$0 reloadapache
		$0 reloadmysql
		$0 reloadftp
		;;

	"stopssl")
		if test -f $lc/startssl
		then
			if test -f /opt/lampp/logs/httpd.pid
			then
				$0 stopapache
				rm $lc/startssl
				$0 startapache
			else
				$de && echo "LAMPP: LAMPP-Apache luft gar nicht."
				$de || echo "LAMPP: LAMPP-Apache is not running."
				rm $lc/startssl
			fi
		else
			$de && echo "LAMPP: SSL luft nicht!"
			$de || echo "LAMPP: SSL isn't running!"
		fi
		;;

	"startssl") 
		if testrun /opt/lampp/logs/httpd.pid httpd
		then
			if test -f $lc/startssl
			then
				$de && echo "LAMPP: SSL luft bereits!"
				$de || echo "LAMPP: SSL is already running!"
			else
				$0 stopapache
				touch $lc/startssl
				$0 startapache
			fi
		else
			touch $lc/startssl
			$0 startapache
		fi
		;;

	"startapache")
		if testrun /opt/lampp/logs/httpd.pid httpd
		then
			$de && echo "LAMPP: LAMPP-Apache luft bereits."
			$de || echo "LAMPP: LAMPP-Apache is already running."
		else
			if testport 80
			then
				$de && echo "LAMPP: Ein Apache luft bereits."
				$de || echo "LAMPP: An Apache daemon is already running."
			else
				if test -f $lc/startssl
				then
					$de && echo "LAMPP: Starte Apache mit SSL..."
					$de || echo "LAMPP: Starting Apache with SSL..."
					/opt/lampp/bin/apachectl startssl > /dev/null
				else
					$de && echo "LAMPP: Starte Apache..."
					$de || echo "LAMPP: Starting Apache..."
					/opt/lampp/bin/apachectl start > /dev/null
				fi
				error=$?
				if test $error -gt 0
				then
					$de && echo "LAMPP: ${bon}Fehler $error beim Starten vom Apache!${boff}"
					$de || echo "LAMPP: ${bon}Error $error! Couldn't start Apache!${boff}"
				fi
			fi
		fi
		;;


	"startmysql")
		if testrun /opt/lampp/var/mysql/`/bin/hostname`.pid mysqld
		then
			$de && echo "LAMPP: LAMPP-MySQL luft bereits."
			$de || echo "LAMPP: LAMPP-MySQL is already running."
		else
			if testport 3306
			then
				$de && echo "LAMPP: Ein MySQL daemon luft bereits."
				$de || echo "LAMPP: A MySQL daemon is already running."
			else
				/opt/lampp/bin/mysql.server start > /dev/null &
				$de && echo "LAMPP: Starte MySQL..."
				$de || echo "LAMPP: Starting MySQL..."
			fi
		fi
		;;
		
	"startwebmin")
		if $iswebmin 
		then
			if test "$2" != "%"
			then
				touch $lc/startwebmin
			fi
			if test -f $lc/startwebmin
			then
				if testrun /opt/lampp/var/webmin/miniserv.pid miniserv
				then
					$de && echo "LAMPP: Webmin luft bereits."
					$de || echo "LAMPP: Webmin is already running."
				else
					$de && echo "LAMPP: Starte Webmin..."
					$de || echo "LAMPP: Starting Webmin..."
					/opt/lampp/etc/webmin/start quiet
				fi
			fi
		else
			$de && echo "LAMPP: Webmin ist nicht installiert."
			$de || echo "LAMPP: Webmin isn't installed."
		fi
		;;

	"stopwebmin")
		if $iswebmin 
		then
			if test -f $lc/startwebmin
			then
				if testrun /opt/lampp/var/webmin/miniserv.pid miniserv
				then
					$de && echo "LAMPP: Stoppe Webmin..."
					$de || echo "LAMPP: Stopping Webmin..."
					/opt/lampp/etc/webmin/stop quiet
				else
					$de && echo "LAMPP: LAMPP-Webmin luft gar nicht."
					$de || echo "LAMPP: LAMPP-Webmin is not running."
				fi
				if test "$2" != "%"
				then
					rm $lc/startwebmin 2> /dev/null
				fi
			fi
		else
			$de && echo "LAMPP: Webmin ist nicht installiert."
			$de || echo "LAMPP: Webmin isn't installed."
		fi
		;;

	"startftp")
		if test "$2" != "%"
		then
			touch $lc/startftp
		fi
		if test -f $lc/startftp
		then
			if testrun /opt/lampp/var/proftpd.pid proftpd
			then
				$de && echo "LAMPP: LAMPP-ProFTPD luft bereits."
				$de || echo "LAMPP: LAMPP-ProFTPD is already running."
			else
				if testport 21
				then
					$de && echo "LAMPP: Ein FTP daemon luft bereits."
					$de || echo "LAMPP: A FTP daemon is already running."
				else
					$de && echo "LAMPP: Starte ProFTPD..."
					$de || echo "LAMPP: Starting ProFTPD..."
					/opt/lampp/sbin/proftpd > /opt/lampp/var/proftpd/start.err 2>&1
					error=$?
					if test $error -gt 0
					then
						echo "LAMPP: `cat /opt/lampp/var/proftpd/start.err`"
						$de && echo "LAMPP: ${bon}Fehler $error beim Starten von ProFTPD!${boff}"
						$de || echo "LAMPP: ${bon}Error $error! Couln't start ProFTPD!${boff}"
					fi
				fi
			fi
		fi
		;;

	"stop")
		$de && echo "Stoppe XAMPP fr Linux $version..."
		$de || echo "Stopping XAMPP for Linux $version..."

		$0 stopapache %
		$0 stopmysql %
		$0 stopftp %
		$iswebmin && $0 stopwebmin %

		$de && echo "LAMPP beendet."
		$de || echo "LAMPP stopped."
		;;

	"stopapache")
		if test -f /opt/lampp/logs/httpd.pid
		then
			if test -f $lc/startssl
			then
				$de && echo "LAMPP: Stoppe Apache mit SSL..."
				$de || echo "LAMPP: Stopping Apache with SSL..."
			else
				$de && echo "LAMPP: Stoppe Apache..."
				$de || echo "LAMPP: Stopping Apache..."
			fi
			/opt/lampp/bin/apachectl stop > /dev/null
			sleep 2
			#test -f /opt/lampp/logs/httpd.pid && rm /opt/lampp/logs/httpd.pid
		else
			$de && echo "LAMPP: LAMPP-Apache luft gar nicht."
			$de || echo "LAMPP: LAMPP-Apache is not running."
		fi
		;;

	"reloadapache")
		if test -f /opt/lampp/logs/httpd.pid
		then
			kill -HUP `cat /opt/lampp/logs/httpd.pid`
			$de && echo "LAMPP: Aktualisiere Apache..."
			$de || echo "LAMPP: Reload Apache..."
		else
			$de && echo "LAMPP: Apache luft nicht..."
			$de || echo "LAMPP: Apache isn't running..."
		fi
		;;


	"stopmysql")
		if test -f /opt/lampp/var/mysql/`/bin/hostname`.pid
		then
			/opt/lampp/bin/mysql.server stop > /dev/null 2>&1 &
			$de && echo "LAMPP: Stoppe MySQL..."
			$de || echo "LAMPP: Stopping MySQL..."
			sleep 2
			test -f /opt/lampp/var/mysql/`/bin/hostname`.pid && rm /opt/lampp/var/mysql/`/bin/hostname`.pid
		else
			$de && echo "LAMPP: LAMPP-MySQL luft gar nicht."
			$de || echo "LAMPP: LAMPP-MySQL is not running."
		fi
		;;

	"reloadmysql")
		if test -f /opt/lampp/var/mysql/`/bin/hostname`.pid
		then
			h="`/bin/hostname`"
			kill -HUP `cat /opt/lampp/var/mysql/$h.pid`
			$de && echo "LAMPP: Aktualisiere MySQL..."
			$de || echo "LAMPP: Reload MySQL..."
		else
			$de && echo "LAMPP: MySQL luft nicht..."
			$de || echo "LAMPP: MySQL isn't running..."
		fi
		;;

	"stopftp")
		if test -f /opt/lampp/var/proftpd.pid
		then
			kill `cat /opt/lampp/var/proftpd.pid`
			$de && echo "LAMPP: Stoppe ProFTPD..."
			$de || echo "LAMPP: Stopping ProFTPD..."
		else
			if test -f $lc/startftp
			then
				$de && echo "LAMPP: LAMPP-ProFTPD luft gar nicht."
				$de || echo "LAMPP: LAMPP-ProFTPD is not running."
			fi
		fi
		if test "$2" != "%"
		then
			rm $lc/startftp 2> /dev/null
		fi
		;;

	"reloadftp")
		if test -f /opt/lampp/var/proftpd.pid
		then
			kill -HUP `cat /opt/lampp/var/proftpd.pid`
			$de && echo "LAMPP: Aktualisiere ProFTPD..."
			$de || echo "LAMPP: Reload ProFTPD..."
		else
			$de && echo "LAMPP: ProFTPD luft gar nicht..."
			$de || echo "LAMPP: ProFTPD isn't running..."
		fi
		;;

	"wizard")
		/opt/lampp/bin/php /opt/lampp/share/lampp/wizard.php
		;;

	"restart")
		$0 stop
		sleep 1
		$0 start
		;;

	"security")
		/opt/lampp/share/lampp/checkall
		;;

	"oci8")
		/opt/lampp/share/lampp/oci8install
		;;
		
	"status")
		if testrun /opt/lampp/logs/httpd.pid httpd
		then
			$de && echo "LAMPP: LAMPP-Apache luft bereits."
			$de || echo "LAMPP: LAMPP-Apache is running."
		else
			if testport 80
			then
			    $de && echo "LAMPP: Ein Apache luft bereits."
			    $de || echo "LAMPP: A HTTP daemon is already running."
			else
		
			    $de && echo "LAMPP: LAMPP-Apache luft gar nicht."
			    $de || echo "LAMPP: LAMPP-Apache is not running."
			fi
		fi

		if testrun /opt/lampp/var/mysql/`/bin/hostname`.pid mysqld
		then
			$de && echo "LAMPP: LAMPP-MySQL luft bereits."
			$de || echo "LAMPP: LAMPP-MySQL is running."
		else
			if testport 3306
			then
				$de && echo "LAMPP: Ein MySQL daemon luft bereits."
				$de || echo "LAMPP: A MySQL daemon is already running."
			else
				$de && echo "LAMPP: MySQL luft nicht..."
				$de || echo "LAMPP: MySQL is not running."
			fi
		fi

		if testrun /opt/lampp/var/proftpd.pid proftpd
		then
			$de && echo "LAMPP: LAMPP-ProFTPD luft bereits."
			$de || echo "LAMPP: LAMPP-ProFTPD is running."
		else
			if testport 21
			then
				$de && echo "LAMPP: Ein FTP daemon luft bereits."
				$de || echo "LAMPP: A FTP daemon is running."
			else
				$de && echo "LAMPP: LAMPP-ProFTPD luft gar nicht."
				$de || echo "LAMPP: LAMPP-ProFTPD is not running."
			fi
		fi
		;;
		
	*)	if $de 
		then
			echo "Usage: $0 <action>"
			echo ""
			echo "	start        Starte LAMPP (Apache, MySQL und evtl. weitere)"
			echo "	startapache  Starte nur den Apache"
			echo "	startssl     Starte nur die SSL-Untersttzung des Apache"
			echo "	startmysql   Starte nur den MySQL"
			echo "	startftp     Starte nur den ProFTPD"
			$iswebmin && echo "	startwebmin  Starte nur Webmin"
			echo ""
			echo "	stop         Stoppe LAMPP (Apache, MySQL und evtl. weitere)"
			echo "	stopapache   Stoppe nur den Apache"
			echo "	stopssl      Stoppe nur die SSL-Untersttzung des Apache"
			echo "	stopmysql    Stoppe nur den MySQL"
			echo "	stopftp      Stoppe nur den ProFTPD"
			$iswebmin && echo "	stopwebmin   Stoppe nur Webmin"
			echo ""
			echo "	reload       Aktualisiere LAMPP (Apache, MySQL und evtl. weitere)"
			echo "	reloadapache Aktualisiere nur den Apache"
			echo "	reloadmysql  Aktualisiere nur den MySQL"
			echo "	reloadftp    Aktualisiere nur den ProFTPD"
			echo ""
			echo "	restart      Stoppe und starte LAMPP"
			echo "	security     berprfe LAMPPs Sicherheit"
			echo "	status       berprfe LAMPP"
			echo ""
		else
			echo "Usage: $0 <action>"
			echo ""
			echo "	start        Start LAMPP (Apache, MySQL and eventually others)"
			echo "	startapache  Start only Apache"
			echo "	startssl     Start only SSL support"
			echo "	startmysql   Start only MySQL"
			echo "	startftp     Start only ProFTPD"
			$iswebmin && echo "	startwebmin  Start only Webmin"
			echo ""
			echo "	stop         Stop LAMPP (Apache, MySQL and eventually others)"
			echo "	stopapache   Stop only Apache"
			echo "	stopssl      Stop only SSL support"
			echo "	stopmysql    Stop only MySQL"
			echo "	stopftp      Stop only ProFTPD"
			$iswebmin && echo "	stopwebmin   Stop only Webmin"
			echo ""
			echo "	reload       Reload LAMPP (Apache, MySQL and eventually others)"
			echo "	reloadapache Reload only Apache"
			echo "	reloadmysql  Reload only MySQL"
			echo "	reloadftp    Reload only ProFTPD"
			echo ""
			echo "	restart      Stop and start LAMPP"
			echo "	security     Check LAMPP's security"
			echo "	status       Check LAMPP's status"
			echo ""
		fi

		;;
esac
