#!/bin/sh
#
### BEGIN INIT INFO
# Provides: USB-OTG
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start:     2 5
# Default-Stop:      0 1 6
### END INIT INFO

DEV="/dev/"
MMC=$(ls /dev/mmcblk*)
NUM_SD=$(lsblk | grep -v - | awk '{print $1}' | grep -v NAME | grep -c sd)
SD_LIST=$(lsblk | grep -v - | awk '{print $1}' | grep -v NAME | grep sd | column)
MMCX=""
SDX=""
COUNT=0
FILE=""
REV=""

if [ -n "${MMC}" ]; then
  for mmc_i in ${MMC}
  do
    len=${#mmc_i}                 # length of mmc_i
    if [ ${len} -eq "12" ]; then
      COUNT=$((COUNT+1))
      if [ -n "${MMCX}" ]; then
        MMCX=${MMCX},${mmc_i}
      else
        MMCX=${mmc_i}
      fi
    fi
  done
  FILE=${MMCX}
fi

if [ -n "${SD_LIST}" ]; then
  for sd_i in ${SD_LIST}
  do
    len=${#sd_i}  # length of sd_i
    if [ ${len} -eq "3" ]; then
      COUNT=$((COUNT+1))
      if [ -n "${SDX}" ]; then
        SDX=${SDX},$DEV$sd_i
      else
        SDX=$DEV$sd_i
      fi
    fi
  done
  FILE=${FILE},${SDX}
fi

co_i=0
while [ ${co_i} -lt ${COUNT} ]; do
  if [ -n "${REV}" ]; then
    REV=${REV},1
  else
    REV=1
  fi
  co_i=$((co_i+1))
done

case "$1" in
"start")
        # insert multimedia modules
        /sbin/modprobe -q -a configfs libcomposite
        /sbin/modprobe -q g_mass_storage file=${FILE} removable=${REV}
        ;;
stop)
        # remove multimedia modules
        /sbin/modprobe -q -r -a g_mass_storage libcomposite configfs
        ;;
reload|restart)
        $0 stop
        sleep 1
        $0 start
        ;;
*)
        echo "usage: $0 { start | stop | restart }"
        ;;
esac
