#!/bin/sh
############################################################
# <bsn.cl fy=2013 v=onl>
#
#        Copyright 2013, 2014 BigSwitch Networks, Inc.
#
# Licensed under the Eclipse Public License, Version 1.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#        http://www.eclipse.org/legal/epl-v10.html
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the
# License.
#
# </bsn.cl>
############################################################
#
# initblockdev
#
# Initialize all block mounts for the platform.
#
############################################################
set -e
exec 9<$0
flock -x 9
case $2 in
    add)
        eval $(echo $1 | sed "s#^\(mmcblk[0-9]*\|ubi[0-9]*_[0-9]*\|mtdblock[0-9]*\|[a-z]*\)\(p[0-9]*\|[0-9]*\).*#dev='\1' part='\2'#")
        devid=
        if [ -e /sys/class/ubi/${dev}/device ]; then
            eval $(realpath /sys/class/ubi/${dev}/device | sed "s#/sys/devices/\(.*\)#devid='\1'#")
        fi
        if [ -e /sys/class/block/${dev}/device ]; then
            eval $(realpath /sys/class/block/${dev}/device | sed "s#/sys/devices/\(.*\)#devid='\1'#")
        fi
        while read i n; do
            expr match "$i" "#" >/dev/null && continue || :
            [ -n "${devid}" ] && expr match "${devid}" "$i" >/dev/null && mount=/mnt/$n && break || :
            expr match "@${dev}" "$i" >/dev/null && mount=/mnt/$n && break || :
        done </etc/onl/block
        [ -n "${mount}" ]
        if [ "$(dd if=/dev/${dev}${part} bs=1 count=6 2>/dev/null)" = "070701" ]; then
            mount=${mount}-recover
        else
            mkdir -p ${mount}
            [ $(stat -c %D ${mount}) = $(stat -c %D /mnt) ]
            if [ "$(dd if=/dev/${dev}${part} bs=1 skip=82 count=5 2>/dev/null)" = "FAT32" ] \
                || [ "$(dd if=/dev/${dev}${part} bs=1 skip=54 count=5 2>/dev/null)" = "FAT16" ]; then
                if [ ! -e /etc/onl/kdump ]; then
                    # Try to repair FAT filesystem automatically to prevent further
                    # damage (but skip it in kdump to save time and memory)
                    if dosfsck -a /dev/${dev}${part} >/dev/null 2>&1; then
                        :
                    else
                        dosfsck -n /dev/${dev}${part} >/dev/null 2>&1
                    fi
                    dosfsck -a /dev/${dev}${part} >/dev/null 2>&1 || :
                fi
                mount -o flush,dirsync,noatime,umask=0007 /dev/${dev}${part} ${mount} 2>/dev/null
            else
                mount -o noatime /dev/${dev}${part} ${mount} 2>/dev/null
            fi
        fi
        echo -e "devid='${devid}'\ndev='${dev}'\npart='${part}'" >${mount}.conf
        ;;
    remove)
        mtab=$(mktemp /tmp/tmp.XXXXXX)
        cp /proc/mounts ${mtab}
        while read dev mount x; do
            if [ /dev/$1 = "${dev}" ]; then
                rm -f ${mount}.conf
                umount -l ${mount} 2>/dev/null || :
            fi
        done <${mtab}
        rm -f ${mtab}
        ;;
esac
