#!/bin/sh

if [ -f "/etc/firstboot.sh" ]; then
    /etc/firstboot.sh
    if [ $? != 0 ]; then
        exit 1
    else
        rm -f /etc/firstboot.sh
        poweroff
    fi
fi

# Recreate the config directories if necessary.
if [ ! -d "/root/Settings" ]; then
    rm -f /root/Settings
    mkdir -p /root/Settings
fi

if [ ! -d "/root/onyx_reader" ]; then
    rm -f /root/onyx_reader
    mkdir -p /root/onyx_reader
fi

if [ ! -d "/usr/share/adobe/resources/fonts" ]; then
    ln -s /opt/onyx/arm/lib/fonts /usr/share/adobe/resources/fonts
fi

if [ ! -f "/root/Settings/language" ]; then
    rm -rf /root/Setting/language  # just in case there's a dir with the name.
    echo "export LANG=en_US.UTF-8" > /root/Settings/language
fi

if [ ! -f "/root/Settings/pointercal" ]; then
    rm -rf /root/Settings/pointercal
    echo "-2584 10 -290058 0 2655 -53469334 -44355" > /root/Settings/pointercal
fi

if [ ! -f "/root/Settings/screen" ]; then
    rm -rf /root/Settings/screen
    echo "export QWS_DISPLAY=Transformed:Rot0:LinuxFB:/dev/fb0" > /root/Settings/screen
fi

[ -f /etc/profile ] && source /etc/profile
cat /sys//class/graphics/fb0/waveform  > /root/Settings/wave_name


# Load mmc modules.
# sdmmc_module.sh load

# Now we're using system bus.
if [ ! -d "/var/run/dbus" ]; then
    rm -f /var/run/dbus
    mkdir -p /var/run/dbus/
fi

if [ -f "/var/run/dbus/pid" ]; then
    rm -f /var/run/dbus/pid
fi
dbus-daemon --system --print-address

# For onyx touch
if [ ! -d "/tmp/onyx" ]; then
    rm -f /tmp/onyx
    mkdir -p /tmp/onyx
fi



# create link
cd /opt/onyx/arm/lib/fonts
for args in *.otf
do
    ln -sf $args ${args%%.*}.ttf
done

# Start watchdog, a safe wrapper for system_manager
/etc/watchdog.sh &
