Rely on make(1)

This commit is contained in:
Xavier Del Campo Romero 2023-06-19 15:23:09 +02:00
parent 611c8d82b6
commit dd4e2883a8
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
3 changed files with 173 additions and 76 deletions

89
mkgen Executable file
View File

@ -0,0 +1,89 @@
#! /bin/sh
usage()
{
echo "$0 [-s <size>] [-j <jobs>] [-h] -d <root> <path>"
}
SIZE=96
while getopts d:s:h arg
do
case $arg in
d) ROOT="$OPTARG"
;;
s) SIZE="$OPTARG"
;;
h) usage
exit 0
;;
?) usage >&2
exit 1
;;
esac
done
if [ -z "$ROOT" ]; then
usage >&2
exit 1
fi
shift $(($OPTIND - 1))
if [ $# != 1 ]; then
usage >&2
exit 1
fi
TDIR="$(echo "$1" | sed "s,$ROOT/user,$ROOT/thumbnails,")"
eval find \"$1\"/* -prune >/dev/null || return 0
mkdir -p "$TDIR"
MK="$(echo "$TDIR/Makefile")"
cat <<-"EOF" > "$MK"
.POSIX:
EOF
echo "DIRS=\\" >> "$MK"
eval find \"$TDIR\"/* -prune -type d > "$TDIR/.dirs"
while read dir
do
D="$(echo "$dir" | sed "s, ,\\\\ ,g")"
echo "$D \\" >> "$MK"
done < "$TDIR/.dirs"
echo >> "$MK"
echo "DEPS=\\" >> "$MK"
eval find \"$1\"/* -prune -type f \
-a -iname '*.jpeg' \
-o -iname '*.jpg' \
-o -iname '*.png' \
-o -iname '*.jpeg' > "$TDIR/.files"
while read f
do
THUMBNAIL="$(echo "$f" | sed "s,$ROOT/user/,$ROOT/thumbnails/," \
| sed "s, ,\\\\ ,g")"
echo "$THUMBNAIL \\" >> "$MK"
done < "$TDIR/.files"
echo >> "$MK"
echo 'all: $(DEPS) $(DIRS)' >> "$MK"
echo 'FORCE:' >> "$MK"
while read dir
do
D="$(echo "$dir" | sed "s, ,\\\\ ,g")"
echo "$D: FORCE" >> "$MK"
printf '\t+cd $@ && $(MAKE)\n' >> "$MK"
done < "$TDIR/.dirs"
while read f
do
THUMBNAIL="$(echo "$f" | sed "s,$ROOT/user/,$ROOT/thumbnails/," \
| sed "s, ,\\\\ ,g")"
EF=$(echo "$f" | sed "s, ,\\\\ ,g")
echo $THUMBNAIL: $EF >> "$MK"
printf '\tconvert -thumbnail x%d "$<" "$@" || :\n' $SIZE >> "$MK"
done < "$TDIR/.files"
echo "Successfully generated $MK"

103
tngen
View File

@ -4,73 +4,80 @@ set -e
usage() usage()
{ {
echo "$0 [-s <size>] [-h] -d <dir> <path>" echo "$0 [-s <size>] [-j <jobs>] [-h] -d <root> <path>"
} }
SIZE=96 SIZE=96
while getopts s:d:h arg while getopts d:j:s:h arg
do do
case $arg in case $arg in
d) DIR="$OPTARG" d) ROOT="$OPTARG"
;; ;;
s) SIZE="$OPTARG" j) JOBS="$OPTARG"
;; ;;
h) usage s) SIZE="$OPTARG"
exit 0 ;;
;; h) usage
?) usage >&2 exit 0
exit 1 ;;
;; ?) usage >&2
esac exit 1
;;
esac
done done
if [ -z "$DIR" ] if [ -z "$ROOT" ]; then
then usage >&2
usage >&2 exit 1
exit 1
fi fi
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
if [ $# != 1 ]; then if [ $# != 1 ]; then
usage >&2 usage >&2
exit 1 exit 1
fi fi
F="$1" DIR="$1"
gen() gen()
{ {
IN="$1" IN="$1"
OUT="$2" OUT="$2"
if [ -z "$OUT" ] if [ -z "$OUT" ]
then then
echo Expected output filename >&2 echo Expected output filename >&2
return 1 return 1
fi fi
mkdir -p "$(dirname "$OUT")" mkdir -p "$(dirname "$OUT")"
if convert -thumbnail x$SIZE "$IN" "$OUT" if convert -thumbnail x$SIZE "$IN" "$OUT"
then then
echo Created $OUT echo Created $OUT
else else
echo Failed to create $OUT >&2 echo Failed to create $OUT >&2
fi fi
} }
while read f; do scan()
if [ "$f" != "" ] {
then while read d
THUMBNAIL=$(echo "$f" | sed "s,$DIR/user/,$DIR/thumbnails/,") do
gen "$f" "$THUMBNAIL" if [ -n "$d" ]
fi then
done <<-EOF scan "$d"
$(find "$F" -type f \ fi
-a -iname '*.jpeg' \
-o -iname '*.jpg' \ "$(dirname "$0")"/mkgen -d "$ROOT" "$1"
-o -iname '*.png' \ done <<-EOF
-o -iname '*.jpeg') $(eval find \"$1\"/* -prune -type d || return 0)
EOF EOF
}
echo Generating Makefiles...
scan "$DIR"
cd "$ROOT/thumbnails" && make ${JOBS:+-j$JOBS}

View File

@ -2,51 +2,52 @@
usage() usage()
{ {
echo "$0 [-s <size>] [-r] [-h] [-d <subdir>] <dir>" echo "$0 [-s <size>] [-r] [-h] [-j <jobs>] <dir>"
} }
REGEN=0 REGEN=0
SIZE=96 SIZE=96
while getopts rs:d:h arg while getopts rj:s:h arg
do do
case $arg in case $arg in
r) REGEN=1 j) JOBS="$OPTARG"
;; ;;
s) SIZE="$OPTARG" r) REGEN=1
;; ;;
h) usage s) SIZE="$OPTARG"
exit 0 ;;
;; h) usage
d) SUBDIR="$OPTARG" exit 0
;; ;;
?) usage >&2 ?) usage >&2
exit 1 exit 1
;; ;;
esac esac
done done
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
if [ $# != 1 ]; then if [ $# != 1 ]; then
usage >&2 usage >&2
exit 1 exit 1
fi fi
DIR="$1" DIR="$1"
[ "$REGEN" -eq 1 ] && "$(dirname $0)/tngen" \ [ "$REGEN" -eq 1 ] && "$(dirname $0)/tngen" \
${SIZE:+-s$SIZE} \ ${SIZE:+-s$SIZE} \
-d "$DIR" \ ${JOBS:+-j$JOBS} \
"$DIR/user" -d "$DIR" \
"$DIR/user"
while : while :
do do
F="$(inotifywait -e modify,move,create,delete \ F="$(inotifywait -e modify,move,create,delete \
--format "%w%f" -qr "$DIR/user/")" --format "%w%f" -qr "$DIR/user/")"
sleep 1 # TODO: revisit this "$(dirname $0)/tngen" \
"$(dirname $0)/tngen" \ ${SIZE:+-s$SIZE} \
${SIZE:+-s$SIZE} \ ${JOBS:+-j$JOBS} \
-d "$DIR" \ -d "$DIR" \
"$F" "$(dirname "$F")"
done done