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

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

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

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

#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, or all
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 -add="$homedir_public_html/$public_html_pic_folder/$1"
fi

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

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

exit 0
