Annotation of scripts/pgallery/src/pgallery.sh, revision 1.25

1.25    ! as          1: #!/bin/sh
1.1       as          2: #
                      3: # THE BEER-WARE LICENSE 2012
                      4: # <as@paefchen.net> wrote this file. As long as you retain this notice you
                      5: # can do whatever you want with this stuff. If we meet some day, and you think
                      6: # this stuff is worth it, you can buy me a beer in return Aron Schlesinger
                      7: #
1.25    ! as          8: # $Paefchen: scripts/pgallery/src/pgallery.sh,v 1.24 2012/09/11 16:32:41 as Exp $
1.1       as          9: #
1.25    ! as         10: 
        !            11: . /usr/local/libexec/libpgallery
1.1       as         12: 
                     13: ARG_TITLE="PGallery"
                     14: ARG_FILES=
                     15: ARG_FILETYPES="jpg jpeg png gif tif tiff"
                     16: ARG_TNSIZE="180"
                     17: ARG_TNQALITY="70"
                     18: ARG_BIGSIZE="950"
                     19: ARG_BIGQALITY="90"
                     20: ARG_INDEX="index.html"
                     21: ARG_DIR="."
                     22: ARG_UPDATE=1
                     23: ARG_COPYRIGHT="&copy; $(date +%Y)"
1.6       as         24: ARG_INFOFILE=""
                     25: ARG_MAILTO=""
                     26: ARG_MAILFROM="PGallery <${USER}@${HOST}>"
                     27: ARG_URL=
1.7       as         28: ARG_EXIF="0x920a 0x829d 0x829a ISO 0x8827 / 0x010f - 0x0110"
1.18      as         29: ARG_INDEXIMG=
1.19      as         30: ARG_STARTTS=
                     31: ARG_ENDTS=
