Rely on make(1)

This commit is contained in:
Xavier Del Campo Romero 2023-06-19 15:23:09 +02:00
parent 9cc9d80008
commit 2da827c9f8
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
2 changed files with 136 additions and 78 deletions

95
tngen
View File

@ -4,15 +4,15 @@ set -e
usage() usage()
{ {
echo "$0 [-s <size>] [-h] -d <dir> <path>" echo "$0 [-s <size>] [-j <jobs>] [-h] <path>"
} }
SIZE=96 SIZE=96
while getopts s:d:h arg while getopts j:s:h arg
do do
case $arg in case $arg in
d) DIR="$OPTARG" j) JOBS="$OPTARG"
;; ;;
s) SIZE="$OPTARG" s) SIZE="$OPTARG"
;; ;;
@ -25,12 +25,6 @@ do
esac esac
done done
if [ -z "$DIR" ]
then
usage >&2
exit 1
fi
shift $(($OPTIND - 1)) shift $(($OPTIND - 1))
if [ $# != 1 ]; then if [ $# != 1 ]; then
@ -38,7 +32,7 @@ if [ $# != 1 ]; then
exit 1 exit 1
fi fi
F="$1" DIR="$1"
gen() gen()
{ {
@ -61,16 +55,81 @@ gen()
fi fi
} }
while read f; do scan()
if [ "$f" != "" ] {
while read d
do
if [ -n "$d" ]
then then
THUMBNAIL=$(echo "$f" | sed "s,$DIR/user/,$DIR/thumbnails/,") scan "$d"
gen "$f" "$THUMBNAIL"
fi fi
done <<-EOF
$(find "$F" -type f \ TDIR="$(echo "$1" | sed "s,$DIR/user,$DIR/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' \ -a -iname '*.jpeg' \
-o -iname '*.jpg' \ -o -iname '*.jpg' \
-o -iname '*.png' \ -o -iname '*.png' \
-o -iname '*.jpeg') -o -iname '*.jpeg' > "$TDIR/.files"
EOF
while read f
do
THUMBNAIL="$(echo "$f" | sed "s,$DIR/user/,$DIR/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,$DIR/user/,$DIR/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"
done <<-EOF
$(eval find \"$1\"/* -prune -type d || return 0)
EOF
}
echo Generating Makefiles...
scan "$DIR/user"
cd "$DIR/thumbnails" && make ${JOBS:+-j$JOBS}

View File

@ -2,15 +2,17 @@
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
j) JOBS="$OPTARG"
;;
r) REGEN=1 r) REGEN=1
;; ;;
s) SIZE="$OPTARG" s) SIZE="$OPTARG"
@ -18,8 +20,6 @@ do
h) usage h) usage
exit 0 exit 0
;; ;;
d) SUBDIR="$OPTARG"
;;
?) usage >&2 ?) usage >&2
exit 1 exit 1
;; ;;
@ -37,16 +37,15 @@ 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" "$DIR"
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} \
-d "$DIR" \ ${JOBS:+-j$JOBS} \
"$F" "$DIR"
done done