#!/bin/sh
#
#Scriptname: album-http-main
#chmod 755 /usr/local/bin/album-http-main
#

#Change to your settings
album_bin=/usr/bin/album
html_root_folder=/var/www/set_to_yours
pic_folder=Bilder

# Script
#----------------------------------
#check user
if [ $USER != root ];then
echo
echo " Only root can run album-http-main!"
exit 0
fi

if [ "$1" = "-h" ];then
echo
echo " album-http-main (all Folders)  or   album-http-main FolderName  (to add one Folder or update a Folder)"
echo " or   album-http-main clean (clean all folders)"
echo
exit 0
fi

#checking for /etc/album.conf
if [ -e /etc/album.conf ];then 
echo !; echo "! The file /etc/album.conf must be removed, it hinders /root/.album.conf! Stop the execution.";echo !
exit 0
fi

echo;echo "=> `basename $0` -h shows some options."

#Check for needed folder and config file.
#If root damage his config or album-data, he shout just delete them, i bring fresh one back.
if [ ! -d "$html_root_folder" ];then
echo
echo " Your apache document root not exist!"
echo " Setting is: html_root_folder=$html_root_folder"
echo " Please edit the head of the script $0. Exiting!"
exit 0
fi

if [ ! -d "$html_root_folder/$pic_folder" ];then mkdir -p $html_root_folder/$pic_folder;fi
if [ ! -d "$html_root_folder/Themes" ];then cp -a /usr/share/album-data $html_root_folder; chown -R www-data:www-data $html_root_folder/album-data;fi
if [ ! -e "/root/.album.conf" ];then cp /etc/skel/.album.conf /root/.album.conf; chown root.root /root/.album.conf;fi
if [ -z "`grep theme_path /root/.album.conf`" ];then
	echo "# This lines below are added by $0, don't edit!" >> /root/.album.conf
	echo "data_path       $html_root_folder/album-data" >> /root/.album.conf
	echo "theme_path	$html_root_folder/album-data/Themes" >> /root/.album.conf
fi

#If one Folder selcetet by Input Comandline, or all
if [ -d "$html_root_folder/$pic_folder/$1" -a -n "$1" ];then
echo;echo "..doing add/update $html_root_folder/$pic_folder/$1";echo
	rm "$html_root_folder/$pic_folder/$1/index.html" > /dev/null 2>&1
        $album_bin -add="$html_root_folder/$pic_folder/$1"
fi

if [ "$1" = "clean" ];then
echo;echo "..cleaning all folders in $html_root_folder/$pic_folder";echo
	$album_bin -clean "$html_root_folder/$pic_folder"
fi


#..or do All
if [ -z "$1" ];then
echo;echo "..doing all folders in $html_root_folder/$pic_folder";echo
	$album_bin "$html_root_folder/$pic_folder"
fi

exit 0
