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

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