This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
napi.sh.in
293 lines (245 loc) · 8.87 KB
/
napi.sh.in
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
# force indendation settings
# vim: ts=4 shiftwidth=4 expandtab
########################################################################
########################################################################
########################################################################
# Copyright (C) 2017 Tomasz Wisniewski aka
# DAGON <[email protected]>
#
# http://github.com/dagon666
# http://pcarduino.blogspot.co.uk
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
########################################################################
########################################################################
########################################################################
export G_NAPIPREFIX="@NAPI_INSTALL_PREFIX@"
if [ -z "$G_NAPIPREFIX" ] ||
[ ! -d "${G_NAPIPREFIX}" ] ||
[ ! -d "${G_NAPIPREFIX}/lib/napi" ]; then
echo $"Skrypt nie zostal poprawnie zainstalowany!"
exit -1
fi
########################################################################
# libraries
. "${G_NAPIPREFIX}/lib/napi/libnapi_argv.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_assoc.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_constants.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_fs.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_hooks.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_http.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_language.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_logging.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_napiprojekt.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_retvals.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_subotage.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_subs.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_sysconf.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_tools.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_version.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_wrappers.sh"
. "${G_NAPIPREFIX}/lib/napi/libnapi_xml.sh"
########################################################################
# globals
declare -a g_unparsedArgv=()
########################################################################
#
# @brief prints the help & options overview
#
_usage() {
echo $"napi.sh <AKCJA|--help|-h> [OPCJE]"
printf $"Wersja: [%s]\n" "$g_revision"
echo
echo $"AKCJE:"
echo $" search <tytul> - wyszukaj film"
echo $" subtitles <movie_id> - wyszukaj napisy dla filmu wg ID"
echo $" download <subs_id> - sciagnij napisy o id"
echo $" scan <file|dir|*> - skanuj system plikow i pobierz napisy"
echo
echo $"OPCJE:"
echo $" -u | --user <login> - napiprojekt login"
echo $" -p | --pass <passwd> - napiprojekt haslo"
echo $" -i | --id <pynapi,other,NapiProjekt,NapiProjektPython> - klient id"
echo $" -L | --language <LNG> - jezyk napisow"
echo $" -l | --log <logfile> - drukuj wyjscie do pliku zamiast na konsole"
echo $" -lo| --log-overwrite - jezeli plik loga juz istnieje - nadpisz go"
echo $" -S | --script <script_path> - wywolaj skrypt po pobraniu napisow (sciezka do pliku z napisami, relatywna do argumentu napi.sh, bedzie przekazana jako argument)"
echo $" -v | --verbosity <0..3> - poziom gadatliwosci 0 - cichy, 3 - debug"
}
#
# @brief parse the cli arguments
#
_napi_globalArgvDispatcher() {
case "$1" in
"-u" | "--user") ___g_argvOutputHandlerType="func"
___g_argvOutputHandler="napiprojekt_setNapiprojektUserName_GV"
___g_argvErrorMsg=$"nie podano nazwy uzytkownika"
;;
"-p" | "--pass") ___g_argvOutputHandlerType="func"
___g_argvOutputHandler="napiprojekt_setNapiprojektPassword_GV"
___g_argvErrorMsg=$"nie podano hasla"
;;
"-i" | "--id") ___g_argvOutputHandlerType="func"
___g_argvOutputHandler="napiprojekt_setNapiprojektId_GV"
___g_argvErrorMsg=$"<pynapi,other> - legacy, <NapiProjekt,NapiProjektPython> - (API-3)"
;;
"-L" | "--language")
[ -z "$2" ] || [ "list" = "$2" ] && {
language_listLanguages_SO
return $G_RETBREAK
}
___g_argvOutputHandlerType="func"
___g_argvOutputHandler="language_setLanguage_GV"
___g_argvErrorMsg=$"wybierz jeden z dostepnych 2/3 literowych kodow jezykowych (-L list - lista)"
;;
# log overwrite option
"-lo" | "--log-overwrite") logging_raiseLogOverwrite ;;
# logfile
"-l" | "--log")
___g_argvOutputHandlerType="func"
___g_argvOutputHandler="logging_setLogFile"
___g_argvErrorMsg=$"nie podano nazwy pliku loga"
;;
# external script
"-S" | "--script")
___g_argvOutputHandlerType="func"
___g_argvOutputHandler="sysconf_setKey_GV system.hook.executable"
___g_argvErrorMsg=$"nie okreslono sciezki do skryptu"
;;
# verbosity
"-v" | "--verbosity") ___g_argvOutputHandlerType="func"
___g_argvOutputHandler="logging_setVerbosity"
___g_argvErrorMsg=$"okresl poziom gadatliwosci (0 - najcichszy, 3 - najbardziej gadatliwy, 4 - insane)"
;;
# action specific arguments
*) logging_debug $LINENO $"niezparsowany argument" "[$1]"
g_unparsedArgv=( "${g_unparsedArgv[@]}" "$1" )
;;
esac
}
#
# @brief parse the cli arguments
#
_parseArgv_GV() {
logging_debug $LINENO $"parsuje opcje globalne"
argv_argvParser_GV _napi_globalArgvDispatcher "$@"
napiprojekt_verifyArguments_GV
}
#
# @brief check if given action is supported
#
_verifyAction() {
local action="${1:-}"
[ -z "$action" ] && return $G_RETFAIL
case "$action" in
"search"|"download"|"scan"|"subtitles")
;;
*)
return $G_RETFAIL
;;
esac
}
#
# @brief execute the action
#
_executeAction() {
local action="$1" && shift
logging_debug $"wykonuje" "$action"
source "${G_NAPIPREFIX}/lib/napi/actions/libnapi_${action}.sh"
# if no arguments are given, then print action specific help and exit
# shellcheck disable=SC2086
[ "${#g_unparsedArgv[@]}" -lt 1 ] ||
[ "${g_unparsedArgv[0]}" = "--help" ] ||
[ "${g_unparsedArgv[0]}" = "-h" ] && {
if tools_verifyFunctionPresence "${action}_usage"; then
"${action}"_usage
return $G_RETOK
else
logging_error $"akcja nie implementuje wymaganej funkcji _usage"
return $G_RETFAIL
fi
}
# call action's main if it exists
if tools_verifyFunctionPresence "${action}_main"; then
"${action}_main" "${g_unparsedArgv[@]}"
else
logging_error $"akcja nie implementuje wymaganej funkcji _main"
return $G_RETFAIL
fi
}
#
# @brief detect system details and configure appropriate settings
#
_configureSystem() {
local cores=1
cores="$(wrappers_getCores_SO)"
logging_debug $LINENO $"wykryto rdzeni" "[$cores]"
# sanity checks
[ -z "${cores}" ] ||
[ "${#cores}" -eq 0 ] ||
[ "$cores" -eq 0 ] && cores=1
# two threads on one core should be safe enough
sysconf_setKey_GV system.forks $(( cores * 2 ))
}
#
# @brief main function
#
_main() {
# if no arguments are given, then print help and exit
# shellcheck disable=SC2086
[ $# -lt 1 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ] && {
_usage
return $G_RETOK
}
logging_msg $"wywolano o" "$(date)"
# verify tools presence
logging_debug $LINENO $"sprawdzam narzedzia ..."
tools_configure_GV || {
logging_error $"nie wszystkie wymagane narzedzia sa dostepne"
logging_debug $LINENO $(tools_toString_SO)
# shellcheck disable=SC2086
return $G_RETFAIL
}
logging_debug $LINENO $(tools_toString_SO)
# configure the rest of libraries
fs_configure_GV
http_configure_GV
# perform system configuration
_configureSystem
logging_msg "system: $(wrappers_getSystem_SO)," \
"forkow: $(sysconf_getKey_SO system.forks), wersja: $g_revision"
local action="$1" && shift
# verify given action name
_verifyAction "$action" || {
logging_error $"nieobslugiwana akcja" "[$action]"
return $G_RETFAIL
}
# parse global arguments
_parseArgv_GV "$@" || {
logging_error $"blad podczas parsowania argumentow"
return $G_RETFAIL
}
_executeAction "$action" || {
logging_error $"niepowodzenie"
return $G_RETFAIL
}
return $?
}
# call the main
[ "${SHUNIT_TESTS:-0}" -eq 0 ] && {
_main "$@"
}
# EOF