-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermit.sh
executable file
·174 lines (143 loc) · 2.94 KB
/
hermit.sh
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh
DIR=~/.hermit
###
## Setup
#
# for now, clone git@github.com:Tangent128/hermit.git into ~/.hermit/
#mkdir -p $DIR
###
## Context Variables & Constants
#
PATCH_FILE=
MODE=
# set by each checkShell invocation:
SHELL_NAME=
RC_FILE=
RC_BASE=
# set by each forPatches iteration:
PATCH=
PATCH_TYPE=
PATCH_NAME=
###
## Action functions
#
metadata() {
awk -f $DIR/hermitUnpack.awk mode=metadata $PATCH_FILE
}
readPatch() {
# readPatch index
awk -f $DIR/hermitUnpack.awk mode=patch patchNum=$1 $PATCH_FILE
}
checkShell() {
# checkShell shellName
RC_FILE=
SHELL_NAME=$1
case $SHELL_NAME in
sh)
if test "$ENV"; then
RC_FILE="$ENV"
fi
;;
zsh)
RC_FILE=~/.zshrc
;;
bash)
RC_FILE=~/.bashrc
;;
esac
if test -f "$RC_FILE"; then
RC_BASE=$DIR/$(basename $RC_FILE)
return 0
else
return 1
fi
}
forShells() {
# forShells shellName command args...
# used to apply changes to compatable "in-use" shells,
# will invoke: command specificShellName args...
compatShell=$1
theCommand=$2
shift 2
if [ $compatShell = "sh" ]; then
checkShell sh && $theCommand $*
checkShell zsh && $theCommand $*
checkShell bash && $theCommand $*
return
fi
checkShell $compatShell && $theCommand $*
}
# make backup & working copies of a shell's RC file
doBackup() {
if [ ! "$RC_FILE" -ot "$RC_BASE.old" ]; then
# copy if needed
cp "$RC_FILE" "$RC_BASE.old"
cp "$RC_FILE" "$RC_BASE.new"
fi
}
# install the patched versions of a shell's RC file
doInstall() {
if [ "$RC_FILE" -ot "$RC_BASE.new" ]; then
# copy if needed
cp "$RC_BASE.new" "$RC_FILE"
fi
}
forShellFiles() {
# metadata | forShellFiles command args
while read type shell etc; do
if [ "$type" = "shell" ]; then
forShells $shell $1 $*
fi
done
}
forPatches() {
# metadata | forPatches command args
theCommand=$1
shift 1
while read type shell index patchtype name etc; do
if [ "$type" = "patch" ]; then
PATCH=$(readPatch $index)
PATCH_TYPE=$patchtype
PATCH_NAME=$name
forShells $shell $theCommand $*
fi
done
}
# WIP
patchFile() {
# patchFile filebase
removing="0"
test "$MODE" = "uninstallPatch" && removing="1"
test $1.new && mv $1.new $1.working
awk -f $DIR/hermitManage.awk "patchType=$PATCH_TYPE" "patchName=$PATCH_NAME" \
"patchText=$PATCH" "removePatch=$removing" $1.working > $1.new
#echo patched $1
}
###
## Main logic
#
usage() {
echo "Usage: install patch: $0 -S patchName"
echo " uninstall patch: $0 -D patchName"
}
usageErr() {
echo $*
usage
exit 1
}
# determine mode
FLAG=$1
case $FLAG in
-S*) MODE=installPatch ;;
-D*) MODE=uninstallPatch ;;
esac
case $FLAG in *y*) : tbd ; ;; esac
test "$MODE" || usageErr No mode given.
# identify patch file
test "$2" || usageErr No patch name given.
PATCH_FILE=$DIR/patches/$2
test -f $PATCH_FILE || usageErr Could not find specified patch.
# apply actions
metadata | forShellFiles doBackup
metadata | forPatches eval patchFile \$RC_BASE
metadata | forShellFiles doInstall