-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.sh
executable file
·196 lines (176 loc) · 6.13 KB
/
menu.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
function menu() {
local name="$1"
local patch_file="$2"
local directory="$3"
# Check if the patch has already been applied
if git apply --reverse --check "${mytmpdir}/${patch_file}.patch" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} *** Patch is applied ***"
else
# Try to apply the patch
if git apply --check "${mytmpdir}/${patch_file}.patch" --directory="${directory}" >/dev/null 2>&1; then
echo "${name}" # - Patch can be applied
else
echo "${name} !!! Patch can't be applied !!!"
fi
fi
}
function apply() {
local name="$1"
local patch_file="${mytmpdir}/$2.patch"
local directory="$3"
echo
# Check if the patch has already been applied
if git apply --reverse --check "${patch_file}" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} *** Patch is applied ***"
else
# Try to apply the patch
if git apply --check "${patch_file}" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} - Applying..."
git apply "${patch_file}" --directory="${directory}"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Successful!"
else
echo "Failed!"
fi
else
echo "${name} !!! Patch can't be applied !!!"
fi
fi
}
function revertmenu() {
local name="$1"
local patch_file="$2"
local directory="$3"
# Check if the patch has already been applied
if git apply --reverse --check "${mytmpdir}/${patch_file}.patch" --directory="${directory}" >/dev/null 2>&1; then
echo "${name}" # - Patch can be reverted
else
# Try to apply the patch
if git apply --check "${mytmpdir}/${patch_file}.patch" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} *** Patch is not applied ***"
else
echo "${name} !!! Reverting or applying the patch is not possible !!!"
fi
fi
}
function revert() {
local name="$1"
local patch_file="${mytmpdir}/$2.patch"
local directory="$3"
echo
if git apply --reverse --check "${patch_file}" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} - Reversing..."
git apply --reverse "${patch_file}" --directory="${directory}"
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Successful!"
else
echo "Failed!"
fi
else
if git apply --check "${mytmpdir}/${patch_file}.patch" --directory="${directory}" >/dev/null 2>&1; then
echo "${name} *** Patch is not applied ***"
else
echo "${name} !!! Reverting or applying the patch is not possible !!!"
fi
fi
}
function add_patch() {
name+=("$1")
file+=("$2")
folder+=("$3")
}
# Deletes the temp directory
function cleanup {
echo "Deleting temp working directory $mytmpdir"
rm -r "$mytmpdir"
tput cuu1 && tput el
}
if [ "$(basename "$PWD")" != "LoopWorkspace" ]; then
target_dir="$(find /Users/$USER/Downloads/BuildLoop -maxdepth 2 -type d -name LoopWorkspace -exec dirname {} \; -exec stat -f "%B %N" {} \; | sort -rn | awk '{print $2}' | head -n 1)"
if [ -z "$target_dir" ]; then
echo "Error: No folder containing LoopWorkspace found."
else
echo "Navigating to $target_dir"
cd "$target_dir"
fi
fi
echo "Loop patch selection"
# Verify current folder
if [ $(basename $PWD) = "LoopWorkspace" ]; then
echo "Creating temporary folder"
workingdir=$PWD
mytmpdir=$(mktemp -d)
# Check if tmp dir was created
if [[ ! "$mytmpdir" || ! -d "$mytmpdir" ]]; then
echo "Could not create temporary folder"
exit 1
fi
tput cuu1 && tput el
# Register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
#DEBUG
#echo $mytmpdir
#Declare all patches
#Using simple arrays since bash 3 which is shipped with mac does not support associative arrays
name=()
file=()
folder=()
add_patch "Manual bolus threshold" "manualBolusThreshold" "Loop"
add_patch "View PreMeal in Nightscout" "preMeal" "Loop"
add_patch "Future carbs 90 minutes" "90m" "Loop"
add_patch "Required carbs" "updateRequiredCarbs" ""
add_patch "Dash Fast Forward" "fast_forward" ""
add_patch "Basic I:C Bolus Calculation" "basicICBolusCalc" ""
add_patch "2 hours Lolipop" "2hlollipop" ""
add_patch "NS Override" "lost_overrides_in_ns" ""
echo "Downloading patches, please wait..."
cd $mytmpdir
for i in ${!name[@]}
do
curl -fsSLOJ "https://raw.githubusercontent.com/bjorkert/patches/master/${file[$i]}.patch"
done
tput cuu1 && tput el
cd $workingdir
while true; do
echo
echo "Select a patch you want to apply:"
echo "a) Apply all patches"
for i in ${!name[@]}
do
menu "${i}) ${name[$i]}" "${file[$i]}" "${folder[$i]}";
done
echo "r) Revert a patch"
echo "q) Quit"
read -p "Enter your choice: " choice
if [[ $choice =~ ^[0-9]+$ && $choice -ge 0 && $choice -lt ${#name[@]} ]]; then
apply "${name[$choice]}" "${file[$choice]}" "${folder[$choice]}";
elif [[ $choice == "r" ]]; then
echo
echo "Select a patch to revert:"
for i in ${!name[@]}
do
revertmenu "${i}) ${name[$i]}" "${file[$i]}" "${folder[$i]}";
done
echo "*) Any other key will exit to last menu"
read -p "Enter your choice: " choice
if [[ $choice =~ ^[0-9]+$ && $choice -ge 0 && $choice -lt ${#name[@]} ]]; then
revert "${name[$choice]}" "${file[$choice]}" "${folder[$choice]}";
fi
elif [[ $choice == "a" ]]; then
for i in ${!name[@]}
do
apply "${name[$i]}" "${file[$i]}" "${folder[$i]}";
done
elif [[ $choice == "q" ]]; then
exit 0
else
echo
echo "Invalid choice. Try again."
fi
done
else
exit 1
fi