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

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.23    ! as          8: # $Paefchen: scripts/pgallery/src/pgallery.sh,v 1.22 2012/09/11 15:40:43 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
                    274:        echo "$n" >> $ARGFILE
                    275: done
                    276: 
1.5       as        277: # update and no new and update images found
                    278: if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" -a "$FLIST_UPDATE" = "" ]
                    279: then
                    280:        echo nothing to do
                    281:        exit 1
                    282: fi
                    283: 
                    284: if [ $ARG_UPDATE -eq 1 ]
                    285: then
                    286:        echo new images: $FLIST_NEW
                    287:        echo update images: $FLIST_UPDATE
                    288: fi
                    289: 
1.1       as        290: # Create make file to rotate und convert all big and thumbnails.
                    291: # Is needed to define the new real image sizes.
                    292: echo "all: tns" > $MKAKEFILE
                    293: targets=""
                    294: for file in $FLIST
                    295: do
                    296:        tn="${file}.tn.jpg"
                    297:        big="${file}.big.jpg"
                    298:        targets="$targets $tn"
                    299: 
                    300:        target=
                    301:        if [ $ARG_UPDATE -eq 1 ]
                    302:        then
                    303:                target="$tn: $ME $file"
                    304:        else
                    305:                target="$tn::"
                    306:        fi
                    307: 
                    308:        (
1.5       as        309:                cat <<MAKE
1.1       as        310: $target
                    311:        $BIN_EXIFAUTOTRAN $file
                    312:        $BIN_CONVERT -verbose -thumbnail ${ARG_TNSIZE}x${ARG_TNSIZE} -quality $ARG_TNQALITY $file $tn
                    313:        $BIN_CONVERT -verbose -thumbnail ${ARG_BIGSIZE}x${ARG_BIGSIZE} -quality $ARG_BIGQALITY $file $big
                    314: MAKE
                    315:        ) >> $MKAKEFILE
                    316: done
                    317: echo "tns: $targets" >> $MKAKEFILE
                    318: 
1.22      as        319: make -f $MKAKEFILE
1.1       as        320: rm $MKAKEFILE
                    321: 
1.10      as        322: # create html index
                    323: echo create html index
                    324: (
                    325:        html_header
1.20      as        326: 
1.19      as        327:        startdate=
                    328:        test $ARG_STARTTS -ne -1 && startdate=$(date -r $ARG_STARTTS "+%d.%m.%Y")
                    329:        enddate=
                    330:        test $ARG_ENDTS -ne -1 && enddate=$(date -r $ARG_ENDTS "+%d.%m.%Y")
                    331:        date=
                    332:        test "$startdate" != "" && date="$startdate"
                    333:        test "$enddate" != "$startdate" && date="$date - $enddate"
1.20      as        334:        link=
                    335:        test "$ARG_LINK" != "" && link="<a href='$ARG_LINK'>overview</a>"
                    336:        html_title "$ARG_TITLE" "$link" "$date"
1.19      as        337: 
                    338:        for file in $FLIST
                    339:        do
                    340:                tn="${file}.tn.jpg"
                    341:                imglink="${file}.html"
                    342:                html_tn $tn $imglink
                    343:        done
1.10      as        344:        echo "<div class='clear'></div>"
                    345:        echo "<div class='c'>$ARG_TITLE $ARG_COPYRIGHT</div>"
                    346:        html_footer
                    347: ) > $ARG_INDEX
                    348: 
1.1       as        349: # create single html files
                    350: i=0
1.12      as        351: all=$(echo $FLIST | wc -w)
1.1       as        352: echo -n "create single html files "
                    353: for file in $FLIST
                    354: do
                    355:        h=$i
                    356:        i=$(($i + 1))
                    357:        j=$(($i + 1))
                    358: 
1.11      as        359:        # is update mod and no new images founded
                    360:        if [ $ARG_UPDATE -eq 1 -a "$FLIST_NEW" = "" ]
                    361:        then
                    362:                is_in_list "$FLIST_UPDATE" "$file" || continue
                    363:        fi
                    364: 
1.1       as        365:        big="${file}.big.jpg"
                    366:        imglink="${file}.html"
                    367: 
                    368:        if [ $i -ne 1 ]
                    369:        then
                    370:                linkb=$(echo $FLIST | awk "{print \$$h}").html
                    371:        fi
                    372:        linkn=$(echo $FLIST | awk "{print \$$j}")
                    373:        test "$linkn" != "" && linkn="$linkn.html"
                    374: 
                    375:        echo -n .
                    376:        (
                    377:                html_header
1.12      as        378:                html_img $file $i $all $big $ARG_INDEX "$linkb" "$linkn"
1.1       as        379:                html_footer
                    380:        ) > $imglink
                    381: done
                    382: echo
                    383: 
1.15      as        384: # .htaccess
                    385: if [ -f .htaccess ]
                    386: then
                    387:        grep -q 'AddCharset UTF-8 .html' < .htaccess || echo "AddCharset UTF-8 .html" >> .htaccess
                    388: else
                    389:        echo "AddCharset UTF-8 .html" > .htaccess
                    390: fi
                    391: 
1.6       as        392: test "$ARG_MAILTO" = "" && exit 0
                    393: 
                    394: # send info mail
                    395: if [ "$FLIST_NEW" != "" -o "$FLIST_UPDATE" != "" ]
                    396: then
                    397:        echo "send mail to $ARG_MAILTO"
                    398:        (
                    399:                echo "Hallo,"
                    400:                echo
                    401:                echo -n "the gallery $ARG_TITLE has been updated"
                    402:                if [ "$ARG_URL" != "" ]
                    403:                then
                    404:                        echo ":"
                    405:                        echo $ARG_URL
                    406:                else
                    407:                        echo "."
                    408:                fi
                    409: 
                    410:                suffix=
                    411:                test "$ARG_URL" != "" && suffix=.html
                    412: 
                    413:                if [ "$FLIST_NEW" != "" ]
                    414:                then
                    415:                        echo
                    416:                        echo "add pictures:"
                    417:                        for file in $FLIST_NEW
                    418:                        do
                    419:                                echo "- ${ARG_URL}${file}${suffix}"
                    420:                        done
                    421:                fi
                    422: 
                    423:                if [ "$FLIST_UPDATE" != "" ]
                    424:                then
                    425:                        echo
                    426:                        echo "update pictures:"
                    427:                        for file in $FLIST_UPDATE
                    428:                        do
                    429:                                echo "- ${ARG_URL}${file}${suffix}"
                    430:                        done
                    431:                fi
                    432:        ) | mysendmail "PGallery Update: $ARG_TITLE"
                    433: fi