#!/bin/sh
#
# Pangolin admin utility
#
# Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License
#
# Authors : Christophe Lincoln <pankso@slitaz.org>
#

REPOS="/home/slitaz/repos"
WWW="/home/vhost"
WEBSITE="$WWW/www.slitaz.org/website"
PYTHON_LIB="/usr/lib/python2.7"
HGUSERS="/home/slitaz/auth/hgusers"

usage() {
	cat << EOT

Usage: $(basename $0) [command]
Commands:
  up-www       Update website www.slitaz.org
  up-forum     Update forum.slitaz.org theme
  up-doc       Update doc.slitaz.org theme and configs
  up-hg        Update Hg web interface hg.slitaz.org
  adduser      Add a user to Hg.
  stats        Display some server stats.
  backup       Backup MySQL DB and files.

EOT
}

# While libtaz.sh is not installed on Pangolin
#
newline() {
	echo ""
}

boldify() {
	echo -e "\\033[1m$@\\033[0m"
}

separator() {
	echo "================================================================================"
}

#
# handle commands
#

case "$1" in
	up-www)
		# Update website from repo.
		newline
		boldify "Updating: www.slitaz.org..."
		cd ${WEBSITE} && hg pull -u 
		newline ;;
	up-forum)
		# Update forum.slitaz.org theme
		newline
		boldify "Updating: forum.slitaz.org..."
		cd ${REPOS}/slitaz-forge && hg up
		cp -a forum/my-templates ${WWW}/forum.slitaz.org/public_html 
		newline ;;
	up-doc)
		# Update doc.slitaz.org theme
		newline
		boldify "Updating: doc.slitaz.org..."
		cd ${REPOS}/slitaz-forge && hg up
		cp -a doc/* ${WWW}/doc.slitaz.org/public_html
		chown www.www ${WWW}/doc.slitaz.org/public_html/conf
		chown www.www ${WWW}/doc.slitaz.org/public_html/conf/local.php
		newline ;;
	up-hg)
		# Update hg.slitaz.org template.
		newline
		boldify "Updating Mercurial template..."
		cd $REPOS/slitaz-dev-tools && hg update 
		cp -a slitaz-mercurial-style/* $PYTHON_LIB/site-packages/mercurial
		chown -R root.root $PYTHON_LIB/site-packages/mercurial/templates
		newline ;;
	up-pangolin)
		# Update pangolin.slitaz.org
		newline
		boldify "Updating: pangolin.slitaz.org..."
		cd ${REPOS}/slitaz-forge && hg up
		cp -a pangolin/web/* $WWW/pangolin.slitaz.org/public_html
		newline ;;
	adduser)
		[ ! "$2" ] && echo "Missing user name arg" && exit 0
		[ ! "$3" ] && echo "Missing password arg" && exit 0
		htpasswd -b ${HGUSERS} $2 $3 ;;
	stats)
		# Echo some stats.
		newline
		boldify "Disk usage"
		separator
		df -h | grep ^/dev
		boldify "Memory usage"
		separator
		free -m | grep ^Mem
		boldify "Connected users"
		separator
		who 
		newline ;;
	backup)
		echo "TODO: backup MySQL, SCN files, etc" ;;
	*)
		usage ;;
esac
exit 0
