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

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