-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·169 lines (148 loc) · 3.97 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
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
#!/bin/sh
info=$(cat << EOF
install.sh install-data|install-script|uninstall-data|uninstall-script
EOF
)
local_home="${XDG_LOCAL_HOME:-$HOME/.local}"
data_home="${XDG_DATA_HOME:-$local_home/share}"
bin_home="${XDG_BIN_HOME:-$local_home/bin}"
btcs_install_data() {
project_home="$(dirname "$0")"
if [ ! -d "$data_home" ]; then
mkdir -p "$data_home"
fi
btcs_home="$data_home/btcs"
if [ ! -d "$btcs_home" ]; then
mkdir -p "$btcs_home"
fi
if ! cp -f "$project_home/btcs" "$btcs_home"; then
echo "error: installing btcs script" >&2
return 1
fi
btcs_stf_home="$btcs_home/stf"
if [ -d "$btcs_stf_home" ]; then
rm -rf "$btcs_stf_home"
fi
if [ ! -d "$btcs_stf_home" ]; then
mkdir -p "$btcs_stf_home"
fi
if ! cp -f "$project_home/stf/wordlist" "$btcs_stf_home"; then
echo "error: installing wordlist" >&2
return 1
fi
btcs_script_home="$btcs_home/scripts"
if [ -d "$btcs_script_home" ]; then
rm -rf "$btcs_script_home"
fi
if [ ! -d "$btcs_script_home" ]; then
mkdir -p "$btcs_script_home"
fi
if
! find "$project_home/scripts" \
-type f \
-exec cp -f "{}" "$btcs_script_home" \; >/dev/null
then
echo "error: installing scripts" >&2
return 1
fi
echo "btcs data successfully installed"
}
btcs_install_script() {
if [ -z "$BTC_BIN" ]; then
echo "error: set BTC_BIN" >&2
return 1
fi
# shellcheck disable=SC2086
if ! command -v $BTC_BIN >/dev/null 2>&1; then
echo "error: $BTC_BIN is not a command" >&2
return 1
fi
if [ -z "$BTCS_NAME" ]; then
echo "error: set BTCS_NAME" >&2
return 1
fi
if printf "%s\n" "$BTCS_NAME" | grep "[[:space:]]" >/dev/null 2>&1; then
echo "error: $BTCS_NAME should not contain spaces" >&2
return 1
fi
if [ ! -d "$bin_home" ]; then
mkdir -p "$bin_home"
fi
btcs_install_file="$bin_home/$BTCS_NAME"
if ! touch "$btcs_install_file"; then
echo "error: creating $btcs_install_file" >&2
return 1
fi
if ! chmod u+x "$btcs_install_file"; then
echo "error: setting permission on $btcs_install_file" >&2
return 1
fi
btcs_install_file_content=$(cat << EOF
#!/bin/sh
export BTC_BIN="$BTC_BIN"
"\${XDG_DATA_HOME:-\${XDG_LOCAL_HOME:-\$HOME/.local}/share}/btcs/btcs" "\$@"
EOF
)
printf "%s\n" "$btcs_install_file_content" > "$btcs_install_file"
echo "btcs script $BTCS_NAME successfully installed"
}
btcs_uninstall_data() {
btcs_home="$data_home/btcs"
if [ -e "$btcs_home" ]; then
if ! rm -rf "$btcs_home"; then
echo "error: removing $btcs_home" >&2
return 1
fi
echo "btcs data successfully uninstalled"
else
echo "btcs data was not installed"
fi
}
btcs_uninstall_script() {
if [ -z "$BTCS_NAME" ]; then
echo "error: set BTCS_NAME" >&2
return 1
fi
if printf "%s\n" "$BTCS_NAME" | grep "[[:space:]]" >/dev/null 2>&1; then
echo "error: $BTCS_NAME should not contain spaces" >&2
return 1
fi
btcs_install_file="$bin_home/$BTCS_NAME"
if [ -f "$btcs_install_file" ]; then
if ! rm -f "$btcs_install_file"; then
echo "error: removing $btcs_install_file" >&2
return 1
fi
echo "$BTCS_NAME successfully uninstalled"
else
echo "$BTCS_NAME was not installed"
fi
}
if [ "$#" -lt 1 ]; then
echo "error: insert action" >&2
exit 1
fi
action="$1"
shift 1
case "$action" in
-h|--help)
echo "$info"
exit 0
;;
install-data)
btcs_install_data "$@"
;;
install-script)
btcs_install_script "$@"
;;
uninstall-data)
btcs_uninstall_data "$@"
;;
uninstall-script)
btcs_uninstall_script "$@"
;;
*)
echo "error: action $action not recognized" >&2
exit 1
;;
esac