diff options
| author | Paul Buetow <paul@buetow.org> | 2022-02-19 23:02:51 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2022-02-20 10:18:39 +0000 |
| commit | 38abc2ecb3cbc70aeb11d9d390d9a6ef53478a23 (patch) | |
| tree | 47b93bf8e569886f123dcf6b8d62167f7f31b0f1 /src | |
| parent | 6feae6c65217e8b82c33d3fd4c15be09293ebf9f (diff) | |
ShellCheck OK now. Also, some refactorings.
Diffstat (limited to 'src')
| -rwxr-xr-x | src/photoalbum.sh | 429 |
1 files changed, 216 insertions, 213 deletions
diff --git a/src/photoalbum.sh b/src/photoalbum.sh index 74e89b9..242e756 100755 --- a/src/photoalbum.sh +++ b/src/photoalbum.sh @@ -1,278 +1,281 @@ -#!/bin/bash +#!/bin/env bash # photoalbum (c) 2011 - 2014, 2022 by Paul Buetow # https://codeberg.org/foozone/photoalbum readonly VERSION='PHOTOALBUMVERSION' -readonly DEFAULTRC=/etc/default/photoalbum -readonly ARG1="${1}" ; shift -declare RC_FILE="${1}" ; shift +readonly DEFAULTRC='/etc/default/photoalbum' +declare ARG1="$1" ; shift +declare RC_FILE="$1" ; shift usage () { - cat - <<USAGE >&2 - Usage: - $0 clean|generate|version|makemake|recursive:DIR [rcfile] + cat - <<USAGE >&2 + Usage: + $0 clean|generate|version|makemake|recursive:DIR [rcfile] USAGE } makemake () { - [ ! -f ./photoalbumrc ] && cp "$DEFAULTRC" ./photoalbumrc - cat <<MAKEFILE > ./Makefile + [ ! -f ./photoalbumrc ] && cp "$DEFAULTRC" ./photoalbumrc + cat <<MAKEFILE > ./Makefile all: photoalbum generate photoalbumrc clean: photoalbum clean photoalbumrc MAKEFILE - 'echo You may now customize ./photoalbumrc and run make' + 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 - readonly 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 + # 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() { - readonly template=${1} ; shift - readonly html=${1} ; shift - readonly dist_html="${DIST_DIR}/${html_dir}" +template () { + declare template="$1" ; shift + declare html="$1" ; shift + declare dist_html="$DIST_DIR/$html_dir" + echo "Generating $dist_html/$html" - # Creating ${dist_html}/${html}.html from ${template}.tmpl - [ ! -d "${dist_html}" ] && mkdir -p "${dist_html}" - source "${TEMPLATE_DIR}/${template}.tmpl" >> "${dist_html}/${html}" + [ ! -d "$dist_html" ] && mkdir -p "$dist_html" + source "$TEMPLATE_DIR/$template.tmpl" >> "$dist_html/$html" } scalephotos () { - cd "${INCOMING_DIR}" && find ./ -type f $FIND_ARGS | sort | - while read 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 "Scaling ${photo} to ${destphoto_nospace}" - if [ ! -z "${GEOMETRY}" ]; then - convert -auto-orient \ - -geometry ${GEOMETRY} "${photo}" "${destphoto_nospace}" - else - convert -auto-orient "${photo}" "${destphoto_nospace}" - fi - fi - done + 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 "Scaling $photo to $destphoto_nospace" + if [ -n "$GEOMETRY" ]; then + convert -auto-orient -geometry "$GEOMETRY" "$photo" "$destphoto_nospace" + else + convert -auto-orient "$photo" "$destphoto_nospace" + fi + done } albumhtml () { - declare photos_dir="${1}" ; shift - declare html_dir="${1}" ; shift - declare thumbs_dir="${1}" ; shift - export backhref="${1}" ; shift - - declare -i num=1 - declare -i i=0 - - declare name="page-${num}" - - template header "${name}.html" - template header-first-add "${name}.html" - - cd "${DIST_DIR}/${photos_dir}" && find ./ -type f | sort | sed 's;^\./;;' | - while read photo; do - : $(( i++ )) - - if [ ${i} -gt ${MAXPREVIEWS} ]; then - i=1 - : $(( num++ )) - - declare next="page-${num}" - template next "${name}.html" - template footer "${name}.html" - - declare prev="${name}" - declare name="${next}" - template header "${name}.html" - template prev "${name}.html" - fi + declare photos_dir="$1" ; shift + declare html_dir="$1" ; shift + declare thumbs_dir="$1" ; shift + export backhref="$1" ; shift + + declare -i num=1 + declare -i i=0 + + declare name="page-$num" + + template header "$name.html" + template header-first-add "$name.html" + + cd "$DIST_DIR/$photos_dir" && find ./ -type f | sort | sed 's;^\./;;' | + while read -r photo; do + : $(( i++ )) + + if [ "$i" -gt "$MAXPREVIEWS" ]; then + i=1 + : $(( num++ )) + + declare next="page-$num" + template next "$name.html" + template footer "$name.html" + + export prev="$name" + declare name="$next" + template header "$name.html" + template prev "$name.html" + fi + + # Preview page + template preview "$name.html" + # View page + template header "$num-$i.html" + template view "$num-$i.html" + template footer "$num-$i.html" + + if [ -f "$DIST_DIR/$thumbs_dir/$photo" ]; then + echo "Already exists: $DIST_DIR/$thumbs_dir/$photo" + continue + fi + + dirname="$(dirname "$DIST_DIR/$thumbs_dir/$photo")" + test ! -d "$dirname" && mkdir -p "$dirname" + + echo "Creating thumb $DIST_DIR/$thumbs_dir/$photo" + convert -geometry "x$THUMBGEOMETRY" "$photo" "$DIST_DIR/$thumbs_dir/$photo" + done - # Preview page - template preview "${name}.html" + template footer "$(cd "$DIST_DIR/$html_dir";ls -t page-*.html | head -n 1)" - # View page - template header "${num}-${i}.html" - template view "${num}-${i}.html" - template footer "${num}-${i}.html" + cd "$DIST_DIR/$html_dir" && ls | grep '.*\.html$' | + grep -v page- | cut -d'-' -f1 | uniq | - if [ ! -f "${DIST_DIR}/${thumbs_dir}/${photo}" ]; then - dirname="$(dirname "${DIST_DIR}/${thumbs_dir}/${photo}")" - [ ! -d "${dirname}" ] && mkdir -p "${dirname}" + while read -r prefix; do + declare page="$(ls -t "$prefix"-*.html | head -n 1 | sed 's#\(.*\)-.*.html#\1#')" + declare lastview="$(ls -t "$prefix"-*.html | head -n 1 | sed 's/.*-\(.*\).html/\1/')" - echo "Creating thumb ${DIST_DIR}/${thumbs_dir}/${photo}"; - convert -geometry x${THUMBGEOMETRY} "${photo}" \ - "${DIST_DIR}/${thumbs_dir}/${photo}" - fi - done + declare prevredirect="${page}-0" + declare nextredirect="${page}-$((lastview+1))" - template footer \ - "$(cd "${DIST_DIR}/${html_dir}";ls -t page-*.html | head -n 1)" + declare redirect_page="$(( page-1 ))-${MAXPREVIEWS}" + template redirect "$prevredirect.html" - cd "${DIST_DIR}/${html_dir}" && ls ./*.html | grep -v page- | cut -d'-' -f1 | uniq | - while read prefix; do - declare page="$(ls -t "${prefix}"-*.html | head -n 1 | sed 's#\(.*\)-.*.html#\1#')" - declare lastview="$(ls -t "${prefix}"-*.html | head -n 1 | sed 's/.*-\(.*\).html/\1/')" + if [ "$lastview" -eq "$MAXPREVIEWS" ]; then + declare redirect_page="$(( page+1 ))-1" + else + declare redirect_page="${page}-$lastview" + template redirect "0-$MAXPREVIEWS.html" + redirect_page='1-1' + fi - declare prevredirect="${page}-0" - declare nextredirect="${page}-$((lastview+1))" + export redirect_page + template redirect "$nextredirect.html" + done - declare redirect_page="$(( page-1 ))-${MAXPREVIEWS}" - template redirect "${prevredirect}.html" + # Create per album index/redirect page + declare redirect_page='page-1' + template 'redirect' 'index.html' +} - if [ "$lastview" -eq "$MAXPREVIEWS" ]; then - declare redirect_page="$(( page+1 ))-1" +albumindexhtml () { + declare -a dirs=( "$1" ) + declare is_subalbum='no' + declare html_dir='html' + declare backhref='..' - else - declare redirect_page="${page}-${lastview}" - template redirect "0-${MAXPREVIEWS}.html" - redirect_page='1-1' - fi - export redirect_page - template redirect "${nextredirect}.html" - done + template 'header' 'index.html' + template 'header-first-add' 'index.html' - # Create per album index/redirect page - declare redirect_page='page-1' - template 'redirect' 'index.html' -} + for dir in ${dirs[*]}; do + declare basename="$(basename "$dir")" + export album="$basename" + declare thumbs_dir="$DIST_DIR/thumbs/$basename" + declare pictures="$(ls "$thumbs_dir" | wc -l)" + declare random_num="$(( 1 + RANDOM % pictures ))" + declare pages="$(( pictures / MAXPREVIEWS + 1 ))" + + export random_thumb="./thumbs/$basename"/$(find \ + "$thumbs_dir" -type f -printf "%f\n" | + head -n $random_num | tail -n 1) + + declare s='' + [ $pages -gt 1 ] && s='s' + export description="$pictures pictures / $pages page$s" + template 'index-preview' 'index.html' + done -albumindexhtml () { - declare -a dirs=( "${1}" ) - declare is_subalbum='no' - declare html_dir='html' - declare backhref='..' - - template 'header' 'index.html' - template 'header-first-add' 'index.html' - - for dir in ${dirs[*]}; do - declare basename="$(basename "$dir")" - # TODO: All exported vars in UPPERCASE - export album="$basename" - declare thumbs_dir="${DIST_DIR}/thumbs/${basename}" - declare pictures="$(ls "${thumbs_dir}" | wc -l)" - declare random_num="$(( 1 + RANDOM % pictures ))" - declare pages="$(( pictures / MAXPREVIEWS + 1 ))" - - export random_thumb="./thumbs/${basename}"/$(find \ - "${thumbs_dir}" -type f -printf "%f\n" | - head -n ${random_num} | tail -n 1) - - declare s='' - [ ${pages} -gt 1 ] && s='s' - export description="${pictures} pictures / ${pages} page${s}" - template 'index-preview' 'index.html' - done - - template 'footer' 'index.html' + template 'footer' 'index.html' } generate () { - if [ ! -d "${INCOMING_DIR}" ]; then - echo "ERROR: You have to create ${INCOMING_DIR} first" >&2 - exit 1 - fi - - if [ "${TARBALL_INCLUDE}" = yes ]; then - readonly base=$(basename "${INCOMING_DIR}") - readonly now=$(date +'%Y-%m-%d-%H%M%S') - readonly tarball_name="${base}-${now}${TARBALL_SUFFIX}" - fi + if [ ! -d "$INCOMING_DIR" ]; then + echo "ERROR: You have to create $INCOMING_DIR first" >&2 + exit 1 + fi - test ! -d "${DIST_DIR}/photos" && mkdir -p "${DIST_DIR}/photos" - scalephotos + 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 - find "${DIST_DIR}" -type f -name \*.html -delete - declare -a dirs=( $(find "${DIST_DIR}/photos" -mindepth 1 -maxdepth 1 -type d | sort) ) + test ! -d "$DIST_DIR/photos" && mkdir -p "$DIST_DIR/photos" + scalephotos - # Figure out wether we want sub-albums or not - if [[ "${SUB_ALBUMS}" != yes || ${#dirs[*]} -eq 0 ]]; then - export is_subalbum='no' - albumhtml 'photos' 'html' 'thumbs' '..' + find "$DIST_DIR" -type f -name \*.html -delete + declare -a dirs=( $(find "$DIST_DIR/photos" -mindepth 1 -maxdepth 1 -type d | sort) ) - else - export is_subalbum='yes' - for dir in ${dirs[*]}; do - declare basename="$(basename "${dir}")" - albumhtml "photos/${basename}" "html/${basename}" "thumbs/${basename}" '../..' - done + # Figure out wether we want sub-albums or not + if [[ "$SUB_ALBUMS" != yes || ${#dirs[*]} -eq 0 ]]; then + export is_subalbum='no' + albumhtml 'photos' 'html' 'thumbs' '..' - # Create an album selection screen - albumindexhtml "${dirs[*]}" - fi + else + export is_subalbum='yes' + for dir in ${dirs[*]}; do + declare basename="$(basename "$dir")" + albumhtml "photos/$basename" "html/$basename" "thumbs/$basename" '../..' + done + + # Create an album selection screen + albumindexhtml "${dirs[*]}" + fi - # Create top level index/redirect page - declare html_dir='./' - declare redirect_page='./html/index' - template 'redirect' 'index.html' + # 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 [ "$TARBALL_INCLUDE" = 'yes' ]; then + tarball + fi } recursive () { - local dir="$(cut -d: -f2 <<< "$ARG1")" - - if [ ! -d "$dir" ]; then - echo "Directory $dir does not exist!" - exit 1 - fi + local dir="$(cut -d: -f2 <<< "$ARG1")" - find "$dir" -type d | grep -v '\.HTML' | while read -r d; do - test ! -d "$d.HTML" && mkdir "$d.HTML " - rc_file="$d.HTML/photoalbumrc" - cd "$d.HTML" && cp "$RC_FILE" "$rc_file" && chmod 644 "$rc_file" - - { - echo "INCOMING_DIR=$d"; - echo "DIST_DIR=$d.HTML"; - echo "ORIGINAL_BASEPATH=../../$(basename "$d")"; - echo 'FIND_ARGS="-maxdepth 1"'; - } >> "$rc_file" + if [ ! -d "$dir" ]; then + echo "Directory $dir does not exist!" + exit 1 + fi - photoalbum generate "$rc_file" - cd - &>/dev/null - test -d "$d.HTML" && test ! -d "$d.HTML/thumbs" && rm -Rf "$d.HTML" - done + find "$dir" -type d | grep -v '\.HTML' | + while read -r d; do + test ! -d "$d.HTML" && mkdir "$d.HTML " + rc_file="$d.HTML/photoalbumrc" + cd "$d.HTML" && cp "$RC_FILE" "$rc_file" && chmod 644 "$rc_file" + { + echo "INCOMING_DIR=$d"; + echo "DIST_DIR=$d.HTML"; + echo "ORIGINAL_BASEPATH=../../$(basename "$d")"; + } >> "$rc_file" + + photoalbum generate "$rc_file" + cd - &>/dev/null + test -d "$d.HTML" && test ! -d "$d.HTML/thumbs" && rm -Rf "$d.HTML" + done } -if [ -z "${RC_FILE}" ]; then - if [ -f ~/.photoalbumrc ]; then - RC_FILE=~/.photoalbumrc - else - RC_FILE="${DEFAULTRC}" - 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 +if [ ! -f "$RC_FILE" ]; then + echo "Error: Can not find config file $RC_FILE" >&2 + exit 1 fi -source "${RC_FILE}" +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;; - recursive*) recursive;; - *) usage;; +case "$ARG1" in + clean) [ -d "$DIST_DIR" ] && rm -Rf "$DIST_DIR";; + generate) generate;; + version) echo "This is Photoalbum Version $VERSION";; + makemake) makemake;; + recursive*) recursive;; + *) usage;; esac exit 0 |
