forked from Chinachu/Chinachu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chinachu
executable file
·698 lines (560 loc) · 14.5 KB
/
chinachu
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/bin/bash
# Chinachu-CLI
# ------------
#
# Usage:
#
# ./chinachu <cmd> ...
#
if [ -z "$BASH" ]; then
echo "Chinachu-CLI only works with bash. Sorry." >&2
exit 1
fi
shopt -s extglob
# variables
NODE_VER=4.5.0
main () {
local SELF_PATH DIR SYM
# get the absolute path of the executable
SELF_PATH="$0"
if [ "${SELF_PATH:0:1}" != "." ] && [ "${SELF_PATH:0:1}" != "/" ]; then
SELF_PATH=./"$SELF_PATH"
fi
SELF_PATH=$( cd -P -- "$(dirname -- "$SELF_PATH")" \
&& pwd -P \
) && SELF_PATH=$SELF_PATH/$(basename -- "$0")
# resolve symlinks
while [ -h "$SELF_PATH" ]; do
DIR=$(dirname -- "$SELF_PATH")
SYM=$(readlink -- "$SELF_PATH")
SELF_PATH=$( cd -- "$DIR" \
&& cd -- $(dirname -- "$SYM") \
&& pwd \
)/$(basename -- "$SYM")
done
# path
CHINACHU_DIR=$(dirname -- "$SELF_PATH")
NAVE_DIR=${CHINACHU_DIR}/.nave
NODE_PATH=${NAVE_DIR}/installed/${NODE_VER}/bin/node
NPM_PATH=${NAVE_DIR}/installed/${NODE_VER}/bin/npm
USR_DIR=${CHINACHU_DIR}/usr
export PATH=$USR_DIR/bin:${NAVE_DIR}/installed/${NODE_VER}/bin:$PATH
export LD_LIBRARY_PATH=$USR_DIR/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=$USR_DIR/lib/pkgconfig
cd $CHINACHU_DIR
export CHINACHU_DIR
export NAVE_DIR
local cmd="$1"
shift
case $cmd in
installer | updater | service | update | search | \
reserve | unreserve | skip | unskip | stop | \
rule | enrule | disrule | rmrule | \
rules | reserves | recording | recorded | \
cleanup | ircbot | test )
cmd="chinachu_$cmd"
;;
* )
cmd="chinachu_help"
;;
esac
$cmd "$@" && exit 0 || fail "failed somehow"
}
chinachu_installer () {
ensure_dir "$USR_DIR"
ensure_dir "$USR_DIR/src"
ensure_dir "$USR_DIR/bin"
export CFLAGS="-O2 -w -pipe -march=native"
export CXXFLAGS="-O2 -w -pipe -march=native"
export MAKEFLAGS="-j $((`grep processor /proc/cpuinfo | wc -l` + 1)) -l 3"
echo "Chinachu Installer Menu:"
echo "[!] These are installed under all ${CHINACHU_DIR}/..."
echo "[!] Recommend the Auto installation."
local PS3 item cmd
PS3="what do you install? > "
select item in \
"Auto (full)" "Auto (fast)" "submodule" "Node.js Environment" "Node.js Modules" \
"epgdump" "ffmpeg"
do
echo "selected: ${item}"
case $item in
"Auto (full)" )
cmd="chinachu_installer_auto_full"
;;
"Auto (fast)" )
cmd="chinachu_installer_auto_fast"
;;
"submodule" )
cmd="chinachu_installer_submodule"
;;
"Node.js Environment" )
cmd="chinachu_installer_node"
;;
"Node.js Modules" )
cmd="chinachu_installer_node_modules"
;;
"epgdump" )
cmd="chinachu_installer_epgdump"
;;
"ffmpeg" )
cmd="chinachu_installer_ffmpeg"
;;
* )
cmd="chinachu_installer"
;;
esac
$cmd && break
done
return 0
}
chinachu_installer_auto_full () {
chinachu_installer_submodule
chinachu_installer_node
chinachu_installer_node_modules
chinachu_installer_epgdump
chinachu_installer_ffmpeg
return 0
}
chinachu_installer_auto_fast () {
chinachu_installer_submodule
chinachu_installer_node
chinachu_installer_node_modules
return 0
}
chinachu_installer_submodule () {
git submodule init
git submodule update
return 0
}
chinachu_installer_node () {
remove_dir "$NAVE_DIR"
ensure_dir "$NAVE_DIR"
echo "Installing Node using Nave..."
wget -O - https://github.com/isaacs/nave/archive/v0.5.1.tar.gz | tar zxvf - -C $NAVE_DIR nave-0.5.1/nave.sh
mv $NAVE_DIR/nave-0.5.1/nave.sh $NAVE_DIR/
rm -rfv $NAVE_DIR/nave-0.5.1
${NAVE_DIR}/nave.sh install $NODE_VER
rm -fv ${NAVE_DIR}/node
ln -sv $NODE_PATH ${NAVE_DIR}/node
ln -sv $NPM_PATH ${NAVE_DIR}/npm
echo "done."
return 0
}
chinachu_installer_node_modules () {
echo "Installing Node Modules using NPM..."
npm install
npm update
echo "done."
return 0
}
chinachu_installer_epgdump () {
remove_dir "$USR_DIR/src/epgdump"
ensure_dir "$USR_DIR/src/epgdump"
local EPGDUMP_TB="https://github.com/stz2012/epgdump/archive/master.tar.gz"
echo "Installing epgdump from $EPGDUMP_GIT"
wget -O - $EPGDUMP_TB | tar zxvf - -C $USR_DIR/src/epgdump
cd "$USR_DIR/src/epgdump/epgdump-master"
make || fail "couldn't make epgdump"
cp epgdump $USR_DIR/bin/
cd $CHINACHU_DIR
remove_dir "$USR_DIR/src/epgdump"
echo "epgdump --> done."
return 0
}
chinachu_installer_ffmpeg () {
remove_dir "$USR_DIR/src/ffmpeg"
ensure_dir "$USR_DIR/src/ffmpeg"
cd $USR_DIR
MACHINE=`uname -m`
if [ ${MACHINE} == "x86_64" ]; then
local FFMPEG_FN="ffmpeg-release-64bit-static"
else
local FFMPEG_FN="ffmpeg-release-32bit-static"
fi
local FFMPEG_XZ="http://johnvansickle.com/ffmpeg/releases/${FFMPEG_FN}.tar.xz"
wget $FFMPEG_XZ
xz -dv "${FFMPEG_FN}.tar.xz"
tar -xvf "${FFMPEG_FN}.tar" -C bin --strip-components 1
rm -v "${FFMPEG_FN}.tar"
cd $CHINACHU_DIR
echo "Creating libav aliases..."
rm -rfv "$USR_DIR/bin/avconv"
rm -rfv "$USR_DIR/bin/avprobe"
ln -sv "$USR_DIR/bin/ffmpeg" "$USR_DIR/bin/avconv"
ln -sv "$USR_DIR/bin/ffprobe" "$USR_DIR/bin/avprobe"
echo "ffmpeg --> done."
return 0
}
chinachu_updater () {
if [ -e "/etc/init.d/chinachu-operator" ]; then
/etc/init.d/chinachu-operator status
fi
if [ -e "/etc/init.d/chinachu-wui" ]; then
/etc/init.d/chinachu-wui status
fi
git fetch
git status
while [ 1 ]; do
echo -n "Do you want to update [Y/n]? "
read line
case ${line:-y} in
[yY])
chinachu_updater_main && return 0
;;
[nN])
echo "Abort." && return 0
;;
esac
done
}
chinachu_updater_main () {
if [ -e "/etc/init.d/chinachu-operator" ] && [ -e "/etc/init.d/chinachu-wui" ]; then
while [ 1 ]; do
echo -n "Do you want to stop chinachu services [Y/n]? "
read line
case ${line:-y} in
[yY])
if type sudo > /dev/null 2>&1; then
sudo /etc/init.d/chinachu-operator stop
sudo /etc/init.d/chinachu-wui stop
break
fi
su -c "/etc/init.d/chinachu-operator stop; /etc/init.d/chinachu-wui stop"
break
;;
[nN])
break
;;
esac
done
fi
git pull || fail "failed: git pull"
git gc
while [ 1 ]; do
echo -n "Do you want to run chinachu installer (fast) [Y/n]? "
read line
case ${line:-y} in
[yY])
echo 2 | ./chinachu installer
break
;;
[nN])
break
;;
esac
done
if [ -e "/etc/init.d/chinachu-operator" ] && [ -e "/etc/init.d/chinachu-wui" ]; then
while [ 1 ]; do
echo -n "Do you want to start chinachu services [Y/n]? "
read line
case ${line:-y} in
[yY])
if type sudo > /dev/null 2>&1; then
sudo /etc/init.d/chinachu-operator start
sudo /etc/init.d/chinachu-wui start
break
fi
sudo -c "/etc/init.d/chinachu-operator start; /etc/init.d/chinachu-wui start"
break
;;
[nN])
break
;;
esac
done
fi
}
chinachu_service () {
local cmd name action
name="$1"
action="$2"
case $action in
initscript | execute )
case $name in
operator | wui )
cmd="chinachu_service_$action"
;;
* )
cmd="chinachu_service_help"
;;
esac
;;
* )
cmd="chinachu_service_help"
;;
esac
$cmd "$name" && return 0
}
chinachu_service_initscript () {
local name="$1"
cat <<EOF
#!/bin/bash
# /etc/
### BEGIN INIT INFO
# Provides: chinachu-${name}
# Required-Start: \$local_fs \$remote_fs \$network \$syslog
# Required-Stop: \$local_fs \$remote_fs \$network \$syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the chinachu-${name}
# Description: starts the chinachu-${name} (USER=$USER)
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${CHINACHU_DIR}/chinachu
DAEMON_OPTS="service ${name} execute"
NAME=chinachu-${name}
USER=$USER
PIDFILE=/var/run/\${NAME}.pid
test -x \$DAEMON || exit 0
start () {
echo -n "Starting \${NAME}: "
if [ -f \$PIDFILE ]; then
PID=\`cat \$PIDFILE\`
if [ -z "\`ps axf | grep \${PID} | grep -v grep\`" ]; then
rm -f \$PIDFILE
else
echo "\${NAME} is already running? (pid=\${PID})"
exit
fi
fi
PID=\$(su \$USER -c "exec \$DAEMON \$DAEMON_OPTS < /dev/null > /dev/null 2>&1 & echo \\\$!")
if [ -z \$PID ]; then
echo "Failed!"
exit
else
echo \$PID > \$PIDFILE
fi
echo "OK."
}
stop () {
echo -n "Stopping \${NAME}: "
PID=\`cat \$PIDFILE\`
if [ -f \$PIDFILE ]; then
PGID=\`ps -p \$PID -o pgrp | grep -v PGRP\`
kill -QUIT -\`echo \$PGID\`
echo "OK."
rm -f \$PIDFILE
else
echo "\${NAME} is not running? (\${PIDFILE} not found)."
fi
}
status () {
if [ -f \$PIDFILE ]; then
PID=\`cat \$PIDFILE\`
if [ -z "\`ps axf | grep \${PID} | grep -v grep\`" ]; then
echo "\${NAME} is dead but \${PIDFILE} exists."
else
echo "\${NAME} is running."
fi
else
echo "\${NAME} is NOT running."
fi
}
case "\$1" in
start )
start "\$@"
;;
stop )
stop "\$@"
;;
restart )
stop "\$@"
sleep 3
start "\$@"
;;
status )
status "\$@"
;;
*)
echo "Usage: \$NAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
EOF
return 0
}
chinachu_service_execute () {
local name="$1"
if ! [ -f "config.json" ]; then
cp -v config.sample.json config.json
fi
if ! [ -f "rules.json" ]; then
cp -v rules.sample.json rules.json
fi
ensure_dir log
ensure_dir data
node app-${name}.js > /dev/stdout 2>&1 | tee -a ./log/${name}
return ${PIPESTATUS[0]}
}
chinachu_service_help () {
cat <<EOF
Usage: ./chinachu service <name> <action>
Names:
operator Chinachu Task Operator Service
wui Chinachu WebUI Server Service
Actions:
initscript Output a LSB init script
execute Execute
Examples:
# Create a LSB init script and Install to system
./chinachu service operator initscript > /tmp/chinachu-operator
./chinachu service wui initscript > /tmp/chinachu-wui
sudo chown root:root /tmp/chinachu-operator /tmp/chinachu-wui
sudo chmod +x /tmp/chinachu-operator /tmp/chinachu-wui
sudo mv /tmp/chinachu-operator /tmp/chinachu-wui /etc/init.d/
EOF
return 0
}
chinachu_update () {
node app-scheduler.js "$@" > /dev/stdout 2>&1 | tee -a ./log/scheduler
return ${PIPESTATUS[0]}
}
chinachu_search () {
node app-cli.js -mode search "$@" && return 0
}
chinachu_reserve () {
if [ ! "$1" ]; then
fail "Usage: reserve <pgid>"
fi
node app-cli.js -mode reserve -id "$@" || fail "couldn't reserve $1"
while [ 1 ]; do
echo -n "Run a scheduler [Y/n]? "
read line
case ${line:-y} in
[yY])
node app-scheduler.js > /dev/stdout 2>&1 | tee -a ./log/scheduler
return ${PIPESTATUS[0]}
;;
[nN])
echo "Ok." && return 0
;;
esac
done
}
chinachu_unreserve () {
if [ ! "$1" ]; then
fail "Usage: unreserve <pgid>"
fi
node app-cli.js -mode unreserve -id "$@" || fail "couldn't unreserve $1"
}
chinachu_skip () {
if [ ! "$1" ]; then
fail "Usage: skip <pgid>"
fi
node app-cli.js -mode skip -id "$@" || fail "couldn't skip $1"
}
chinachu_unskip () {
if [ ! "$1" ]; then
fail "Usage: unskip <pgid>"
fi
node app-cli.js -mode unskip -id "$@" || fail "couldn't skip $1"
}
chinachu_stop () {
if [ ! "$1" ]; then
fail "Usage: stop <pgid>"
fi
node app-cli.js -mode stop -id "$@" || fail "couldn't stop $1"
}
chinachu_rule () {
node app-cli.js -mode rule "$@" && return 0
}
chinachu_enrule () {
if [ ! "$1" ]; then
fail "Usage: enrule <ruleid>"
fi
node app-cli.js -mode rule --enable -n "$@" && return 0
}
chinachu_disrule () {
if [ ! "$1" ]; then
fail "Usage: disrule <ruleid>"
fi
node app-cli.js -mode rule --disable -n "$@" && return 0
}
chinachu_rmrule () {
if [ ! "$1" ]; then
fail "Usage: rmrule <ruleid>"
fi
node app-cli.js -mode rule --remove -n "$@" && return 0
}
chinachu_rules () {
node app-cli.js -mode rules "$@" && return 0
}
chinachu_reserves () {
node app-cli.js -mode reserves "$@" && return 0
}
chinachu_recording () {
node app-cli.js -mode recording "$@" && return 0
}
chinachu_recorded () {
node app-cli.js -mode recorded "$@" && return 0
}
chinachu_cleanup () {
echo "Clean-up the recorded list."
while [ 1 ]; do
echo -n "Do you want to continue [Y/n]? "
read line
case ${line:-y} in
[yY])
node app-cli.js -mode cleanup "$@" && return 0
;;
[nN])
echo "Abort." && return 0
;;
esac
done
}
chinachu_ircbot () {
node app-cli.js -mode ircbot "$@" && return 0
}
chinachu_test () {
if [ ! "$1" ]; then
fail "Usage: test <app> [options]"
fi
${USR_DIR}/bin/"$@" && return 0
}
chinachu_help () {
cat <<EOF
Usage: ./chinachu <cmd> ...
Commands:
installer Run a Installer.
updater Run a Updater.
service <name> <action> Service-utility.
update Run a Scheduler.
search [options] Search for programs.
reserve <pgid> Reserve the program manually.
unreserve <pgid> Unreserve the program manually.
skip <pgid> Skip the auto-reserved program.
unskip <pgid> Cancel the skip.
stop <pgid> Stop the recording.
rule [options] Add or config a rule for auto reservation.
enrule <rule#> Enable a rule. (alias of rule -n <rule#> --enable)
disrule <rule#> Disable a rule. (alias of rule -n <rule#> --disable)
rmrule <rule#> Remove a rule. (alias of rule -n <rule#> --remove)
rules Show a list of auto reserving rules.
reserves Show a list of reserved programs.
recording Show a list of recording programs.
recorded Show a list of recorded programs.
cleanup Clean-up the recorded list.
ircbot [options] Connect to IRC server and run a ircbot. (Experimental)
test <app> [options] Run <app> in ${USR_DIR}/bin
help Output this information.
EOF
return 0
}
ensure_dir () {
if ! [ -d "$1" ]; then
mkdir -p -- "$1" || fail "couldn't create $1"
fi
}
remove_dir () {
if [ -d "$1" ]; then
rm -rf -- "$1" || fail "couldn't remove $1"
echo "removed \`$1'"
fi
}
fail () {
echo "$@" >&2
exit 1
}
main "$@"