File:  [Paefchen] / scripts / pgallery / src / pgallery.sh
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Fri Aug 31 12:33:07 2012 UTC (12 years, 2 months ago) by as
Branches: MAIN
CVS tags: HEAD
Mit pgalleryindex lässt sich ein index von allen Gallerien erstellen.

#!/bin/sh
#
# THE BEER-WARE LICENSE 2012
# <as@paefchen.net> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Aron Schlesinger
#
# $Paefchen: scripts/pgallery/src/pgallery.sh,v 1.18 2012/08/31 12:33:07 as Exp $
#
. $(dirname $0)/libpgallery

ARG_TITLE="PGallery"
ARG_FILES=
ARG_FILETYPES="jpg jpeg png gif tif tiff"
ARG_TNSIZE="180"
ARG_TNQALITY="70"
ARG_BIGSIZE="950"
ARG_BIGQALITY="90"
ARG_INDEX="index.html"
ARG_DIR="."
ARG_UPDATE=1
ARG_COPYRIGHT="&copy; $(date +%Y)"
ARG_INFOFILE=""
ARG_MAILTO=""
ARG_MAILFROM="PGallery <${USER}@${HOST}>"
ARG_URL=
ARG_EXIF="0x920a 0x829d 0x829a ISO 0x8827 / 0x010f - 0x0110"
ARG_INDEXIMG=

MKAKEFILE=".htmakefile"

