#!/bin/bash

MOUNT_FLAG=""
chk_rw_status() {
        # Get ext_fs from cmdline. e.g. sd=3
        ext_fs=$(cat /proc/cmdline)
        ext_fs="${ext_fs##*sd=}"
        ext_fs="${ext_fs%% *}"
        # Get true while setting storage write protect
        ext_ro=$((((ext_fs >> 1)) & 0x01));
        if [ x"$ext_ro" == x"1" ]; then
                MOUNT_FLAG=",ro"
        fi
}
auto_mount() {
	# Do not mount root partition automatically
	if [ ${sd_node}p1 == /dev/${1} ] || [ ${sd_node}p2 == /dev/${1} ] || [ ${sd_node}p3 == /dev/${1} ]; then
		return
	fi
	/bin/mkdir -p /media/sd-$1
	/bin/ln -s /media/sd-$1 /mnt/sd-$1
	/bin/mount -o $2$MOUNT_FLAG /dev/$1 /media/sd-$1
}

# Get rootfile system partition from cmdline. e.g. /dev/mmcblk0p2
root_part=$(cat /proc/cmdline)
root_part="${root_part##*root=}"
root_part="${root_part%% *}"

# Strip the partition number to fetch sd node e.g. /dev/mmcblk0
sd_node="${root_part%%p2}"

chk_rw_status
auto_mount $1 $2
