#!/bin/sh
#------------------------------------------------------------------
#
# mountfs - Early filesystem mount setup
#
# Feb 2016 Joel Aller - Move/ported from S10boot
#
# Copyright (c) 2016 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------
#set -x

#
# Mount firmware volume as UBIFS.  The filesystem can be use to store binary
# firmwares of phy, radio, etc.
#
# Platform dependent calls can optionally pass the ubi volume
#
# $1 - Volume (default: /dev/ubivol/firmware)
#

. /usr/cisco/bin/ciscosetup.sh

check_ubifs_volume()
{
    local volume=/dev/ubivol/$1
    local ubifs_image=/lib/firmware/base-$1-ubifs.img
    local mount_dir=/tmp/ubifs/$1

    if [ -e $volume ]; then
        mkdir -p $mount_dir
        mount -t ubifs $volume $mount_dir >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            umount -f $mount_dir >/dev/null 2>&1
        elif [ -e ${ubifs_image}.bz2 ]; then
            echo "$volume is not UBIFS, initialize filesystems."
            bunzip2 ${ubifs_image}.bz2
            ubiupdatevol $volume $ubifs_image
            mount -t ubifs $volume $mount_dir >/dev/null 2>&1
            [ $? -eq 0 ] && umount -f $mount_dir >/dev/null 2>&1
            rm -f $ubifs_image
        fi
        rm -rf $mount_dir
    fi
}

mount_firmware() {
    local volume=${1:-"/dev/ubivol/firmware"}

    # Check if partition/volume exist
    if [ -e $volume ]; then 
        mkdir -p /firmware
        mount -t ubifs $volume /firmware
    else
        mkdir -p /storage/firmware
        ln -s /storage/firmware /firmware
    fi
}


if [ "$MERAKI_BOARD" == "barbados" ]; then
    mount none /proc -t proc 2> /dev/null
    grep -q devtmpfs /proc/filesystems && \
        mount -t devtmpfs none /dev 2> /dev/null
    mkdir -p /dev/pts
    mount none /dev/pts -t devpts 2> /dev/null
    mount -t sysfs none /sys 2> /dev/null
    grep -q debugfs /proc/filesystems && \
    mount -t debugfs none /sys/kernel/debug 2> /dev/null

    mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=100M 2> /dev/null
    mkdir -p /dev/shm
    mount none /dev/shm -t tmpfs -o nosuid,nodev,mode=1777,size=1M
    #Barbados mount firmware
    #Mount NAND UBIFS /firmware
    mount_firmware
elif [ "$MERAKI_BOARD" == "corsica" -o "$MERAKI_BOARD" == "mallorca" -o \
       "$MERAKI_BOARD" == "axel-qca" -o "$MERAKI_BOARD" == "entr17" ]; then

    mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=80M
    if ! grep -q pts /proc/mounts ; then
        mkdir -p /dev/pts
        mount none /dev/pts -t devpts
    fi
    grep -q debugfs /proc/filesystems && mount -t debugfs none /sys/kernel/debug

    if [ "$MERAKI_BOARD" == "mallorca" -o "$MERAKI_BOARD" == "axel-qca" -o \
         "$MERAKI_BOARD" == "entr17" ]; then
        mkdir -p /pstore
        mount none /pstore -t pstore
    fi
    mkdir -p /dev/shm
    mount none /dev/shm -t tmpfs -o nosuid,nodev,mode=1777,size=1M

elif [ "$MERAKI_BOARD" == "apvirtual" ]; then
    if [ "$MODE" != "docker" ]; then
        mkdir -p /dev/pts
        mount -t devpts devpts /dev/pts
        mkdir -p /dev/shm
        mount none /dev/shm -t tmpfs -o nosuid,nodev,mode=1777,size=1M
    fi

elif [ "$MERAKI_BOARD" == "axel-bcm" ]; then
    mount none /proc -t proc 2> /dev/null
    grep -q devtmpfs /proc/filesystems && \
                                 mount -t devtmpfs none /dev 2> /dev/null
    mkdir -p /dev/pts
    mount none /dev/pts -t devpts 2> /dev/null
    mount -t sysfs none /sys 2> /dev/null
    grep -q debugfs /proc/filesystems && \
                      mount -t debugfs none /sys/kernel/debug 2> /dev/null
    mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=100M 2> /dev/null
    mkdir -p /firmwarelog
    mount none /firmwarelog -t tmpfs -o mode=1777,size=10M  2> /dev/null
    mkdir -p /dev/shm
    mount none /dev/shm -t tmpfs -o nosuid,nodev,mode=1777,size=1M
    #
    # UBI volume firmware,ewlc,config no longer exist with new part1/part2 scheme
    #
    # check_ubifs_volume firmware
    # check_ubifs_volume ewlc
    # check_ubifs_volume config
    mount_firmware

else
    mkdir -p /dev/pts
    mount none /dev/pts -t devpts
    mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=6M
    grep -q debugfs /proc/filesystems && mount -t debugfs none /sys/kernel/debug
    grep -q usbfs /proc/filesystems && mount -t usbfs usbfs /proc/bus/usb
fi

cat /proc/uptime > /MERAKI_START

if [[ -d /var -a ! -L /var ]];then
  mv /var /tmp && ln -s /tmp/var/ /var
fi
mkdir -p /var/lib/selinux/targeted /var/log/audit /var/log/si /var/run \
	 /var/lock /var/tmp /var/empty /var/tmp/systemd /var/log/wsa \
	 /var/log/pnp

#grep -q selinuxfs /proc/filesystems && \
#    mount -t selinuxfs none /sys/fs/selinux 2> /dev/null

ifconfig lo 127.0.0.1 netmask 255.255.255.255 up

rm -r -f /storage/log
mkdir -p /storage/lists
mkdir -p /storage/cores
mkdir /tmp/connection
chmod 1777 /var/run /var/log /var/tmp
chmod 755 /var/empty
mkdir -p /var/log/wsa /var/lib/selinux/targeted
touch /var/log/wtmp /var/log/lastlog /var/log/pnp/pnp.log


#systemd env file creation
SYSTEMD_FILES="wcpd hostapd capwapd cleanair sensord grpc mrvlfwd brain\
 aptraced dhcpv6 dnsmasq fastcgi klogd synclogd lighttpd\
 rtd wbridged printkd"
for file in $SYSTEMD_FILES; do
	touch /var/tmp/systemd/$file.env
done
