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

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