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

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