#!/bin/sh -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Silently exit if the kernel does not support modules or needs modutils.
[ -f /proc/modules ] || exit 0
[ ! -f /proc/ksyms ] || exit 0
[ -x /sbin/modprobe  ] || exit 0

KVER=$(uname -r)
KMAJ=${KVER%${KVER#*.*[^.]}}
KMAJ=${KMAJ%.}

#adding a check to load EXAR module only for Vespa Platform..
PLATFORM=$(cat /proc/cmdline | awk 'BEGIN{FS="platformid="}{print $NF}')

if [ -w /lib/modules/$KVER/ ]; then
  if [ -x /sbin/depmod  ] 
  then
  	echo -n "Calculating module dependencies... "
  	depmod -n > /var/modules.dep
	ln -sf /var/modules.dep /lib/modules/$KVER/modules.dep
  	echo "done."
  fi
else
  echo "Not running depmod because /lib/modules/$KVER/ is not writeable."
fi

#if [ -e /etc/modules-$KVER ]; then
#  MODULES_FILE=/etc/modules-$KVER
#elif [ -e /etc/modules-$KMAJ ]; then
#  MODULES_FILE=/etc/modules-$KMAJ
#else
  MODULES_FILE=/etc/modules
#fi

# Loop over every line in /etc/modules.
echo 'Loading modules...'
grep '^[^#]' $MODULES_FILE | \
while read module args; do
  [ "$module" ] || continue
  echo "Loading Module $module ..."
if [ $PLATFORM != "Vespa" -a $PLATFORM != "Helix" -a $PLATFORM != "Zuma" -a $module != "exaruart" ]; then
    if [ $module == "enet_switch" ]; then
        continue;
    fi
	if [ $module == "gpio" ]; then
		 # This string "str_platformID" is passed to GPIO driver for platform detection(ie) module_param(str_platformID, charp,0000)
		 modprobe $module $args str_platformID=$PLATFORM || true	
	else 
	 	 modprobe $module $args || true
	fi
elif [ $PLATFORM == "Vespa" ]; then
    if [ $module == "enet_switch" ]; then
        continue;
    fi
	if [ $module == "gpio" ]; then
		 # This string "str_platformID" is passed to GPIO driver for platform detection (ie) module_param(str_platformID, charp,0000)
		 modprobe $module $args str_platformID=$PLATFORM || true   	
	else 
	 	 modprobe $module $args || true
	fi
elif [ $PLATFORM == "Helix" -o $PLATFORM == "Zuma" ]; then
    if [ $module == "switch-vespa" ]; then
        continue;
    fi
	if [ $module == "gpio" ]; then
		 # This string "str_platformID" is passed to GPIO driver for platform detection (ie) module_param(str_platformID, charp,0000)
		 modprobe $module $args str_platformID=$PLATFORM || true   	
	else 
	 	 modprobe $module $args || true
	fi
fi
#  insmod $module $args || true
done
echo "All modules loaded."

# Just in case a sysadmin prefers generic symbolic links in
# /lib/modules/boot for boot time modules we will load these modules.
#boot="$(modprobe --list --type boot)"
#for d in $boot; do
#    mod="${d##*/}"
#    mod="${mod%.ko}"
#    modprobe "$mod"
#done

if [ -r /etc/modprobe.conf ] \
	&& ! grep -q '^include.*modprobe.d' /etc/modprobe.conf; then
   echo 'WARNING: /etc/modprobe.conf exists but does not include /etc/modprobe.d/!'
fi

exit 0

