1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 16:09:03 -08:00
rc/sh/decently

43 lines
1.6 KiB
Text
Raw Normal View History

2021-10-01 08:24:30 -07:00
#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
# TODO: check if this is compatible with busybox.
decently() ( ### @-
2021-10-01 08:24:30 -07:00
### given a list of directories, update the last-modified timestamp
### of each argument to that of the most recent file that it contains.
###
### note that *files* are found recursively, but only the
### *outermost directory* (the one specified by argument)
### has its timestamp updated. symlinks are followed.
### `.git` subdirectories are skipped over.
### the timestamps of subdirectories are ignored.
2021-10-01 08:24:30 -07:00
local arg= dir= latest= prev= ts= fails= succs=0
finder() { find "$dir" -type f "$@" '!' -path '*/.git/*' -print -quit; }
2021-10-01 08:24:30 -07:00
[ -n "$1" ] || { printf '%s: missing directory argument\n' decently >&2; return 1; }
for arg; do
dir="$(readlink -f "$arg")" || continue
[ -d "$dir" ] || { printf '%s: not a directory: %s\n' decently "$dir" >&2; continue; }
prev=
latest="$(finder)" || continue
2021-10-01 08:24:30 -07:00
while [ -n "$latest" ]; do
prev="$latest"
latest="$(finder -newer "$latest")" || break
2021-10-01 08:24:30 -07:00
done
[ -e "$prev" ] || { printf '%s: no valid files found: %s\n' decently "$dir" >&2; continue; }
ts="$(date -u '+%s' -r "$prev")"
dir="${dir%/}/" # ensure dir has a trailing slash
printf '%s\t%s\n' "$ts" "${arg%/}/${prev#$dir}"
touch -c -r "$prev" "$dir" || continue
: $(( succs+=1 ))
done
fails=$(( $# - succs ))
[ $fails -le 255 ] || fails=255
return $fails
)
2021-10-01 08:24:30 -07:00
[ -n "${preload+-}" ] || decently "$@"