#!/bin/env bash # photoalbum (c) 2011 - 2014, 2022 by Paul Buetow # https://codeberg.org/snonux/photoalbum readonly VERSION='PHOTOALBUMVERSION' readonly DEFAULTRC='/etc/default/photoalbum' declare ARG1="$1" ; shift declare RC_FILE="$1" ; shift usage () { cat - <&2 Usage: $0 clean|generate|version|makemake [rcfile] USAGE } makemake () { [ ! -f ./photoalbumrc ] && cp "$DEFAULTRC" ./photoalbumrc cat < ./Makefile all: photoalbum generate photoalbumrc clean: photoalbum clean photoalbumrc MAKEFILE echo 'You may now customize ./photoalbumrc and run make' } tarball () { # Cleanup tarball from prev run if any find "$DIST_DIR" -maxdepth 1 -type f -name \*.tar -delete declare base="$(basename "$INCOMING_DIR")" echo "Creating tarball $DIST_DIR/$tarball_name from $INCOMING_DIR" cd "$(dirname "$INCOMING_DIR")" tar "$TAR_OPTS" -f "$DIST_DIR/$tarball_name" "$base" cd - &>/dev/null } template () { declare template="$1" ; shift declare html="$1" ; shift declare dist_html="$DIST_DIR/$html_dir" echo "Generating $dist_html/$html" [ ! -d "$dist_html" ] && mkdir -p "$dist_html" source "$TEMPLATE_DIR/$template.tmpl" >> "$dist_html/$html" } cleanphotos () { find "$DIST_DIR/photos" -maxdepth 1 -type f | while read photo; do local basename=$(basename $photo) if [ -f "$INCOMING_DIR/$basename" ]; then continue fi echo "Cleaning up $photo" for sub in thumbs blurs photos; do if [ -f "$DIST_DIR/$sub/$basename" ]; then rm -v "$DIST_DIR/$sub/$basename" fi done done } scalephotos () { cd "$INCOMING_DIR" && find ./ -maxdepth 1 -type f | sort | while read -r photo; do declare photo="$(sed 's#^\./##' <<< "$photo")" declare destphoto="$DIST_DIR/photos/$photo" declare destphoto_nospace="${destphoto// /_}" declare dirname="$(dirname "$destphoto")" [ ! -d "$dirname" ] && mkdir -p "$dirname" if [ -f "$destphoto_nospace" ]; then echo "Already exists: $destphoto_nospace" continue fi echo "Processing $photo to $destphoto_nospace" if [ -n "$HEIGHT" ]; then # Scale down size. convert -auto-orient -geometry "$HEIGHT" "$photo" "$destphoto_nospace" else # Keep original size convert -auto-orient "$photo" "$destphoto_nospace" fi done } randomphoto () { declare photos_dir="$1" ; shift basename $(find "$photos_dir" -type f -maxpdeth 1 -mindepth 1 | sort -R | head -n 1) } random_animation_css_class () { local -r speed="$1"; shift cat <&2 exit 1 fi if [ "$TARBALL_INCLUDE" = yes ]; then declare base="$(basename "$INCOMING_DIR")" declare now="$(date +'%Y-%m-%d-%H%M%S')" declare tarball_name="${base}-${now}$TARBALL_SUFFIX" fi test ! -d "$DIST_DIR/photos" && mkdir -p "$DIST_DIR/photos" cleanphotos scalephotos find "$DIST_DIR" -type f -name \*.html -delete declare -a dirs=( $(find "$DIST_DIR/photos" -mindepth 1 -maxdepth 1 -type d | sort) ) albumhtml 'photos' 'html' 'thumbs' 'blurs' '..' # Create top level index/redirect page declare html_dir='./' declare redirect_page='./html/index' template 'redirect' 'index.html' if [ "$TARBALL_INCLUDE" = 'yes' ]; then tarball fi } if [ -z "$RC_FILE" ]; then if [ -f photoalbumrc ]; then RC_FILE=photoalbumrc elif [ -f ~/.photoalbumrc ]; then RC_FILE=~/.photoalbumrc else RC_FILE="$DEFAULTRC" fi fi if [ ! -f "$RC_FILE" ]; then echo "Error: Can not find config file $RC_FILE" >&2 exit 1 fi source "$RC_FILE" case "$ARG1" in clean) [ -d "$DIST_DIR" ] && rm -Rf "$DIST_DIR";; generate) generate;; version) echo "This is Photoalbum Version $VERSION";; makemake) makemake;; *) usage;; esac exit 0