-
Notifications
You must be signed in to change notification settings - Fork 1
/
homelander.bash
executable file
·52 lines (46 loc) · 1.16 KB
/
homelander.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash
# shellcheck source=./_core.bash
. "$(dirname "${0}")/_core.bash"
if [[ "${1:-}" == '--clean' ]]; then
clean=true
shift
fi
dotfile="${1:-}"
target="${2:-${HOME}/${dotfile}}"
plugin_dir="${script_dir}/homelander/${dotfile}"
__usage() {
plain <<-DOC
${cyan}./homelander.bash${reset} [--clean] <dotfile> [target]
${yellow}--clean${reset} removes all previous entries from the target
DOC
exit 1
}
if [[ -z "$dotfile" || ! -d "$plugin_dir" ]]; then
fail 'invalid <dotfile>'
__usage
fi
if [[ -z "${target}" ]]; then
__usage
fi
info "homelander starting"
if [[ "${clean:-}" == "true" ]]; then
sed -i '/^#[+]:[^:]\+:$/,/^#[-]:[^:]\+:$/d' "$target"
fi
find "${plugin_dir}" -type f -name "*.bash" -print0 \
| sort -z \
| while IFS= read -r -d '' file; do
# skip if there are no files
[[ -f "${file}" ]] || continue
# remove the last .bash from the file name
label="$(basename "${file%.bash*}")"
# remove section ( eg. #[+-]:somefile: )
touch "${target}"
# add the section back
{
echo "#+:${label}:"
# remove blank lines and comments
sed -e '/^$/d' -e '/^\s*#/d' "${file}"
echo "#-:${label}:"
} >> "${target}"
done
info "homelander finished"