View Single Post
Old 11-06-2017, 04:32 AM   #73
h87booky
Junior Member
h87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it ish87booky knows what time it is
 
Posts: 9
Karma: 2004
Join Date: Nov 2017
Device: Kobo H2O
Hi, here's my solution.

I use the excellent webportal local web server plugin. https://www.mobileread.com/forums/sh...d.php?t=262353
I put all Kiwix binaries and zim files in a ext4 second partition on a 128 Go SD-Card (yes, you can, never tried 256 Go because I don't have one).
The Kobo browser is patched to work offline.

With telnet or ssh

Code:
# mkdir /mnt/wikipedia
The content of /usr/local/Kobo/udev/sd (to mount the wikipedia partition)

Code:
#!/bin/sh

if [ "$STARTUP" == "1" ]; then
  exit;
fi

# check whether nickel is running
[ "$(pidof nickel | wc -w)" != "0" ] && nickelisrunning=true || nickelisrunning=false

MODULE_LOADED=`lsmod | grep -c g_file_storage`
if [ $MODULE_LOADED -gt 0 ]; then
  if [ "$ACTION" == "remove" ]; then
    [ "$nickelisrunning" == "true" ] && echo sd $ACTION $DEVNAME >> /tmp/nickel-hardware-status
    ACTION=remove /usr/local/Kobo/udev/plug
    sleep 5
    exit
  fi
fi

DEV="/dev/mmcblk1p1"
MOUNTARGS="noatime,nodiratime,shortname=mixed,utf8"

if [ "$DEV" == "$DEVNAME" ]; then
  if [ "$ACTION" == "add" ]; then
    dosfsck -a -w $DEVNAME
    mount /dev/mmcblk1p2 /mnt/wikipedia
    mount -r -t vfat -o $MOUNTARGS $DEVNAME /mnt/sd
    if [ $? != 0 ]; then
      [ "$nickelisrunning" == "true" ] && echo sd mount fail $DEVNAME >> /tmp/nickel-hardware-status &
    else
      [ "$nickelisrunning" == "true" ] && echo sd $ACTION $DEVNAME >> /tmp/nickel-hardware-status &
      [ -e "/mnt/sd/autoscript.sh" ] && sh /mnt/sd/autoscript.sh
    fi
  fi
fi

if [ "$ACTION" == "remove" ]; then
  [ "$nickelisrunning" == "true" ] && echo sd $ACTION $DEVNAME >> /tmp/nickel-hardware-status &
  umount -l /mnt/sd
  umount -l /mnt/wikipedia
  sync
fi
The content of my "kiwix" file inside webportal/cgi-bin/

Code:
#!/bin/sh

# --- Constants: ---

TMPFILE=/tmp/servicemenu

ZIMEXT=zim
ZIMFOLDER=/mnt/wikipedia/
KIWIXM=/mnt/wikipedia/kiwix-manage
KIWIXS=/mnt/wikipedia/kiwix-serve
WIKIXML=/mnt/wikipedia/wiki.xml

# --- Helpers: ---

_chunk()
{
    CHUNK="$1"
    printf "%X\r\n" ${#CHUNK}
    printf "%s\r\n" "$CHUNK"
}

# htmlspecialchars
_htmlspecialchars()
{
    echo -n "$1" | sed -r -e 's@&@\&amp;@g' -e 's@[""]@\&quot;@g' -e 's@<@\&lt;@g' -e 's@>@\&gt;@g'
}

# urlencode
_urlencode()
{
    echo -n "$1" | hexdump -v -e '/1 "%02x"' | sed -r -e 's@..@%&@g'
}

# parse a=b&c=d data
_parse()
{
    PREFIX=$1
    DATA=$2

    while [ "$DATA" != "" ]
    do
        FIELD="`echo "$DATA" | cut -d '&' -f 1`"
        DATA="${DATA:$((${#FIELD}+1))}"

        key="`echo "$FIELD" | cut -d '=' -f 1 | sed -r -e s@[^a-zA-Z0-9_]@_@g`"
        value="`echo "$FIELD" | cut -s -d '=' -f 2-'`"

        if [ "$key" != "" ]
        then
            eval "$PREFIX""$key"'="`httpd -d "$value"`"'
        fi
    done
}

# print header
_header()
{
    echo -n -e 'HTTP/1.1 200 OK\r\n'
    echo -n -e 'CONTENT-Type: text/html; charset=UTF-8\r\n'
    echo -n -e 'Transfer-Encoding: chunked\r\n'
    echo -n -e 'Cache-Control: no-cache, must-revalidate\r\n'
    echo -n -e 'Expires: Thu, 01 Jan 1970 01:10:00 +0100\r\n'
    echo -n -e '\r\n'
    # echo -n -e 'CONTENT-Type: text/html; charset=UTF-8\r\n\r\n'
    _chunk "`printf "%4096s" " "`"
    _chunk '<html><head><style type="text/css">'

    _chunk 'input, textarea, select {
border: 1px solid #555;
padding: 0.5em;
font-size: 15px;
line-height: 1.2em;
width: 95%;
background: #fff;
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#ccc));
-webkit-appearance: none;
-webkit-box-shadow: 1px 1px 1px #fff;
-webkit-border-radius: 0.5em;
}
'

    _chunk '
#progress {
  margin: 0px auto;
  width: 60%;
  height: 30px;
  border: 8px double #222;
  overflow: hidden;
  background: #fff;
}
#progressbar {
  width: 0%;
  height: 30px;
  border-right: 4px solid #000;
  background: #aaa;
  position: relative;
  text-align: center;
  font-size: 25px;
}'

    _chunk '</style></head><body>'
    _chunk '<h1>Kiwix Menu</h1>'
}

