#!/bin/sh
#
#  XCSoar Glide Computer - http://www.xcsoar.org/
#  Copyright (C) 2000-2021 The XCSoar Project
#
# 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 2 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.
#

# This script is launched by /etc/inittab on boot.  It initialises the
# Kobo Mini and launches XCSoar's KoboMenu.

/bin/mount -o remount,noatime,nodiratime /dev/mmcblk0p1 /

export PATH=/sbin:/bin:/usr/sbin:/usr/bin
umask 022

# mount the data partition
/bin/dosfsck -a -w /dev/mmcblk0p3
/bin/mkdir -p /mnt/onboard
/bin/mount -t vfat -o iocharset=utf8 /dev/mmcblk0p3 /mnt/onboard
/bin/rm -rf /mnt/onboard/fsck* /mnt/onboard/FSCK*

# launch the original Kobo init script if:
# - there's a pending update
# - Nickel start was scheduled
# - the XCSoar installation is broken
if [ -f /mnt/onboard/.kobo/Kobo.tgz -o -f /mnt/onboard/.kobo/KoboRoot.tgz \
    -o -f /mnt/onboard/XCSoarData/kobo/start_nickel \
    -o ! -x /opt/xcsoar/bin/KoboMenu ]; then

    rm -f /mnt/onboard/XCSoarData/kobo/start_nickel
    # launch user script before nickel start
    if [ -f /mnt/onboard/XCSoarData/kobo/init_nickel.sh ]; then
        source /mnt/onboard/XCSoarData/kobo/init_nickel.sh
    fi
    umount -l /mnt/onboard
    exec /etc/init.d/rcS
fi

# prepare file system

/bin/mkdir -p /proc /dev /root /tmp /sys /var/lib /var/log /var/run

/bin/mount -t proc none /proc
/bin/mount -t tmpfs none -o size=16m /tmp
/bin/mount -t tmpfs none /dev
/bin/mount -t tmpfs none -o size=16k /var/lib
/bin/mount -t tmpfs none -o size=16k /var/log
/bin/mount -t tmpfs none -o size=128k /var/run
/bin/mount -t sysfs none -o size=500k /sys

# WORKAROUND: /dev/null might be needed in udev rules, before /dev/null is
# created by udev, therefore create it before starting udev.
/bin/mknod /dev/null c 1 3
/bin/chmod 666 /dev/null

/sbin/udevd --daemon
/sbin/udevadm trigger

/sbin/hwclock -s -u

# workaround for kernel crash "scheduling while atomic"
echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug

# disable the flashing led
echo "ch 3" > /sys/devices/platform/pmic_light.1/lit
echo "cur 0" > /sys/devices/platform/pmic_light.1/lit
echo "dc 0" > /sys/devices/platform/pmic_light.1/lit

# enable crash dumps
/bin/mkdir -p /mnt/onboard/XCSoarData/crash
echo '/mnt/onboard/XCSoarData/crash/core.%p' >/proc/sys/kernel/core_pattern
ulimit -c unlimited

# workaround for Kobo Clara HD: the touch screen doesn't respond
# and the initial screen render is blank if we are too fast
# delay should not cause any problem and might help other models
# and at least for the Libra 2 we have to wait for /dev/mmcblk0
sleep 1

# load kernel modules for ClaraHD, Libra2 and Libra H2O as factory kernel is OTG
# but missing modules. Also put ClaraHD and Libra2 in the right mode
model=`dd if=/dev/mmcblk0 bs=8 count=1 skip=64`
if [ `expr substr $model 1 7` = SN-N418 ] || [ `expr substr $model 1 7` = SN-N249 ] || \
    [ `expr substr $model 1 7` = SN-N873 ] || [ `expr substr $model 1 7` = SN-N506 ]; then
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/usbserial.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/aircable.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/ch341.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/cp210x.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/ftdi_sio.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/garmin_gps.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/mos7840.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/navman.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/serial/pl2303.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/usb/class/cdc-acm.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/sound/core/snd-hwdep.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/sound/core/snd-rawmidi.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/sound/usb/snd-usbmidi-lib.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/sound/usb/snd-usb-audio.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/mii.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/usbnet.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/cdc_subset.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/cdc_ether.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/cdc_eem.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/cdc_ncm.ko"
    insmod "/opt/xcsoar/lib/modules/4.1.15/drivers/net/usb/rndis_host.ko"
    mount -t debugfs none /sys/kernel/debug
    if [ -f /mnt/onboard/XCSoarData/kobo/OTG_Host_Active ]; then
        echo host >/sys/kernel/debug/ci_hdrc.0/role
    fi
fi
# launch user script
if [ -f /mnt/onboard/XCSoarData/kobo/init.sh ]; then
    source /mnt/onboard/XCSoarData/kobo/init.sh
fi

# workaround for Kobo Touch N905B kernel bug: the mxc_fb driver
# crashes when KoboMenu tries to rotate the display too early after
# boot
if [ $model = "SN-N905B" ]; then
    sleep 1
fi

# WORKAROUND: USB devices which require USB mode switching might not
# suffesscully switched, if simple_modeswitch was called too early (before the
# USB device files were created). Call it again to make sure.
/opt/xcsoar/bin/simple_usbmodeswitch

# finally, open the menu
/opt/xcsoar/bin/KoboMenu &
