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

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