#
# usage()
#
usage()
{
	cat <<USAGE
usage $0: ( [-f <files>] | [-s <suffix>] ) [-d <dir>] [-n <name>]
                [-t <thumbnail_size>] [-u <thumbnail_quality>]
                [-b <big_size>] [-c <big_quality>] [-l link] [-e <exif>]
                [-i <index>] [-C <copyright>] [-F <mailfrom>] [-T <mailto>]
                [-U] | -h | -v
Options:
  -b size       size from big images in pixel (default: $ARG_BIGSIZE)
  -c quality    quality from big images 0-100% (default: $ARG_BIGQALITY)
  -d dir        gallery dir (default: $ARG_DIR)
  -e exif       list of tags in hex. (default: $ARG_EXIF)
                see: http://www.exiv2.org/tags.html
  -f files      gallery images
  -h            this page
  -i name       index file name (default: $ARG_INDEX)
  -l url        gallery link
  -m image      image for pgalleryindex. default the first image
  -n name       page title (default: $ARG_TITLE)
  -s suffix     list of suffix. if no \`\`-f'' defined, used to find images
                in \`\`-d'' directoy. case insensitive
                (default: $ARG_FILETYPES)
  -t size       size from thumbnail images in pixel (default: $ARG_TNSIZE)
  -u quality    quality from thumbnail images 0-100% (default: $ARG_BIGQALITY)
  -v            print version
  -C copyright  copyright (html) string (default: $ARG_COPYRIGHT)
  -F from       mail sender (default: $ARG_MAILFROM)
  -I file       info text file for pgallery. info file paras prefered.
                the syntax of the file is MAKE(1)
                supported variable: TITLE COPYRIGHT INDEX FILETYPES TNSIZE URL
                                    TNQALITY BIGSIZE BIGQALITY MAILTO MAILFROM
                                    EXIF INDEXIMG
  -T mailto     send update infos to this address. if not set, no mail delivery
  -U            no update, new calculation of all images.
                normally only new and modified images will calculated
USAGE
	exit
}

# first find image dir
is_dir=0
for arg in $*
do
	if [ $is_dir -eq 1 ]
	then
		ARG_DIR="$arg"
		break
	fi
	test $arg = -d && is_dir=1
done

echo $ARG_DIR

# load save args
if [ -f "$ARG_DIR/$ARGFILE" ]
then
	echo "load save settings"
	. "$ARG_DIR/$ARGFILE"
fi

# parse args
while [ "$1" != "" ]
do
	arg=$1
	shift
	case $arg in
		-h)
			usage;;
		-n)
			ARG_TITLE=$1
			shift;;
		-t)
			ARG_TNSIZE=$1
			shift;;
		-u)
			ARG_TNQALITY=$1
			shift;;
		-b)
			ARG_BIGSIZE=$1
			shift;;
		-c)
			ARG_BIGQALITY=$1
			shift;;
		-f)
			ARG_FILES=$1
			shift;;
		-l)
			ARG_URL=$1
			shift;;
		-s)
			ARG_FILETYPES=$1
			shift;;
		-d)
			ARG_DIR=$1
			shift;;
		-e)
			ARG_EXIF=$1
			shift;;
		-C)
			ARG_COPYRIGHT=$1
			shift;;
		-F)
			ARG_MAILFROM=$1
			shift;;
		-I)
			ARG_INFOFILE=$1
			shift;;
		-T)
			ARG_MAILTO=$1
			shift;;
		-U)
			ARG_UPDATE=0;;
		-v)
			echo $VERSION
			exit;;
		*)
			echo "unknown argument $arg"
			exit
	esac
done

# go to gallery directory
MY=$(get_my_path)
cd "$ARG_DIR" || exit 1

# parese info file
if [ "$ARG_INFOFILE" != "" ]
then
	test -f $ARG_INFOFILE || err "info file not found \`\`$ARG_INFOFILE''"
	for n in TITLE COPYRIGHT INDEX FILETYPES TNSIZE TNQALITY BIGSIZE \
		BIGQALITY MAILTO MAILFROM URL EXIF INDEXIMG
	do
		v=$(make -f $ARG_INFOFILE -V$n)
		test "$v" = "" && continue
		eval "ARG_$n='$v'"
	done	
fi

#paras from iso to utf-8
for n in $(set | awk -F= '{print $1}')
do
	echo "$n" | grep -q '^ARG_' || continue
	eval "$n=\"\$(echo \"\$$n\" | iconv -f ISO-8859-1 -t UTF-8)\""
done

# fix url
if [ "$ARG_URL" != "" ]
then
	echo "$ARG_URL" | grep -q '/$' || ARG_URL="${ARG_URL}/"
fi

# if no images defined, find files
if [ "$ARG_FILES" = "" -a "$ARG_FILETYPES" != "" ]
then
	match=
	for type in $ARG_FILETYPES
	do
		match="$match *.$(echo $type | tr '[:lower:]' '[:upper:]')"
		match="$match *.$(echo $type | tr '[:upper:]' '[:lower:]')"
	done
	FLIST=$(ls $match 2>/dev/null)
else
	FLIST="$ARG_FILES"
fi

# Filter big and thumbnails
echo -n "find images"
FLISTF=
for file in $FLIST
do
	test $(echo $file | grep -E '\.(tn|big)\.jpg$') && continue
	FLISTF="$FLISTF $file"
	echo -n " $file"
done
echo
FLIST="$FLISTF"

test $(echo $FLIST | wc -w) -eq 0 && err "error: no image found in \`\`$ARG_DIR''"

# use exif createtime to order images
FLIST=$(
	(
		for file in $FLIST
		do
			echo "$(get_exif_ts $file) $file"
		done
	) | sort -n | awk '{print $2}'
)

# if no master image, we use the first one
test "$ARG_INDEXIMG" = "" && ARG_INDEXIMG=$(echo $FLIST | awk '{print $1}')

# find new and to update images
FLIST_NEW=
FLIST_UPDATE=
echo "all: tns" > $MKAKEFILE
targets=""
for file in $FLIST
do
	tn="${file}.tn.jpg"
	if [ ! -f $tn ]
	then
		FLIST_NEW="$FLIST_NEW $file"
		continue
	fi

	targets="$targets $tn"
	(
		cat <<MAKE
$tn: $file
	@echo ${file}
MAKE
	) >> $MKAKEFILE
done
echo "tns: $targets" >> $MKAKEFILE
FLIST_UPDATE=$(make -f $MKAKEFILE)
rm $MKAKEFILE

# save args
test -f $ARGFILE && rm $ARGFILE
echo "save settings"
set | while read n
do
	echo "$n" | grep -q '^ARG_' || continue
	echo "$n" >> $ARGFILE
done

# update and no new and update images found
if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" -a "$FLIST_UPDATE" = "" ]
then
	echo nothing to do
	exit 1
fi

if [ $ARG_UPDATE -eq 1 ]
then
	echo new images: $FLIST_NEW
	echo update images: $FLIST_UPDATE
fi

# Create make file to rotate und convert all big and thumbnails.
# Is needed to define the new real image sizes.
echo "all: tns" > $MKAKEFILE
targets=""
for file in $FLIST
do
	tn="${file}.tn.jpg"
	big="${file}.big.jpg"
	targets="$targets $tn"

	target=
	if [ $ARG_UPDATE -eq 1 ]
	then
		target="$tn: $ME $file"
	else
		target="$tn::"
	fi

	(
		cat <<MAKE
$target
	$BIN_EXIFAUTOTRAN $file
	$BIN_CONVERT -verbose -thumbnail ${ARG_TNSIZE}x${ARG_TNSIZE} -quality $ARG_TNQALITY $file $tn
	$BIN_CONVERT -verbose -thumbnail ${ARG_BIGSIZE}x${ARG_BIGSIZE} -quality $ARG_BIGQALITY $file $big
MAKE
	) >> $MKAKEFILE
done
echo "tns: $targets" >> $MKAKEFILE

make -f $MKAKEFILE
rm $MKAKEFILE

# create html index
index=""
for file in $FLIST
do
	tn="${file}.tn.jpg"
	imglink="${file}.html"
	index="$index $(html_tn $tn $imglink)"
done
echo create html index
(
	html_header
	echo $index
	echo "<div class='clear'></div>"
	echo "<div class='c'>$ARG_TITLE $ARG_COPYRIGHT</div>"
	html_footer
) > $ARG_INDEX

# create single html files
i=0
all=$(echo $FLIST | wc -w)
echo -n "create single html files "
for file in $FLIST
do
	h=$i
	i=$(($i + 1))
	j=$(($i + 1))

	# is update mod and no new images founded
	if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" ]
	then
		is_in_list "$FLIST_UPDATE" "$file" || continue
	fi

	big="${file}.big.jpg"
	imglink="${file}.html"

	if [ $i -ne 1 ]
	then
		linkb=$(echo $FLIST | awk "{print \$$h}").html
	fi
	linkn=$(echo $FLIST | awk "{print \$$j}")
	test "$linkn" != "" && linkn="$linkn.html"

	echo -n .
	(
		html_header
		html_img $file $i $all $big $ARG_INDEX "$linkb" "$linkn"
		html_footer
	) > $imglink
done
echo

# .htaccess
if [ -f .htaccess ]
then
	grep -q 'AddCharset UTF-8 .html' < .htaccess || echo "AddCharset UTF-8 .html" >> .htaccess
else
	echo "AddCharset UTF-8 .html" > .htaccess
fi

test "$ARG_MAILTO" = "" && exit 0

# send info mail
if [ "$FLIST_NEW" != "" -o "$FLIST_UPDATE" != "" ]
then
	echo "send mail to $ARG_MAILTO"
	(
		echo "Hallo,"
		echo
		echo -n "the gallery $ARG_TITLE has been updated"
		if [ "$ARG_URL" != "" ]
		then
			echo ":"
			echo $ARG_URL
		else
			echo "."
		fi

		suffix=
		test "$ARG_URL" != "" && suffix=.html

		if [ "$FLIST_NEW" != "" ]
		then
			echo
			echo "add pictures:"
			for file in $FLIST_NEW
			do
				echo "- ${ARG_URL}${file}${suffix}"
			done
		fi

		if [ "$FLIST_UPDATE" != "" ]
		then
			echo
			echo "update pictures:"
			for file in $FLIST_UPDATE
			do
				echo "- ${ARG_URL}${file}${suffix}"
			done
		fi
	) | mysendmail "PGallery Update: $ARG_TITLE"
fi