diff options
| author | Paul Buetow <paul@buetow.org> | 2022-02-14 23:04:18 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2022-02-14 23:04:18 +0000 |
| commit | 75c62b3abb4f84c2dc967b9f9289df10cb6af227 (patch) | |
| tree | 4250df03f18abb54587e28b6c3008594453b8eac | |
| parent | 78771e1f4f0f7c2a503a486ba88570c1ac2e2ba8 (diff) | |
refactor a bit - closer to ShellCheck safety but not quite there yet
| -rwxr-xr-x | src/photoalbum.sh | 175 |
1 files changed, 85 insertions, 90 deletions
diff --git a/src/photoalbum.sh b/src/photoalbum.sh index 410d7cb..eb1e06a 100755 --- a/src/photoalbum.sh +++ b/src/photoalbum.sh @@ -1,45 +1,45 @@ #!/bin/bash -# photoalbum (c) 2011 - 2014 by Paul C. Buetow +# photoalbum (c) 2011 - 2014, 2022 by Paul Buetow # https://codeberg.org/foozone/photoalbum declare -r VERSION='PHOTOALBUMVERSION' -declare -r DEFAULTRC=/etc/default/photoalbum +declare -r DEFAULTRC='/etc/default/photoalbum' declare -r ARG1="${1}" ; shift declare RC_FILE="${1}" ; shift -function usage { +usage () { cat - <<USAGE >&2 Usage: $0 clean|generate|version|makemake|recursive:DIR [rcfile] USAGE } -function makemake { - [ ! -f ./photoalbumrc ] && cp $DEFAULTRC ./photoalbumrc +makemake () { + [ ! -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' } -function tarball { +tarball () { # Cleanup tarball from prev run if any find "${DIST_DIR}" -maxdepth 1 -type f -name \*.tar -delete - declare -r base=$(basename "${INCOMING_DIR}") + declare -r base="$(basename "${INCOMING_DIR}")" echo "Creating tarball ${DIST_DIR}/${tarball_name} from ${INCOMING_DIR}" - cd $(dirname "${INCOMING_DIR}") + cd "$(dirname "${INCOMING_DIR}")" tar $TAR_OPTS -f "${DIST_DIR}/${tarball_name}" "${base}" cd - &>/dev/null } -function template { - declare -r template=${1} ; shift - declare -r html=${1} ; shift +template () { + declare -r template="${1}" ; shift + declare -r html="${1}" ; shift declare -r dist_html="${DIST_DIR}/${html_dir}" # Creating ${dist_html}/${html}.html from ${template}.tmpl @@ -47,14 +47,14 @@ function template { source "${TEMPLATE_DIR}/${template}.tmpl" >> "${dist_html}/${html}" } -function scalephotos { +scalephotos () { cd "${INCOMING_DIR}" && find ./ -type f $FIND_ARGS | sort | while read photo; do - declare photo=$(sed 's#^\./##' <<< "${photo}") + declare photo="$(sed 's#^\./##' <<< "${photo}")" declare destphoto="${DIST_DIR}/photos/${photo}" - declare destphoto_nospace=${destphoto// /_} + declare destphoto_nospace="${destphoto// /_}" - declare dirname=$(dirname "${destphoto}") + declare dirname="$(dirname "${destphoto}")" [ ! -d "${dirname}" ] && mkdir -p "${dirname}" if [ ! -f "${destphoto_nospace}" ]; then @@ -69,7 +69,7 @@ function scalephotos { done } -function albumhtml { +albumhtml () { declare photos_dir="${1}" ; shift declare html_dir="${1}" ; shift declare thumbs_dir="${1}" ; shift @@ -78,10 +78,10 @@ function albumhtml { declare -i num=1 declare -i i=0 - declare name=page-${num} + declare name="page-${num}" - template header ${name}.html - template header-first-add ${name}.html + 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 @@ -91,26 +91,26 @@ function albumhtml { i=1 : $(( num++ )) - declare next=page-${num} - template next ${name}.html - template footer ${name}.html + 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 + declare prev="${name}" + declare name="${next}" + template header "${name}.html" + template prev "${name}.html" fi # Preview page - template preview ${name}.html + template preview "${name}.html" # View page - template header ${num}-${i}.html - template view ${num}-${i}.html - template footer ${num}-${i}.html + template header "${num}-${i}.html" + template view "${num}-${i}.html" + template footer "${num}-${i}.html" if [ ! -f "${DIST_DIR}/${thumbs_dir}/${photo}" ]; then - dirname=$(dirname "${DIST_DIR}/${thumbs_dir}/${photo}") + dirname="$(dirname "${DIST_DIR}/${thumbs_dir}/${photo}")" [ ! -d "${dirname}" ] && mkdir -p "${dirname}" echo "Creating thumb ${DIST_DIR}/${thumbs_dir}/${photo}"; @@ -120,76 +120,73 @@ function albumhtml { done template footer \ - $(cd "${DIST_DIR}/${html_dir}";ls -t page-*.html | head -n 1) + "$(cd "${DIST_DIR}/${html_dir}";ls -t page-*.html | head -n 1)" 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 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/')" - declare lastview=$(ls -t ${prefix}-*.html | - head -n 1 | sed 's/.*-\(.*\).html/\1/') + declare prevredirect="${page}-0" + declare nextredirect="${page}-$((lastview+1))" - declare prevredirect=${page}-0 - declare nextredirect=${page}-$((lastview+1)) - - declare redirect_page=$(( page-1 ))-${MAXPREVIEWS} - template redirect ${prevredirect}.html + declare redirect_page="$(( page-1 ))-${MAXPREVIEWS}" + template redirect "${prevredirect}.html" if [ ${lastview} -eq ${MAXPREVIEWS} ]; then - declare redirect_page=$(( page+1 ))-1 + declare redirect_page="$(( page+1 ))-1" else - declare redirect_page=${page}-${lastview} - template redirect 0-${MAXPREVIEWS}.html - redirect_page=1-1 + declare redirect_page="${page}-${lastview}" + template redirect "0-${MAXPREVIEWS}.html" + redirect_page='1-1' fi - template redirect ${nextredirect}.html + template redirect "${nextredirect}.html" done # Create per album index/redirect page - declare redirect_page=page-1 - template redirect index.html + declare redirect_page='page-1' + template 'redirect' 'index.html' } -function albumindexhtml { +albumindexhtml () { declare -a dirs=( "${1}" ) - declare is_subalbum=no - declare html_dir=html - declare backhref=.. + declare is_subalbum='no' + declare html_dir='html' + declare backhref='..' - template header index.html - template header-first-add index.html + template 'header' 'index.html' + template 'header-first-add' 'index.html' for dir in ${dirs[*]}; do - declare basename=$(basename "$dir") - declare album=$basename + declare basename="$(basename "$dir")" + declare 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 )) + declare pictures="$(ls "${thumbs_dir}" | wc -l)" + declare random_num="$(( 1 + $RANDOM % $pictures ))" + declare pages="$(( $pictures / $MAXPREVIEWS + 1 ))" declare random_thumb="./thumbs/${basename}"/$(find \ "${thumbs_dir}" -type f -printf "%f\n" | head -n ${random_num} | tail -n 1) - [ ${pages} -gt 1 ] && declare s=s || declare s='' + [ ${pages} -gt 1 ] && declare s='s' || declare s='' declare description="${pictures} pictures / ${pages} page${s}" - template index-preview index.html + template 'index-preview' 'index.html' done - template footer index.html + template 'footer' 'index.html' } -function generate { +generate () { if [ ! -d "${INCOMING_DIR}" ]; then echo "ERROR: You have to create ${INCOMING_DIR} first" >&2 exit 1 fi if [ "${TARBALL_INCLUDE}" = yes ]; then - declare -r base=$(basename "${INCOMING_DIR}") - declare -r now=$(date +'%Y-%m-%d-%H%M%S') + declare -r base="$(basename "${INCOMING_DIR}")" + declare -r now="$(date +'%Y-%m-%d-%H%M%S')" declare -r tarball_name="${base}-${now}${TARBALL_SUFFIX}" fi @@ -197,20 +194,18 @@ function generate { scalephotos find "${DIST_DIR}" -type f -name \*.html -delete - declare -a dirs=( $(find "${DIST_DIR}/photos" \ - -mindepth 1 -maxdepth 1 -type d | sort) ) + declare -a dirs=( $(find "${DIST_DIR}/photos" -mindepth 1 -maxdepth 1 -type d | sort) ) # Figure out wether we want sub-albums or not if [[ "${SUB_ALBUMS}" != yes || ${#dirs[*]} -eq 0 ]]; then - declare is_subalbum=no - albumhtml photos html thumbs .. + declare is_subalbum='no' + albumhtml 'photos' 'html' 'thumbs' '..' else - declare is_subalbum=yes + declare is_subalbum='yes' for dir in ${dirs[*]}; do - declare basename=$(basename "${dir}") - albumhtml \ - "photos/${basename}" "html/${basename}" "thumbs/${basename}" ../.. + declare basename="$(basename "${dir}")" + albumhtml "photos/${basename}" "html/${basename}" "thumbs/${basename}" '../..' done # Create an album selection screen @@ -218,34 +213,34 @@ function generate { fi # Create top level index/redirect page - declare html_dir=./ - declare redirect_page=./html/index - template redirect index.html + declare html_dir='./' + declare redirect_page='./html/index' + template 'redirect' 'index.html' - if [ "${TARBALL_INCLUDE}" = yes ]; then + if [ "${TARBALL_INCLUDE}" = 'yes' ]; then tarball fi } -function recursive { - local dir=$(cut -d: -f2 <<< $ARG1) +recursive () { + local dir="$(cut -d: -f2 <<< $ARG1)" - if [ ! -d $dir ]; then + if [ ! -d "$dir" ]; then echo "Directory $dir does not exist!" exit 1 fi find $dir -type d | grep -v '\.HTML' | while read 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" >> $rc_file - echo "DIST_DIR=$d.HTML" >> $rc_file - echo "ORIGINAL_BASEPATH=../../$(basename $d)" >> $rc_file - echo 'FIND_ARGS="-maxdepth 1"' >> $rc_file - photoalbum generate $rc_file + 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" >> "$rc_file" + echo "DIST_DIR=$d.HTML" >> "$rc_file" + echo "ORIGINAL_BASEPATH=../../$(basename $d)" >> "$rc_file" + echo 'FIND_ARGS="-maxdepth 1"' >> "$rc_file" + photoalbum generate "$rc_file" cd - &>/dev/null - test -d $d.HTML && test ! -d $d.HTML/thumbs && rm -Rf $d.HTML + test -d "$d.HTML" && test ! -d "$d.HTML/thumbs" && rm -Rf "$d.HTML" done } |