1.20      as         32: ARG_LINK=
1.23      as         33: ARG_NAVI=0
1.1       as         34: 
                     35: MKAKEFILE=".htmakefile"
                     36: 
                     37: #
                     38: # usage()
                     39: #
                     40: usage()
                     41: {
                     42:        cat <<USAGE
                     43: usage $0: ( [-f <files>] | [-s <suffix>] ) [-d <dir>] [-n <name>]
1.20      as         44:                 [-t <thumbnail_size>] [-u <thumbnail_quality>] [-H <link>]
1.7       as         45:                 [-b <big_size>] [-c <big_quality>] [-l link] [-e <exif>]
1.6       as         46:                 [-i <index>] [-C <copyright>] [-F <mailfrom>] [-T <mailto>]
1.23      as         47:                 [-U] [-w] | -h | -v
1.1       as         48: Options:
                     49:   -b size       size from big images in pixel (default: $ARG_BIGSIZE)
                     50:   -c quality    quality from big images 0-100% (default: $ARG_BIGQALITY)
                     51:   -d dir        gallery dir (default: $ARG_DIR)
1.7       as         52:   -e exif       list of tags in hex. (default: $ARG_EXIF)
                     53:                 see: http://www.exiv2.org/tags.html
1.1       as         54:   -f files      gallery images
                     55:   -h            this page
                     56:   -i name       index file name (default: $ARG_INDEX)
1.6       as         57:   -l url        gallery link
1.18      as         58:   -m image      image for pgalleryindex. default the first image
1.1       as         59:   -n name       page title (default: $ARG_TITLE)
                     60:   -s suffix     list of suffix. if no \`\`-f'' defined, used to find images
                     61:                 in \`\`-d'' directoy. case insensitive
                     62:                 (default: $ARG_FILETYPES)
                     63:   -t size       size from thumbnail images in pixel (default: $ARG_TNSIZE)
                     64:   -u quality    quality from thumbnail images 0-100% (default: $ARG_BIGQALITY)
1.4       as         65:   -v            print version
1.23      as         66:   -w            enable navi width link in the overview
1.4       as         67:   -C copyright  copyright (html) string (default: $ARG_COPYRIGHT)
1.6       as         68:   -F from       mail sender (default: $ARG_MAILFROM)
1.20      as         69:   -L link       optional home link
1.5       as         70:   -I file       info text file for pgallery. info file paras prefered.
                     71:                 the syntax of the file is MAKE(1)
1.6       as         72:                 supported variable: TITLE COPYRIGHT INDEX FILETYPES TNSIZE URL
                     73:                                     TNQALITY BIGSIZE BIGQALITY MAILTO MAILFROM
1.23      as         74:                                     EXIF INDEXIMG LINK NAVI
1.6       as         75:   -T mailto     send update infos to this address. if not set, no mail delivery
1.1       as         76:   -U            no update, new calculation of all images.
                     77:                 normally only new and modified images will calculated
                     78: USAGE
                     79:        exit
                     80: }
                     81: 
1.18      as         82: # first find image dir
                     83: is_dir=0
                     84: for arg in $*
                     85: do
                     86:        if [ $is_dir -eq 1 ]
1.7       as         87:        then
1.18      as         88:                ARG_DIR="$arg"
                     89:                break
1.7       as         90:        fi
1.18      as         91:        test $arg = -d && is_dir=1
                     92: done
1.7       as         93: 
1.18      as         94: # load save args
                     95: if [ -f "$ARG_DIR/$ARGFILE" ]
                     96: then
                     97:        echo "load save settings"
                     98:        . "$ARG_DIR/$ARGFILE"
                     99: fi
1.11      as        100: 
1.1       as        101: # parse args
                    102: while [ "$1" != "" ]
                    103: do
                    104:        arg=$1
                    105:        shift
                    106:        case $arg in
                    107:                -h)
1.6       as        108:                        usage;;
1.1       as        109:                -n)
                    110:                        ARG_TITLE=$1
1.6       as        111:                        shift;;
1.1       as        112:                -t)
                    113:                        ARG_TNSIZE=$1
1.6       as        114:                        shift;;
1.1       as        115:                -u)
                    116:                        ARG_TNQALITY=$1
1.6       as        117:                        shift;;
1.1       as        118:                -b)
                    119:                        ARG_BIGSIZE=$1
1.6       as        120:                        shift;;
1.1       as        121:                -c)
                    122:                        ARG_BIGQALITY=$1
1.6       as        123:                        shift;;
1.1       as        124:                -f)
                    125:                        ARG_FILES=$1
1.6       as        126:                        shift;;
                    127:                -l)
                    128:                        ARG_URL=$1
                    129:                        shift;;
1.1       as        130:                -s)
                    131:                        ARG_FILETYPES=$1
1.6       as        132:                        shift;;
1.1       as        133:                -d)
                    134:                        ARG_DIR=$1
1.6       as        135:                        shift;;
1.7       as        136:                -e)
                    137:                        ARG_EXIF=$1
                    138:                        shift;;
1.1       as        139:                -C)
                    140:                        ARG_COPYRIGHT=$1
1.6       as        141:                        shift;;
                    142:                -F)
                    143:                        ARG_MAILFROM=$1
                    144:                        shift;;
1.20      as        145:                -L)
                    146:                        ARG_LINK=$1
                    147:                        shift;;
1.5       as        148:                -I)
                    149:                        ARG_INFOFILE=$1
1.6       as        150:                        shift;;
                    151:                -T)
                    152:                        ARG_MAILTO=$1
                    153:                        shift;;
1.1       as        154:                -U)
1.6       as        155:                        ARG_UPDATE=0;;
1.1       as        156:                -v)
                    157:                        echo $VERSION
1.6       as        158:                        exit;;
1.23      as        159:                -w)
                    160:                        ARG_NAVI=1;;
1.1       as        161:                *)
                    162:                        echo "unknown argument $arg"
                    163:                        exit
                    164:        esac
                    165: done
                    166: 
1.14      as        167: # go to gallery directory
                    168: MY=$(get_my_path)
                    169: cd "$ARG_DIR" || exit 1
                    170: 
1.5       as        171: # parese info file
                    172: if [ "$ARG_INFOFILE" != "" ]
                    173: then
                    174:        test -f $ARG_INFOFILE || err "info file not found \`\`$ARG_INFOFILE''"
1.6       as        175:        for n in TITLE COPYRIGHT INDEX FILETYPES TNSIZE TNQALITY BIGSIZE \
1.23      as        176:                BIGQALITY MAILTO MAILFROM URL EXIF INDEXIMG LINK NAVI
1.5       as        177:        do
                    178:                v=$(make -f $ARG_INFOFILE -V$n)
                    179:                test "$v" = "" && continue
                    180:                eval "ARG_$n='$v'"
                    181:        done    
                    182: fi
                    183: 
1.18      as        184: #paras from iso to utf-8
1.15      as        185: for n in $(set | awk -F= '{print $1}')
                    186: do
                    187:        echo "$n" | grep -q '^ARG_' || continue
1.23      as        188:        eval "$n=\"\$(echo \"\$$n\" | $BIN_ICONV -f ISO-8859-1 -t UTF-8)\""
1.15      as        189: done
                    190: 
1.6       as        191: # fix url
                    192: if [ "$ARG_URL" != "" ]
                    193: then
                    194:        echo "$ARG_URL" | grep -q '/$' || ARG_URL="${ARG_URL}/"
                    195: fi
                    196: 
1.1       as        197: # if no images defined, find files
                    198: if [ "$ARG_FILES" = "" -a "$ARG_FILETYPES" != "" ]
                    199: then
                    200:        match=
                    201:        for type in $ARG_FILETYPES
                    202:        do
                    203:                match="$match *.$(echo $type | tr '[:lower:]' '[:upper:]')"
                    204:                match="$match *.$(echo $type | tr '[:upper:]' '[:lower:]')"
                    205:        done
1.18      as        206:        FLIST=$(ls $match 2>/dev/null)
                    207: else
                    208:        FLIST="$ARG_FILES"
1.1       as        209: fi
                    210: 
1.23      as        211: # Filter big, thumbnails and curipted images
1.1       as        212: echo -n "find images"
1.18      as        213: FLISTF=
                    214: for file in $FLIST
1.1       as        215: do
                    216:        test $(echo $file | grep -E '\.(tn|big)\.jpg$') && continue
1.23      as        217:        test $($BIN_JPEGINFO $file | grep -Eq 'WARNING|ERROR') && continue
1.18      as        218:        FLISTF="$FLISTF $file"
1.1       as        219:        echo -n " $file"
                    220: done
1.18      as        221: echo
                    222: FLIST="$FLISTF"
1.1       as        223: 
                    224: test $(echo $FLIST | wc -w) -eq 0 && err "error: no image found in \`\`$ARG_DIR''"
                    225: 
1.13      as        226: # use exif createtime to order images
                    227: FLIST=$(
                    228:        (
                    229:                for file in $FLIST
                    230:                do
                    231:                        echo "$(get_exif_ts $file) $file"
                    232:                done
                    233:        ) | sort -n | awk '{print $2}'
                    234: )
                    235: 
1.18      as        236: # if no master image, we use the first one
                    237: test "$ARG_INDEXIMG" = "" && ARG_INDEXIMG=$(echo $FLIST | awk '{print $1}')
                    238: 
1.19      as        239: # find start and end ts
                    240: ARG_STARTTS=$(get_exif_ts $(echo $FLIST | awk '{print $1}'))
                    241: ARG_ENDTS=$(get_exif_ts $(echo $FLIST | awk '{print $NF}'))
                    242: 
1.5       as        243: # find new and to update images
                    244: FLIST_NEW=
                    245: FLIST_UPDATE=
                    246: echo "all: tns" > $MKAKEFILE
                    247: targets=""
                    248: for file in $FLIST
                    249: do
                    250:        tn="${file}.tn.jpg"
                    251:        if [ ! -f $tn ]
                    252:        then
                    253:                FLIST_NEW="$FLIST_NEW $file"
                    254:                continue
                    255:        fi
                    256: 
                    257:        targets="$targets $tn"
                    258:        (
                    259:                cat <<MAKE
                    260: $tn: $file
                    261:        @echo ${file}
                    262: MAKE
                    263:        ) >> $MKAKEFILE
                    264: done
                    265: echo "tns: $targets" >> $MKAKEFILE
                    266: FLIST_UPDATE=$(make -f $MKAKEFILE)
                    267: rm $MKAKEFILE
                    268: 
1.18      as        269: # save args
                    270: test -f $ARGFILE && rm $ARGFILE
                    271: echo "save settings"
                    272: set | while read n
                    273: do
                    274:        echo "$n" | grep -q '^ARG_' || continue
1.24      as        275:        echo "$n" | grep -q '^ARG_UPDATE' && continue
1.18      as        276:        echo "$n" >> $ARGFILE
                    277: done
                    278: 
1.5       as        279: # update and no new and update images found
                    280: if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" -a "$FLIST_UPDATE" = "" ]
                    281: then
                    282:        echo nothing to do
                    283:        exit 1
                    284: fi
                    285: 
                    286: if [ $ARG_UPDATE -eq 1 ]
                    287: then
                    288:        echo new images: $FLIST_NEW
                    289:        echo update images: $FLIST_UPDATE
                    290: fi
                    291: 
1.1       as        292: # Create make file to rotate und convert all big and thumbnails.
                    293: # Is needed to define the new real image sizes.
                    294: echo "all: tns" > $MKAKEFILE
                    295: targets=""
                    296: for file in $FLIST
                    297: do
                    298:        tn="${file}.tn.jpg"
                    299:        big="${file}.big.jpg"
                    300:        targets="$targets $tn"
                    301: 
                    302:        target=
                    303:        if [ $ARG_UPDATE -eq 1 ]
                    304:        then
                    305:                target="$tn: $ME $file"
                    306:        else
                    307:                target="$tn::"
                    308:        fi
                    309: 
                    310:        (
1.5       as        311:                cat <<MAKE
1.1       as        312: $target
                    313:        $BIN_EXIFAUTOTRAN $file
                    314:        $BIN_CONVERT -verbose -thumbnail ${ARG_TNSIZE}x${ARG_TNSIZE} -quality $ARG_TNQALITY $file $tn
                    315:        $BIN_CONVERT -verbose -thumbnail ${ARG_BIGSIZE}x${ARG_BIGSIZE} -quality $ARG_BIGQALITY $file $big
                    316: MAKE
                    317:        ) >> $MKAKEFILE
                    318: done
                    319: echo "tns: $targets" >> $MKAKEFILE
                    320: 
1.22      as        321: make -f $MKAKEFILE
1.1       as        322: rm $MKAKEFILE
                    323: 
1.10      as        324: # create html index
                    325: echo create html index
                    326: (
                    327:        html_header
1.20      as        328: 
1.19      as        329:        startdate=
                    330:        test $ARG_STARTTS -ne -1 && startdate=$(date -r $ARG_STARTTS "+%d.%m.%Y")
                    331:        enddate=
                    332:        test $ARG_ENDTS -ne -1 && enddate=$(date -r $ARG_ENDTS "+%d.%m.%Y")
                    333:        date=
                    334:        test "$startdate" != "" && date="$startdate"
                    335:        test "$enddate" != "$startdate" && date="$date - $enddate"
1.20      as        336:        link=
                    337:        test "$ARG_LINK" != "" && link="<a href='$ARG_LINK'>overview</a>"
                    338:        html_title "$ARG_TITLE" "$link" "$date"
1.19      as        339: 
                    340:        for file in $FLIST
                    341:        do
                    342:                tn="${file}.tn.jpg"
                    343:                imglink="${file}.html"
                    344:                html_tn $tn $imglink
                    345:        done
1.10      as        346:        echo "<div class='clear'></div>"
                    347:        echo "<div class='c'>$ARG_TITLE $ARG_COPYRIGHT</div>"
                    348:        html_footer
                    349: ) > $ARG_INDEX
                    350: 
1.1       as        351: # create single html files
                    352: i=0
1.12      as        353: all=$(echo $FLIST | wc -w)
1.1       as        354: echo -n "create single html files "
                    355: for file in $FLIST
                    356: do
                    357:        h=$i
                    358:        i=$(($i + 1))
                    359:        j=$(($i + 1))
                    360: 
1.11      as        361:        # is update mod and no new images founded
                    362:        if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" ]
                    363:        then
                    364:                is_in_list "$FLIST_UPDATE" "$file" || continue
                    365:        fi
                    366: 
1.1       as        367:        big="${file}.big.jpg"
                    368:        imglink="${file}.html"
                    369: 
                    370:        if [ $i -ne 1 ]
                    371:        then
                    372:                linkb=$(echo $FLIST | awk "{print \$$h}").html
                    373:        fi
                    374:        linkn=$(echo $FLIST | awk "{print \$$j}")
                    375:        test "$linkn" != "" && linkn="$linkn.html"
                    376: 
                    377:        echo -n .
                    378:        (
                    379:                html_header
1.12      as        380:                html_img $file $i $all $big $ARG_INDEX "$linkb" "$linkn"
1.1       as        381:                html_footer
                    382:        ) > $imglink
                    383: done
                    384: echo
                    385: 
1.15      as        386: # .htaccess
                    387: if [ -f .htaccess ]
                    388: then
                    389:        grep -q 'AddCharset UTF-8 .html' < .htaccess || echo "AddCharset UTF-8 .html" >> .htaccess
                    390: else
                    391:        echo "AddCharset UTF-8 .html" > .htaccess
                    392: fi
                    393: 
1.6       as        394: test "$ARG_MAILTO" = "" && exit 0
                    395: 
                    396: # send info mail
                    397: if [ "$FLIST_NEW" != "" -o "$FLIST_UPDATE" != "" ]
                    398: then
                    399:        echo "send mail to $ARG_MAILTO"
                    400:        (
                    401:                echo "Hallo,"
                    402:                echo
                    403:                echo -n "the gallery $ARG_TITLE has been updated"
                    404:                if [ "$ARG_URL" != "" ]
                    405:                then
                    406:                        echo ":"
                    407:                        echo $ARG_URL
                    408:                else
                    409:                        echo "."
                    410:                fi
                    411: 
                    412:                suffix=
                    413:                test "$ARG_URL" != "" && suffix=.html
                    414: 
                    415:                if [ "$FLIST_NEW" != "" ]
                    416:                then
                    417:                        echo
                    418:                        echo "add pictures:"
                    419:                        for file in $FLIST_NEW
                    420:                        do
                    421:                                echo "- ${ARG_URL}${file}${suffix}"
                    422:                        done
                    423:                fi
                    424: 
                    425:                if [ "$FLIST_UPDATE" != "" ]
                    426:                then
                    427:                        echo
                    428:                        echo "update pictures:"
                    429:                        for file in $FLIST_UPDATE
                    430:                        do
                    431:                                echo "- ${ARG_URL}${file}${suffix}"
                    432:                        done
                    433:                fi
                    434:        ) | mysendmail "PGallery Update: $ARG_TITLE"
                    435: fi