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

#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!"
exit 0
fi


if [ "$1" = "-h" ];then
echo
echo " album-http (all Sub-Folders)  or  (mostly) album-http FolderName  (add one Folder or updadting deleted ones)"
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."

#checking wars album-http-main run and made a index.html in main folder to remove
if [ -e "$html_root_folder/$pic_folder/index.html" ];then rm "$html_root_folder/$pic_folder/index.html" > /dev/null 2>&1 ;fi

#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 "$html_root_folder/$pic_folder/$1"
fi

#..or do All
if [ -z "$1" ];then
echo;echo "..doing all folders in $html_root_folder/$pic_folder";echo
	#Search the Capital Folder inside public_html_pic_folder and create there the albums
	for full_sub_dir in "$html_root_folder/$pic_folder"/* ;do
	if [ -d "$full_sub_dir" ];then
	rm "$full_sub_dir/index.html" > /dev/null 2>&1
	$album_bin "$full_sub_dir"
	fi
	done
fi

exit 0
