#!/bin/sh

printf '%s' \
'/*!
 * mkpicindex.sh - v0.1
 * https://codevoid.de
 * Copyright (c) 2019 Stefan Hagen
 * Licensed under the ISC license.
 * 
 * style.css, justify.js:
 * justifiedGallery - v4.0.0-alpha
 * http://miromannino.github.io/Justified-Gallery/
 * Copyright (c) 2019 Miro Mannino
 * Licensed under the MIT license.
 */
' > LICENSE

# CONFIGURE
GALLERY_TITLE="My Gallery" # browser title
GALLERY_ROW_HEIGHT=150     # how high will the justified rows be?
GALLERY_RANDOMIZE=false     # enable random sorting (true,false)
BODY_STYLE="color:orange; background:black;" # <body style="?">
THUMBNAIL_QUALITY=83       # quality for thumbnails
THUMBNAIL_PATH="thm"       # relative path to thumbnail folder
INCLUDE_HEADER="HEADER"    # file with html to include before gallery
INCLUDE_FOOTER="FOOTER"    # file with html to include after gallery

### ZE PROGAM STARTZ HERE ##############################################
cleanup() {
    # DELETE BROKEN IMAGES
    printf '%s\n' "Removing incomplete thumbnails." >&2
    find $THUMBNAIL_PATH -name "*_tmp.*" -exec rm -v "{}" \;
    exit 1
}
trap cleanup 1 2 3 6

# CREATE THUMBNAIL DIRECTORY
mkdir -p $THUMBNAIL_PATH

# INCLUDE CUSTOM HEADER & FOOTER
FOOTER=$([ -f $INCLUDE_FOOTER ] && cat $INCLUDE_FOOTER | sed 's/^/        /g')
HEADER=$([ -f $INCLUDE_HEADER ] && cat $INCLUDE_HEADER | sed 's/^/        /g')

# PRINT HEADER
printf '%s%s%s%s%s\n' \
'<html>
    <head>
        <title>'"$GALLERY_TITLE"'</title>
        <meta name="viewport" content="width=device-width">
        <link href="style.css" rel="stylesheet">
        <script src="justify.js"></script>
    </head>
    <body style="'"$BODY_STYLE"'">
'       "$HEADER"'
        <div id="base">'


# RESCALE AND ADD IMAGE
# PARAM 1: original
#       2: thumbnail_basename
#       3: thumbnail_format (extension)
add_image() {
    local FILE="$1"
    local THUMB="$THUMBNAIL_PATH/$2-$GALLERY_ROW_HEIGHT"
    local EXT="$3"
    printf '%s\n' "Adding image: $FILE" >&2
    if ! [ -f "$THUMB.$EXT" ] && [ "$FILE" != "$THUMB.$EXT" ];
        then convert -quality $THUMBNAIL_QUALITY -sharpen 2x2 \
                     -coalesce -resize 6000x$GALLERY_ROW_HEIGHT\> \
                     -deconstruct "$FILE" "${THUMB}_tmp.$EXT" && \
        mv "${THUMB}_tmp.$EXT" "$THUMB.$EXT"
    fi
    local WH="$(identify -format ' %w %h ' "$THUMB.$EXT" \
                | awk '{ print "width="$1" height="$2 }')"
    printf '            %s\n' "<a href=\"$FILE\">"
    printf '                %s\n' "<img $WH src=\"$THUMB.$EXT\">"
    printf '            %s\n' '</a>'
}

### MAIN LOOP ##########################################################

for file in *.*;
do
    if [ -f "$file" ];
    then
        case $(printf '%s' ${file##*.} | tr '[:upper:]' '[:lower:]') in
            jpg|jpeg|png) 
                add_image "$file" "${file%%.*}" "jpeg" ;;
            gif)
                add_image "$file" "${file%%.*}" "gif" ;;
            *) printf '%s\n' "Ignoring: $file" >&2 ;;
        esac
    fi
done

### MAIN LOOP END ######################################################

# PRINT FOOTER
printf '%s%s\n' \
"        </div>
        <script>
            \$(\"#base\").justifiedGallery({
                rowHeight : $GALLERY_ROW_HEIGHT/0.8,
                maxRowHeight : $GALLERY_ROW_HEIGHT,
                randomize : $GALLERY_RANDOMIZE,
                waitThumbnailsLoad : false,
                lastRow : \"nojustify\",
                margins : 3
            });
        </script>
"        "$FOOTER
    </body>
</html>"

### ASSETS (style.css, justify.js) #####################################
printf '%s' '
[encoded data, 47961 characters]
' | base64 -d | gunzip > justify.js


printf '%s' '
H4sICOujrl0AA3N0eWxlLmNzcwDVVk1vnDAQve+vmCaqlEQ1y6abJiVKLz1UrZJb/oABA5MY
G9lmN5sq/73GfKzYBUKORVotY7/xzDyPH15efFrABTyV2mCCLP5FOWdqBwQ2a8/3fEJ5kdEK
khlTBMtljkrmVAgU0kvRZGXooVz+af1Js8Cycvkpi53CNDNwFp3Dpb/6Dg/WHR5q/wpyjxET
msVQipgpMBmDh9+PwOthz0KWC6/LjqRNen8XAFuMTRbAyvc/31qzkBoNShGAYpwa3LBqVG6Y
SrjcBpBhHDNxu3gbWu8H0C/D4zFuRmYSTEvFXCr72DTUkpfGxY5RF5zuAkDBUTASchk9DycF
sLyAkEbPqZKWiQBOb9xzC48SMrphkCq6A7tcxDLJLVUathly5hhrc0INXNIYRWrJMRlsKZrH
rMxDQZHrezsFd5BQrllFK9gKuGEqgBO3yWeyoBGa3d3KPz9xadZ2AL63quycqhRtjb6jm8ZV
IGeNUWp/mKfjxE7ON/ROQejMINOYLtA0rJrVm+lA4/NdkHEInRlkGtOrxsIm+tPIIoCr+vRw
lpjOGN1ogFCquGoZIYVbY6SFjjposkm8iBZVepNlvwPq6m5xrvDuCLb5DjMRSmNkfljwVfHi
JrtjSSLJpS321HfPnjf36pSuee8x2HjZ81pHS6QwROMrs+J1WcdwQ1tWr/C1XtqNJTRHbvPX
VGiimcJkFpPtP9mgxpCzWczOdDpi+tCvzzwKSzjlE81yfaw315VtCQmf0RCjbPHNrjUY26l+
roFRzQg6/SS5fJ2JlPNwM0AjO+E9pYQJo3Y9RkbV9rD8Vb/r2uadHWxKxYbQ07o35DEuP2Pr
t0r0MRL+6w6oS7VXmwEhGnPTBQph70Hjot2TqlpmSC1C5HJdi0knYCu/eAG/+TtS+Y9txf7O
0h3o94qwu15QcagG/avQ/O9Hd+G7qavMGrVszE5xYe2KXk+J9/5LRpS9L5U6gG8V/m3xD0Dc
I3kQCwAA
' | base64 -d | gunzip > style.css
