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

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