1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 00:19:02 -08:00
rc/regenerate

81 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env dash
NAME="$0"
note() {
local IFS=" "
printf "%s\n" "$*"
}
warn() {
note "$@" >&2
}
die() {
warn "$NAME:" "$@"
#cleanup
exit 1
}
cleanup() {
:
#[ ! -e temp.tar.gz ] || rm temp.tar.gz || die failed
#[ ! -d temp ] || rm -r temp || die failed
}
backup() {
if [ -e "${1}~" ]; then
backup "${1}~" || die 'failed to backup file' "$1"
fi
mv "$1" "${1}~" || die 'failed to backup file' "$1"
}
rc="$(readlink -f "$(dirname "$NAME")" )"
[ -d "$rc" ] || die 'failed to determine rc directory'
cd "$rc" || die 'failed to change directory'
if ! which git >/dev/null 2>&1; then
[ "$1" != commit ] || die 'git not found'
# git unavailable. just document everything as it is.
dash ./sh/document || die 'failed to generate documentation'
printf '%s\n' '' '## compatibility' '' >> README.md~ || die 'failed to make room for compatibility table'
dash ./tableize >> README.md~ || die 'failed to generate compatibility table'
elif [ "$1" = local ]; then
# user requested to ignore git.
dash ./sh/document || die 'failed to generate documentation'
printf '%s\n' '' '## compatibility' '' >> README.md~ || die 'failed to make room for compatibility table'
dash ./tableize >> README.md~ || die 'failed to generate compatibility table'
else
# git available. document the most recent commit, without pending changes.
[ ! -e temp.tar.gz ] || backup temp.tar.gz || die 'failed to backup existing temp archive'
git archive --prefix=temp/ HEAD -o temp.tar.gz || die 'failed to create git archive'
[ ! -e temp ] || backup temp || die 'failed to backup existing temp directory'
tar -zxf temp.tar.gz || die 'failed to unarchive temp archive'
#( cd temp; HOME="$rc/temp" dash ./install )
ln temp/home/zshrc temp/.zshrc || die 'failed to copy files'
ln temp/home/-shrc temp/.-shrc || die 'failed to copy files'
ln temp/home/bashrc temp/.bashrc || die 'failed to copy files'
# use new documentation-generating scripts on old shell scripts.
cp -p README.md temp/README.md || die 'failed to copy existing readme'
dash ./sh/document temp || die 'failed to generate documentation'
printf '%s\n' '' '## compatibility' '' >> temp/README.md~ || die 'failed to make room for compatibility table'
dash ./tableize temp >> temp/README.md~ || die 'failed to generate compatibility table'
#[ ! -e README.md ] || backup README.md || die 'failed to backup existing readme'
mv temp/README.md~ README.md~ || die 'failed to move generated readme'
rm temp.tar.gz || die 'failed to clean up temp archive'
rm -r temp || die 'failed to clean up temp directory'
if [ "$1" = commit ]; then
mv README.md~ README.md || die 'failed to overwrite readme'
git commit README.md -m 'regenerate readme' || die 'failed to commit'
fi
fi