Last active
August 29, 2015 14:02
-
-
Save rgaudin/569e00eb50ebfac9fb5f to your computer and use it in GitHub Desktop.
kiwix-serve helper for cybook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### | |
# kiwix-serve helper for Cybook Odyssey | |
# | |
# launch upon mount/unmount event of the SD card and at boot time. | |
# - tries to kill a runnning kiwix-serve | |
# - tries to launch kiwix-serve if a library file is present | |
# | |
### | |
INTERNAL_STORAGE_MNT=/var/books | |
EXTERNAL_STORAGE_MNT=/mnt/sd/ | |
KIWIX_SERVE=/usr/local/bin/kiwix-serve | |
KIWIX_PID=/var/run/kiwix-serve.pid | |
KIWIX_PORT=4242 | |
KIWIX_LIBRARY="" | |
KIWIX_VERBOSE=1 | |
binecho=/bin/echo | |
LIBRARY_FILE="data/library/library.xml" | |
usage () { | |
echo "Usage: $0 start|restart|stop" | |
echo "" | |
echo "\tstart," | |
echo "\trestart:\tAt boot, mount or unmount" | |
echo "\t\t\ttries to find a running kiwix-serve and kill it" | |
echo "\t\t\ttries to find a library and launch kiwix-serve" | |
echo "" | |
echo "\tstop:\t\tAt system shutdown." | |
echo "\t\t\ttries to find a running kiwix-serve and kill it" | |
} | |
launch_kiwix_serve() { | |
if [ "$KIWIX_LIBRARY" = "" ]; | |
then | |
return 0 | |
fi | |
# verbose mode switch | |
if [ $KIWIX_VERBOSE -eq 1 ]; | |
then | |
verbose="--verbose" | |
else | |
verbose="" | |
fi | |
# launch kiwix-serve | |
${KIWIX_SERVE} --library -p ${KIWIX_PORT} ${verbose} ${KIWIX_LIBRARY} & | |
kpid=$! | |
${binecho} $kpid > ${KIWIX_PID} | |
${binecho} "kiwix-serve PID: $kpid. Library: ${KIWIX_LIBRARY}" | |
return $kpid | |
} | |
launch_if_library_exist() { | |
${binecho} -n "Attempt to launch kiwix-serve if library exist..." | |
# look for library on SD card | |
if [ -f "${EXTERNAL_STORAGE_MNT}/${LIBRARY_FILE}" ]; then | |
KIWIX_LIBRARY="${EXTERNAL_STORAGE_MNT}/${LIBRARY_FILE}" | |
${binecho} "found on SD." | |
launch_kiwix_serve | |
return 1 | |
else | |
${binecho} -n "not on SD... Internal ? " | |
fi | |
# look for library on device (only if SD failed) | |
if [ "$KIWIX_LIBRARY" = "" -a -f "${INTERNAL_STORAGE_MNT}/${LIBRARY_FILE}" ]; then | |
KIWIX_LIBRARY="${INTERNAL_STORAGE_MNT}/${LIBRARY_FILE}" | |
${binecho} "found on device." | |
launch_kiwix_serve | |
return 1 | |
# no library found, return | |
else | |
${binecho} "nope." | |
return 0 | |
fi | |
} | |
kill_kiwix_if_running() { | |
${binecho} -n "Attempt to kill kiwix-serve if running..." | |
if [ -f "${KIWIX_PID}" ]; | |
then | |
kill -HUP `cat ${KIWIX_PID}` | |
rm -f ${KIWIX_PID} | |
${binecho} "OK" | |
else | |
${binecho} "nope." | |
fi | |
} | |
# upon SD card mount (inserted, boot) or unmount | |
if [ "$1" = "start" -o "$1" = "restart" ]; then | |
${binecho} "launching/relaunching kiwix-serve." | |
kill_kiwix_if_running | |
launch_if_library_exist | |
# tear down on system shut down | |
elif [ "$1" = "stop" ]; then | |
${binecho} "shutting down kiwix-serve" | |
kill_kiwix_if_running | |
# if first param neither `start` nor `stop`, exit. | |
else | |
usage | |
exit | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment