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

    # check the kernel
    if [ -f /opt/xcsoar/lib/kernel/uImage.kobo ]; then
        /bin/mkdir -p /proc /dev
        /bin/mount -t proc none /proc

        if zcat /proc/config.gz |grep -q ^CONFIG_USB_EHCI_ARC_OTG=y; then
            # this is the USB-OTG kernel, but Nickel should better have
            # the original Kobo kernel; install it and reboot
            echo "Switch to Kobo kernel"
            /bin/mount -t tmpfs none /dev
            /bin/mknod /dev/mmcblk0 b 179 0
            dd if=/opt/xcsoar/lib/kernel/uImage.kobo of=/dev/mmcblk0 bs=512 seek=2048 && sync && reboot
        fi

        umount /proc
    fi

    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

/bin/mknod /dev/null c 1 3
/bin/mknod /dev/zero c 1 5
/bin/mknod /dev/random c 1 8
/bin/mknod /dev/urandom c 1 9
/bin/mknod /dev/ptmx c 5 2
/bin/mkdir /dev/input
/bin/mknod /dev/input/event0 c 13 64
/bin/mknod /dev/input/event1 c 13 65
/bin/mknod /dev/fb0 c 29 0
/bin/mknod /dev/ttyACM0 c 166 0
/bin/mknod /dev/ttyACM1 c 166 1
/bin/mknod /dev/ttyACM2 c 166 2
/bin/mknod /dev/ttyACM3 c 166 3
/bin/mknod /dev/ttyACM4 c 166 4
/bin/mknod /dev/ttyACM5 c 166 5
/bin/mknod /dev/ttyACM6 c 166 6
/bin/mknod /dev/ttyACM7 c 166 7
/bin/mknod /dev/ttyACM8 c 166 8
/bin/mknod /dev/ttyACM9 c 166 9
/bin/mknod /dev/ttyUSB0 c 188 0
/bin/mknod /dev/ttyUSB1 c 188 1
/bin/mknod /dev/ttyUSB2 c 188 2
/bin/mknod /dev/ttyUSB3 c 188 3
/bin/mknod /dev/ttyUSB4 c 188 4
/bin/mknod /dev/ttyUSB5 c 188 5
/bin/mknod /dev/ttyUSB6 c 188 6
/bin/mknod /dev/ttyUSB7 c 188 7
/bin/mknod /dev/ttyUSB8 c 188 8
/bin/mknod /dev/ttyUSB9 c 188 9
/bin/mknod /dev/ttymxc0 c 207 16
/bin/mknod /dev/ttymxc1 c 207 17
/bin/mknod /dev/ttymxc2 c 207 18
/bin/mknod /dev/rtc0 c 254 0

/bin/mknod /dev/mmcblk0 b 179 0
/bin/mknod /dev/mmcblk0p1 b 179 1
/bin/mknod /dev/mmcblk0p2 b 179 2
/bin/mknod /dev/mmcblk0p3 b 179 3

/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

# 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 [ `dd if=/dev/mmcblk0 bs=8 count=1 skip=64` = "SN-N905B" ]; then
    sleep 1
fi

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

# just in case launching KoboMenu fails: try xcsoar
exec /opt/xcsoar/bin/xcsoar
