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

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