# print footer
_footer()
{
    _chunk '<hr>'

    # extra links
    query="$1"
    shift
    text="$1"
    shift

    while [ "$text" != "" ]
    do
        _chunk '<a href="?'$query'">'$text'</a> | '
        query="$1"
        shift
        text="$1"
        shift
    done

    # standard footer
    _chunk '<a href="?">Back to Kiwix Menu</a>'

    _chunk '</body></html>'
    _chunk '' # terminator
    exit
}

# print error message
# argument: msg
_die()
{
    _chunk "<p>Error: $1</p>"
    _footer
    exit
}

# print yesno dialog
# argument: question yesquery noquery
_yesno()
{
    _chunk "<p> $1 </p>"
    #_chunk '<a href="?'$2'">Yes</a> <a href="?'$3'">No</a>'
    _footer "$2" Yes "$3" No
    exit
}

# print progress bar
_progressbar()
{
    TOTAL="$1"
    CURRENT="$2"

    percent=$(($CURRENT*100/$TOTAL))

    _chunk '<div id="progress"><div style="width: '$percent'% !important;" id="progressbar">'$percent'%</div></div>'
}

# --- Views: ---

_view()
{
    case "$GETaction" in
        "")
            _view_list
            ;;
        "info")
            _view_info
            ;;
        "kiwixon")                                                                        
            _view_kiwixon                                                    
            ;;
        "kiwixoff")                                                                        
            _view_kiwixoff                                                                                                                                                       
            ;;
        "kiwixupdate")                                                                        
            _view_kiwixupdate                                                                                                     
            ;;
        *)
            _die "Unknown action: $GETaction"
            ;;
    esac
}

_view_list()
{
    _chunk '<ul>'
    _chunk '<li><a href="?action=info">Info</a></li>'
    _chunk '</ul>'
    _chunk '<br/>'
    _chunk '<br/>'
    _chunk '<ul>'
    _chunk '<li><a href="?action=kiwixon">Start Kiwix Server</a></li>'
    _chunk '<br/>'
    _chunk '<li><a href="?action=kiwixoff">Stop Kiwix Server</a></li>'
    _chunk '<br/>'
    _chunk '<li><a href="?action=kiwixupdate">Update Kiwix Library</a></li>'
    _chunk '</ul>'
    _chunk '<br/>'
    _chunk '<br/>'
}

_view_info()
{
    _chunk '<h4>CPU</h4>'
    _chunk "<pre>$(_htmlspecialchars "$(cat /proc/cpuinfo)")</pre>"

    _chunk '<h4>RAM</h4>'
    _chunk "<pre>$(_htmlspecialchars "$(free -m)")</pre>"

    _chunk '<h4>HDD</h4>'
    _chunk "<pre>$(_htmlspecialchars "$(df -h)")</pre>"

    _chunk '<h4>WIFI</h4>'
    _chunk "<pre>$(_htmlspecialchars "$(iwconfig)")</pre>"
    _chunk "<pre>$(_htmlspecialchars "$(ifconfig)")</pre>"
}

_view_kiwixon()
{
    if pidof -s kiwix-serve > /dev/null; then
        _chunk "Kiwix is already running!"
    else
    _chunk "<p>Kiwix starting...</p>"
        $KIWIXS --daemon --port=8888 --library $WIKIXML
        sleep 5
        if pidof -s kiwix-serve > /dev/null; then
            _chunk "Kiwix has started!"
        else
            _chunk "Error!"
        fi
    fi
}

_view_kiwixoff()
{
    if pidof -s kiwix-serve > /dev/null; then
        _chunk "<p>Kiwix stopping...</p>"
        killall -q kiwix-serve
        sleep 5
        if pidof -s kiwix-serve > /dev/null; then
            _chunk "<p>Retrying...</p>"
            killall -q kiwix-serve
            sleep 5
        else
            _chunk "<p>Kiwix has stopped.</p>"
        fi
    else
        _chunk "<p>Already stopped.</p>"
    fi
}

_view_kiwixupdate()
{
    _chunk "<p>Please wait...</p>"
    rm -f $WIKIXML
    for ZIMCONTENT in $ZIMFOLDER*; do
        if [ "${ZIMCONTENT}" != "${ZIMCONTENT%.${ZIMEXT}}" ];then
            $KIWIXM $WIKIXML add $ZIMCONTENT
        fi
    done
    _chunk "<p>Done.</p>"
}

# --- Main: ---

_parse GET "$QUERY_STRING"
read POST_STRING
_parse POST "$POST_STRING"
_header
_view
_footer

# --- End of file. ---
How it works?

I open the browser (the default home page is webportal).
I choose the kiwix menu, then I can launch kiwix-serve and go to the bookmark 127.0.0.1:8888 or refresh the list of my zim files with kiwix-manage and generate a new xml file.

Because the zim files are on a ext4 partitions I don't have to split them.

To acess the zim files with built-in Kobo reader on the good old 3.19.5761 kernel, you can do this:

Code:
# mkdir /mnt/onboard/wikipedia
Then in /usr/local/Kobo/udev/sd add something like

Code:
mount /dev/mmcblk1p2 /mnt/wikipedia
mount --bind /mnt/wikipedia /mnt/onboard/wikipedia
Maybe it's very dirty, but I like it this way.

Last edited by h87booky; 11-07-2017 at 08:49 PM. Reason: Confusing location of /usr/local/Kobo/udev/sd with my own mount point
h87booky is offline   Reply With Quote