#!/bin/sh -e
#
#    pictor-thumbs: a script for generating all thumbnails
#    Copyright (C) 2015 Dustin Kirkland <dustin.kirkland@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.

#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

find_binary() {
	if command -v php; then
		BINARY=php
	elif command -v php5; then
		BINARY=php5
	elif command -v php.7.0; then
		BINARY=php7.0
	fi
}

find_binary
if [ "$(id -un)" != "www-data" ]; then
	exec sudo -u www-data "$0" "$@"
else
	cd /usr/share/pictor
	find /var/cache/pictor -type l -delete
	$BINARY /usr/share/pictor/index.php batch
	find /var/cache/pictor/ -empty -type f -delete
fi
