--- scripts/pgallery/src/pgallery.sh 2012/08/28 08:15:13 1.2 +++ scripts/pgallery/src/pgallery.sh 2012/08/31 15:58:03 1.21 @@ -1,12 +1,13 @@ -#!/bin/sh +#!/home/as/sh # # THE BEER-WARE LICENSE 2012 # 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.1 2012/08/27 14:23:59 as Exp $ +# $Paefchen: scripts/pgallery/src/pgallery.sh,v 1.20 2012/08/31 15:52:10 as Exp $ # +. $(dirname $0)/libpgallery ARG_TITLE="PGallery" ARG_FILES= @@ -19,13 +20,17 @@ ARG_INDEX="index.html" ARG_DIR="." ARG_UPDATE=1 ARG_COPYRIGHT="© $(date +%Y)" +ARG_INFOFILE="" +ARG_MAILTO="" +ARG_MAILFROM="PGallery <${USER}@${HOST}>" +ARG_URL= +ARG_EXIF="0x920a 0x829d 0x829a ISO 0x8827 / 0x010f - 0x0110" +ARG_INDEXIMG= +ARG_STARTTS= +ARG_ENDTS= +ARG_LINK= -BIN_CONVERT="/usr/local/bin/convert" -BIN_EXIFAUTOTRAN="/usr/local/bin/exifautotran" -BIN_IMGINFO="/usr/local/bin/imginfo" - MKAKEFILE=".htmakefile" -VERSION="pgallery-1" # # usage() @@ -34,340 +39,62 @@ usage() { cat <] | [-s ] ) [-d ] [-n ] - [-t ] [-u ] - [-b ] [-c ] - [-i ] [-U] | -h + [-t ] [-u ] [-H ] + [-b ] [-c ] [-l link] [-e ] + [-i ] [-C ] [-F ] [-T ] + [-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) + -L link optional home link + -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 LINK + -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 - -C copyright copyright (html) string (default: $ARG_COPYRIGHT) - -v print version USAGE exit } -# -# get_my_path() -# -get_my_path() -{ - current=$(pwd) - cd $(dirname $0) - echo $(pwd)/$(basename $0) - cd "$current" -} - -# -# html_header() -# -html_header() -{ - cat < - - - $ARG_TITLE - - - - -HTML -} - -# -# html_footer() -# -html_footer() -{ - cat < -HTML -} - -# -# html_tn() -# -html_tn() -{ - tn=$1 - link=$2 - - info=$($BIN_IMGINFO -f $tn) - width=$(echo $info | awk '{print $3}') - height=$(echo $info | awk '{print $4}') - top=$(($(($ARG_TNSIZE - $height)) / 2)) - - cat < - - $tn - - -HTML -} - -# -# html_img() -# -html_img() -{ - img=$1 - link=$2 - linkb=$3 - linkn=$4 - - height=$($BIN_IMGINFO -f $img | awk '{print $4}') - - if [ "$linkb" != "" ] +# first find image dir +is_dir=0 +for arg in $* +do + if [ $is_dir -eq 1 ] then - hlinkb="zurück" - olinkb="" + ARG_DIR="$arg" + break fi + test $arg = -d && is_dir=1 +done - if [ "$linkn" != "" ] - then - hlinkn="weiter" - olinkn="" - fi +# load save args +if [ -f "$ARG_DIR/$ARGFILE" ] +then + echo "load save settings" + . "$ARG_DIR/$ARGFILE" +fi - cat < - -
- -
-
- $ARG_TITLE $ARG_COPYRIGHT -
-
- $olinkb - $olinkn -
- -HTML -} - -# -# err -# -err() -{ - echo "$1" >&2 - exit 1 -} - - # parse args while [ "$1" != "" ] do @@ -375,60 +102,93 @@ do shift case $arg in -h) - usage - ;; + usage;; -n) ARG_TITLE=$1 - shift - ;; + shift;; -t) ARG_TNSIZE=$1 - shift - ;; + shift;; -u) ARG_TNQALITY=$1 - shift - ;; + shift;; -b) ARG_BIGSIZE=$1 - shift - ;; + shift;; -c) ARG_BIGQALITY=$1 - shift - ;; + shift;; -f) ARG_FILES=$1 - shift - ;; + shift;; + -l) + ARG_URL=$1 + shift;; -s) ARG_FILETYPES=$1 - shift - ;; + shift;; -d) ARG_DIR=$1 - shift - ;; + shift;; + -e) + ARG_EXIF=$1 + shift;; -C) ARG_COPYRIGHT=$1 - shift - ;; + shift;; + -F) + ARG_MAILFROM=$1 + shift;; + -L) + ARG_LINK=$1 + shift;; + -I) + ARG_INFOFILE=$1 + shift;; + -T) + ARG_MAILTO=$1 + shift;; -U) - ARG_UPDATE=0 - ;; + ARG_UPDATE=0;; -v) echo $VERSION - exit - ;; + 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 LINK + 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 @@ -438,22 +198,90 @@ then match="$match *.$(echo $type | tr '[:lower:]' '[:upper:]')" match="$match *.$(echo $type | tr '[:upper:]' '[:lower:]')" done - ARG_FILES=$(cd $ARG_DIR && ls $match 2>/dev/null) + FLIST=$(ls $match 2>/dev/null) +else + FLIST="$ARG_FILES" fi # Filter big and thumbnails echo -n "find images" -FLIST= -for file in $ARG_FILES +FLISTF= +for file in $FLIST do test $(echo $file | grep -E '\.(tn|big)\.jpg$') && continue - FLIST="$FLIST $file" + FLISTF="$FLISTF $file" echo -n " $file" done -echo +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 start and end ts +ARG_STARTTS=$(get_exif_ts $(echo $FLIST | awk '{print $1}')) +ARG_ENDTS=$(get_exif_ts $(echo $FLIST | awk '{print $NF}')) + +# 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 <> $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 @@ -473,7 +301,7 @@ do fi ( - cat <> $MKAKEFILE -make -f $MKAKEFILE +#make -f $MKAKEFILE rm $MKAKEFILE +# create html index +echo create html index +( + html_header + + startdate= + test $ARG_STARTTS -ne -1 && startdate=$(date -r $ARG_STARTTS "+%d.%m.%Y") + enddate= + test $ARG_ENDTS -ne -1 && enddate=$(date -r $ARG_ENDTS "+%d.%m.%Y") + date= + test "$startdate" != "" && date="$startdate" + test "$enddate" != "$startdate" && date="$date - $enddate" + link= + test "$ARG_LINK" != "" && link="overview" + html_title "$ARG_TITLE" "$link" "$date" + + for file in $FLIST + do + tn="${file}.tn.jpg" + imglink="${file}.html" + html_tn $tn $imglink + done + echo "
" + echo "
$ARG_TITLE $ARG_COPYRIGHT
" + html_footer +) > $ARG_INDEX + # create single html files -index="" i=0 +all=$(echo $FLIST | wc -w) echo -n "create single html files " for file in $FLIST do @@ -496,7 +351,12 @@ do i=$(($i + 1)) j=$(($i + 1)) - tn="${file}.tn.jpg" + # 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" @@ -508,21 +368,61 @@ do test "$linkn" != "" && linkn="$linkn.html" echo -n . - index="$index $(html_tn $tn $imglink)" ( html_header - html_img $big $ARG_INDEX "$linkb" "$linkn" + html_img $file $i $all $big $ARG_INDEX "$linkb" "$linkn" html_footer ) > $imglink done echo -# create html index -echo create html index -( - html_header - echo $index - echo "
" - echo "
$ARG_TITLE $ARG_COPYRIGHT
" - html_footer -) > $ARG_INDEX +# .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