#!/bin/sh
### Copyright 2010 b-rad.cc
### http://wdlxtv.com & http://b-rad.cc & http://nextdimension.cc
### GPLv3 - full license located @ /usr/share/LICENSE
### Stipulations:
### - the full copyright notices in /osd/setup_about.xml must be left intact, as it is the publicly displayed copyright portion of this software as dictated by the GPLv3
### - this entire header must be left intact

case $1 in
    start|forcestart)
        if [ -n "`grep -i LINKSHEETS=\'ON\' /conf/config`" -o "$1" == "forcestart" ] ; then
            if [ "$FW_VERSION" == "WDLXTV_LIVE" -a -z "`grep -i WDTVEXT=\'OFF\' /conf/config`" -a -n "`grep MovieSheet.plugin.js /conf/wdtvext-plugins`" ] ; then
                logger -t `basename $0` "Warning LINKSHEETS=ON, but MovieSheet WDTVExt plugin is enabled!!! You must disable MovieSheet to use linksheets."
                cat /osd/empty.xml | tee /tmp/file_info.xml /tmp/list_sheet.xml /tmp/list_wall.xml
                eval `egrep 'MSHEETDIR|MENUSHEETDIR' /conf/config`

                if [ -n "$MENUSHEETDIR" -a -e "$MENUSHEETDIR" ] ; then
                    MSD2="$MENUSHEETDIR"
                elif -n "$MENUSHEETDIR" -a ! -e "$MENUSHEETDIR" ] ; then
                    logger -t `basename $0` -s "MENUSHEETDIR ( $MENUSHEETDIR ) DOES NOT EXIST!"
                    MSD2="/tmp/.root/osd/sample-menusheets"
                else
                    MSD2="/tmp/.root/osd/sample-menusheets"
                fi

                mkdir -p /tmp/.root/osd
                mount -o bind /osd /tmp/.root/osd
                unionfs /tmp/.root/osd:"$MSD2" /osd -o nonempty,allow_other

            else
                cp /osd/file_info.2.xml /tmp/file_info.xml
                cp /osd/list_sheet.2.xml /tmp/list_sheet.xml
                cp /osd/list_wall.2.xml /tmp/list_wall.xml
                eval `egrep 'MSHEETDIR|MENUSHEETDIR' /conf/config`
                [ -n "`grep 'unionfs /osd ' /proc/mounts`" ] && logger -t `basename $0` "/msheets already mounted!" && exit 1
                if [ -n "$MSHEETDIR" -a -e "$MSHEETDIR" ] ; then
                    mount -o bind "$MSHEETDIR" /msheets
                    MSD="$MSHEETDIR"
                elif [ -n "$MSHEETDIR" -a ! -e "$MSHEETDIR" ] ; then
                    logger -t `basename $0` -s "MSHEETDIR ( $MSHEETDIR ) DOES NOT EXIST!"
                    mount -t tmpfs none /msheets
                    MSD="/msheets"
                else
                    mount -t tmpfs none /msheets
                    MSD="/msheets"
                fi

                if [ -n "$MENUSHEETDIR" -a -e "$MENUSHEETDIR" ] ; then
                    MSD2="$MENUSHEETDIR"
                elif -n "$MENUSHEETDIR" -a ! -e "$MENUSHEETDIR" ] ; then
                    logger -t `basename $0` -s "MENUSHEETDIR ( $MENUSHEETDIR ) DOES NOT EXIST!"
                    MSD2="/tmp/.root/osd/sample-menusheets"
                else
                    MSD2="/tmp/.root/osd/sample-menusheets"
                fi

                mkdir -p /tmp/.root/osd
                mount -o bind /osd /tmp/.root/osd
                unionfs /tmp/.root/osd:"$MSD":"$MSD2" /osd -o nonempty,allow_other
            fi
        fi
    ;;
esac
