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

#Change to your settings
album_bin=/usr/bin/album
homedir_public_html=$HOME/public_html
public_html_pic_folder=Bilder

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

if [ "$1" = "-h" ];then
echo
echo " album-user (all Sub-Folders)  or  (mostly) album-user FolderName  (add 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 $HOME/.album.conf! Stop the execution.";echo !
exit 0
fi

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

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

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

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

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

#..or do all SubDir
if [ -z "$1" ];then
echo;echo "..doing all folders in $homedir_public_html/$public_html_pic_folder";echo
	#Search the Capital Folder inside public_html_pic_folder and create there the albums
	for full_sub_dir in "$homedir_public_html/$public_html_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
