-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
137 lines (102 loc) · 3.54 KB
/
Copy pathinstall.sh
File metadata and controls
137 lines (102 loc) · 3.54 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Set any necessary shell options since we may not be in a set-up environment
shopt -s globstar
# Execute relative to this script, no matter where it was run from.
# Needed for symlink/readpath logic.
pushd $(dirname "$BASH_SOURCE") || exit
export CYGWIN="winsymlinks:nativestrict"
lnif() {
# link-if: create a fullpath symlink to $1 at ~/($2 or $1)
# abort script on error
if [[ ! ( -f "$1" || -d "$1" ) ]]; then
echo "Missing: $1"
exit
fi
dotfile="$1" # $(basename ${1})
dest=~/${2:-$dotfile}
if [[ -L "${dest}" ]]; then
rm "${dest}"
elif [[ -f "${dest}" || -d "${dest}" ]]; then
mv -v "${dest}" "${dest}.$(date -Iminutes).bak"
fi
ln -s "$(realpath -e "${dotfile}" || readlink -e "${dotfile}")" "${dest}" && ls -ao "${dest}"
}
generate_lazyload() {
# Generate lazyload.sh with wrapper functions for optional modules
# Each wrapper sources the module on first use and calls the real function
# echo -n "Generating support/_lazyload.sh... "
local lazyload_file="support/_lazyload.sh"
# Start with header
cat > "$lazyload_file" << 'HEADER'
#!/bin/bash
# Lazy-loading wrappers for support/lazy/* generated by install.sh
HEADER
# For each module, extract function names and create wrappers
for module in support/lazy/*.sh; do
if [[ ! -f "$module" ]]; then
echo "Warning: $module not found, skipping"
continue
fi
# echo -n "$module "
echo "" >> "$lazyload_file"
echo "# $module" >> "$lazyload_file"
# Extract function names (matches "function_name()" and "function function_name")
local functions=$(grep -oP '^[[:space:]]*(function[[:space:]]+)?[a-zA-Z_][a-zA-Z0-9_]*(?=\s*\(\))' "$module" | sed 's/^[[:space:]]*function[[:space:]]*//' | sort -u)
if [[ -z "$functions" ]]; then
echo " No functions found in $module"
continue
fi
# Create wrapper for each function
while IFS= read -r func; do
# Skip if function is empty or contains only whitespace
[[ -z "${func// /}" ]] && continue
# Skip if function is marked as internal
[[ "${func}" == _* ]] && continue
cat >> "$lazyload_file" << EOF
${func}() { unset -f ${func}; source ~/dotfiles/${module}; ${func} "\$@"; }
EOF
done <<< "$functions"
# Extract alias definitions from shell file $module
local aliases=$(grep "^alias " "$module")
# Create wrapper for each function
while IFS= read -r alias; do
echo "$alias" >> "$lazyload_file"
done <<< "$aliases"
done
chmod +x $lazyload_file
echo "Generated $lazyload_file"
}
# Prepare files in dotfiles/
chmod +x scripts/* githooks/* &
generate_lazyload &
# Link files
for dotfile in home/.?*[^~]; do
lnif "$dotfile" "$(basename "$dotfile")" &
done
# TODO: Windows-only workaround
# TODO: cygwin symlink option should fix this?
# rm ~/.gitconfig; cp -v home/.gitconfig ~/.gitconfig
# rm ~/.gitignore; cp -v home/.gitignore ~/.gitignore
for dotfile in config/**/*.*; do
[ -f "$dotfile" ] && lnif "$dotfile" ".$dotfile" &
done
for dotdir in githooks; do
lnif "$dotdir" ".$dotdir" &
done
mkdir -p ~/.emacs.d/
for elisp in emacs.d/*.el emacs.d/vend-lisp emacs.d/site-lisp; do
# mkdir -p ~/."$(dirname "$elisp")"
lnif "$elisp" ".$elisp"
done
# Tweak filesystem
mkdir -p ~/.vim/backup ~/.vim/swap ~/.vim/undo &
if [[ -d ~/.history ]]; then
chmod 0700 ~/.history &
else
mkdir --mode=0700 -p "$HOME/.history" &
fi
wait
# Cleanup and migrations
[[ ! -f home/.profile ]] && [[ -f ~/.profile ]] && rm -v ~/.profile
[[ ! -f home/.bash_profile ]] && [[ -f ~/.bash_profile ]] && rm -v ~/.bash_profile
popd;