#!/bin/sh
#
# Tank - Admin Tank, backup, update and give stats.
#
# (C) 2012-2014 SliTaz - GNU General Public License.
# Author: Christophe Lincoln <pankso@slitaz.org>
#
. /lib/libtaz.sh

REPOS="/home/slitaz/repos"
WWW="/home/slitaz/www"
VHOST="$WWW/tank"
WEBSITE="$WWW/website"
BACKUPS="/home/backups"
LOGFILE="/var/log/tank.log"

usage() {
	cat << EOT

Usage: $(basename $0) [command]
Commands:
  backup|-b    Backup files and MySQL DB
  adduser      Add user on Tank and create people files
  up-stats     Update Awstats statistics (run by cron)
  online       Show online users

  up-tank      Update http://tank.slitaz.org/
  up-people    Update http://people.slitaz.org/
  up-pro       Update http://pro.slitaz.org/
  up-boot      Update http://boot.slitaz.org/
  up-cook      Update http://cook.slitaz.org/
  up-roadmap   Update http://roadmap.slitaz.org/
  up-bugs      Update http://bugs.slitaz.org/
  up-irc       Update http://irc.slitaz.org/
  up-arm       Update http://arm.slitaz.org/
  up-piclass   Update http://piclass.org/

EOT
}

case "$1" in
	backup|-b)
		# Backup config files and SQL db.
		echo "TODO" ;;
	up-tank)
		# Update Tank web interface
		echo -e "\nUpdating: tank.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		rm -rf $VHOST/*.* $VHOST/images
		cp -a tank/web/* $VHOST
		echo "" ;;
	up-people)
		# Update People web interface
		echo -e "\nUpdating: people.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		rm -rf $WWW/people/*
		cp -a people/* $WWW/people
		echo "" ;;
	up-pro)
		# Update Pro website
		echo -e "\nUpdating: pro.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		rm -rf $WWW/pro/web/*
		cp -a pro/* $WWW/pro/web
		echo "" ;;
	up-boot)
		# Update Web Boot interface
		echo -e "\nUpdating: boot.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		rm -rf $WWW/boot/*
		cp -a boot/* $WWW/boot
		echo "" ;;
	up-cook)
		# Update Web Boot interface
		echo -e "\nUpdating: cook.slitaz.org..."
		cd $REPOS/cookutils
		[ "$2" == "--nohg" ] || hg pull -u
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		cp -a cook/* $WWW/cook
		# We use symlinks for cooker's
		cd $WWW/cook && rm -f style.css
		ln -s $REPOS/cookutils/web/style.css .
		cd $WWW/cook/cross && rm -f style.css
		ln -s $REPOS/cookutils/web/style.css .
		for web in stable undigest backports cross/arm cross/x86_64
		do
			echo "Linking: $web CSS/CGI files"
			cd $WWW/cook/$web
			for file in style.css cooker.cgi cookiso.cgi
			do
				rm -f $file
				ln -s $REPOS/cookutils/web/$file .
			done
			# header.html
			echo "Linking: $web/header.html"
			rm -f header.html
			ln -s ../header.html .
		done

		# No ISO's for undigest and ARM.
		rm -f \
			$WWW/cook/undigest/cookiso.cgi \
			$WWW/cook/cross/arm/cookiso.cgi
		echo "" ;;
	up-roadmap)
		# Update Roadmap Web interface
		echo -e "\nUpdating: roadmap.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		cp -a roadmap/* $WWW/roadmap
		echo "" ;;
	up-bugs)
		# Update Bugs Web interface
		bugs="$WWW/bugs"
		echo -e "\nUpdating: bugs.slitaz.org..."
		cd $REPOS/tazbug
		[ "$2" == "--nohg" ] || hg pull -u
		cp -a web/* ${bugs}
		cp README ${bugs}
		chown -R www.www ${bugs}
		echo "" ;;
	up-irc)
		# Update IRC interface
		echo -e "\nUpdating: irc.slitaz.org..."
		cd $REPOS/slitaz-forge
		[ "$2" == "--nohg" ] || hg pull -u
		rm -rf $WWW/irc/images $WWW/irc/lib
		cp -a irc/* $WWW/irc
		echo "" ;;
	up-arm)
		# Update ARM Web interface
		echo -e "\nUpdating: arm.slitaz.org..."
		cd $REPOS/slitaz-forge && hg pull -u
		cp -a arm/* $WWW/arm
		echo "Updating: slitaz-arm Hg repo..."
		cd $REPOS/slitaz-arm && hg pull -u
		echo "" ;;
	up-piclass)
		# Update piclass.org
		echo -e "\nUpdating: piclass.org..."
		cd /home/piclass/website
		git pull && echo "" ;;
	up-stats)
		echo -e "\nUpdating all awstats databases..." | tee -a $LOGFILE
		date >> $LOGFILE
		for vh in pro boot cook people tank
		do
			/var/www/cgi-bin/awstats.pl \
				-config=$vh.slitaz.org -update 2>&1 | tee -a $LOGFILE
		done && echo "" ;;
	adduser)
		echo ""
		if [ -n "$2" ]; then
			user=$2
		else
			echo -n "User name: " && read user
		fi
		if [ -d /home/$user ]; then
			echo -e "User $user already exists...\n" && exit 1
		fi
		if [ -n "$3" ]; then
			gecos="$3"
		else
			echo -n "Real name: " && read name
		fi
		if [ -n "$4" ]; then
			pass=$4
		else
			echo -n "Password: " && read pass
		fi
		echo "Adding user: $user"
		adduser -D -g "$gecos" $user -G users
		echo $user:$pass | chpasswd --md5
		addgroup $user slitaz
		# HG access
		#echo "$user:$pass" >> /etc/lighttpd/plain.passwd
		# Public dir at http://people.slitaz.org/~$user/
		sed -i s/'%user%'/"$user"/ /home/$user/Public/index.html
		sed -i s/'%name%'/"$gecos"/ /home/$user/Public/profile.php
		# Empty Shell profile
		cat > /home/$user/.profile << EOF
# ~/.profile: Executed by Bourne-compatible login SHells.
#
EOF
		#chown -R $user.$user /home/$user
		echo -e "Done\n" ;;
	online)
		echo -n "Bugs online users: "
		colorize 34 "$(ls /tmp/bugs/sessions | wc -l)" ;;
	*)
		usage ;;
esac
exit 0
