#!/bin/sh
#
# Tiny Tazpanel cmdline interface
#
# Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License
#

VERSION=638
CONFIG="/etc/slitaz/tazpanel.conf"
[ -f "$CONFIG" ] && . $CONFIG
[ -f "data/tazpanel.conf" ] && . ./data/tazpanel.conf
[ ! "$PANEL" ] && echo "No config file found: $CONFIG" && exit 1
. /lib/libtaz.sh
TEXTDOMAIN='tazpanel'
export TEXTDOMAIN
NAME=tazpanel
DESC="SliTaz administration and configuration panel."
DAEMON=/usr/sbin/httpd
PIDFILE=/run/$NAME.pid
case "$1" in
-h|*help|*usage)
cat <<EOT
Usage: tazpanel [command]
Commands:
start    Start tazpanel daemon
stop     Stop tazpanel daemon
restart  Restart tazpanel daemon
<app>    Open TazPanel page in the browser, where <app> is one of:
boot, hardware, help, index, installer, live, network, settings, pkgs
If <app> is empty, index page will open in the browser.
You can also open sub-pages, so "pkgs#list" will open packages list.
cc       Clean cache: remove interface's header from cache
EOT
;;
start)
if [ -f $PIDFILE ]; then
_ 'TazPanel is already running.'
exit 1
fi
_n 'Starting TazPanel web server on port %d...' $HTTPD_PORT
$DAEMON -p $HTTPD_PORT -u root -c $HTTPD_CONF \
-r "$(_ 'TazPanel Authentication - Default: root:root')" &
ps 2>/dev/null | grep "$DAEMON -p $HTTPD_PORT " | grep -v grep | \
awk '{ print $1 }' > $PIDFILE
[ ! -f /var/lib/tazpkg/installed.info ] && tazpkg -l>/dev/null&
status;;
stop)
if [ ! -f $PIDFILE ]; then
_ 'TazPanel is not running.'
exit 1
fi
_n 'Stopping TazPanel web server...'
kill $(pgrep -f TazPanel)
rm -f $PIDFILE
status;;
restart)
tazpanel stop; sleep 1; tazpanel start;;
cc)
find /var/cache/tazpanel -name 'header.*' -delete;;
*)
. /etc/slitaz/applications.conf
USER_CONFIG="$HOME/.config/slitaz/applications.conf"
[ -f "$USER_CONFIG" ] && . $USER_CONFIG
applet=${1:-index}
case $1 in
*#*) url="http://127.0.0.1:$HTTPD_PORT/${applet%%#*}.cgi?${applet#*#}&guiuser=$(id -un)";;
*)   url="http://127.0.0.1:$HTTPD_PORT/$applet.cgi?guiuser=$(id -un)";;
esac
echo "$url"
case "$BROWSER" in
tazweb) tazweb --notoolbar "$url" &;;
midori) midori "--app=$url" &;;
*) $BROWSER "$url" &;;
esac;;
esac
exit 0
