forked from scopatz/nanorc
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinstall.sh
executable file
·97 lines (86 loc) · 2.12 KB
/
install.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
#!/bin/bash
set -e
# check for unzip before we continue
if [ ! "$(command -v unzip)" ]; then
echo 'unzip is required but was not found. Install unzip first and then run this script again.' >&2
exit 1
fi
_fetch_sources() {
br=$(_find_suitable_branch)
mkdir -p ~/.nano/
cd ~/.nano/
wget -O "/tmp/nanorc.zip" "https://github.com/galenguyer/nano-syntax-highlighting/archive/${br}.zip"
unzip -o "/tmp/nanorc.zip"
mv "nano-syntax-highlighting-${br}"/* ./
rm -rf "nano-syntax-highlighting-${br}"
rm -f "/tmp/nanorc.zip"
}
_update_nanorc() {
touch $NANORC_FILE
# add all includes from ~/.nano/nanorc if they're not already there
while read -r inc; do
if ! grep -q "$inc" "${NANORC_FILE}"; then
echo "$inc" >> "$NANORC_FILE"
fi
done < ~/.nano/nanorc
}
_update_nanorc_lite() {
sed -i '/include "\/usr\/share\/nano\/\*\.nanorc"/i include "~\/.nano\/*.nanorc"' "${NANORC_FILE}"
}
_version_str_to_num() {
if [ -z "$1" ]; then
return
fi
echo -n "$1" | awk -F . '{printf("%d%02d%02d", $1, $2, $3)}'
}
_find_suitable_branch() {
# find the branch that is suitable for local nano
verstr=$(nano --version 2>/dev/null | awk '/GNU nano/ {print ($3=="version")? $4: substr($5,2)}')
ver=$(_version_str_to_num "$verstr")
if [ -z "$ver" ]; then
echo "Cannot determine nano's version, fallback to master" >&2
echo "master"
return
fi
branches=(
pre-6.0
pre-5.0
pre-4.5
pre-2.9.5
pre-2.6.0
pre-2.3.3
pre-2.1.6
)
target="master"
# find smallest branch that is larger than ver
for b in "${branches[@]}"; do
num=$(_version_str_to_num "${b#*pre-}")
if (( ver < num )); then
target="${b}"
else
break
fi
done
echo "$target"
}
NANORC_FILE=~/.nanorc
case "$1" in
-l|--lite)
UPDATE_LITE=1
;;
--find_suitable_branch)
_find_suitable_branch
exit 0
;;
-h|--help)
echo "Install script for nanorc syntax highlights"
echo "Call with -l or --lite to update .nanorc with secondary precedence to existing .nanorc includes"
exit 0
;;
esac
_fetch_sources
if [ "$UPDATE_LITE" ]; then
_update_nanorc_lite
else
_update_nanorc
fi