#!/bin/sh

start()
{
  echo "[makedev] Starting makedev for $TARGET..."
  # Check if started on target or on host
  MACHINE=`uname -m`
  if [ $MACHINE == "sh4" ]; then
    MAKEDEV="/sbin/MAKEDEV -p /etc/passwd -g /etc/group"
    HOST=`cat /etc/hostname`
  else
    # Expecting to be in /dev
    if [ ! -e ../etc/passwd ] || [ ! -e ../etc/group ]; then
	MAKEDEV="../sbin/MAKEDEV -p ../var/etc/passwd -g ../var/etc/group"
	HOST=`cat ../var/etc/hostname`
    else
	MAKEDEV="../sbin/MAKEDEV -p ../etc/passwd -g ../etc/group"
	HOST=`cat ../etc/hostname`
    fi
  fi

  echo "Target      : ${TARGET}"

  # Decide if we have CI and/or TWIN/TRIPLET
  CI="No"
  TUNER="1"
  for TARGET in cuberevo-9500hd cuberevo-mini2 cuberevo-mini cuberevo cuberevo-3000hd ufs922 ufs912 ufs913 ufc960 fs9000 hs7110 hs7420 hs7810a hs7119 hs7429 hs7819 hs8200 hs9510 hchs8100 hchs9000 ufs910 hl101 vip1_v1 vip1_v2 tf7700 ipbox9900 opt9600 opt9600prima opt9600mini; do
    if [ ${HOST} == ${TARGET} ]; then
      CI="Yes"
      break
    fi
  done

  for TARGET in adb_box cuberevo-9500hd cuberevo ufs922 ufs913 fs9000 hs8200 hchs8100 hchs9000 opt9600 opt9600prima vip2 tf7700 ipbox9900; do
    if [ ${HOST} == ${TARGET} ]; then
      TUNER="2"
      break
    fi
  done

  for TARGET in spark7162 sagemcom88 pace7241; do
    if [ ${HOST} == ${TARGET} ]; then
      TUNER="3"
      break
    fi
  done

  # Decide if we support HDMI CEC
  HDMI_CEC="No"
  for TARGET in adb_box arivalink200 hs7110 hs7420 hs7810a hs7119 hs7429 hs7819 hs8200 sagemcom88 spark spark7162 ufs912 ufs913 vitamin_hd5000; do
    if [ ${HOST} == ${TARGET} ]; then
      HDMI_CEC="Yes"
      break
    fi
  done

  echo " CI-slot(s) : ${CI}"
  echo " Tuners     : ${TUNER}"
  echo " HDMI CEC   : ${HDMI_CEC}"

  ${MAKEDEV} std
  ${MAKEDEV} fd
  ${MAKEDEV} hda hdb
  ${MAKEDEV} sda sdb sdc sdd
  ${MAKEDEV} scd0 scd1
  ${MAKEDEV} st0 st1
  ${MAKEDEV} sg
  ${MAKEDEV} ptyp ptyq
  ${MAKEDEV} console
  ${MAKEDEV} ttyAS0 ttyAS1 ttyAS2 ttyAS3
  ${MAKEDEV} lp par audio video fb rtc lirc st200 alsasnd
  ${MAKEDEV} ppp busmice
  ${MAKEDEV} input
  ${MAKEDEV} tundev
  ln -s -f /dev/input/mouse0 mouse
  ${MAKEDEV} i2c mtd

  ${MAKEDEV} dvb
  if [ ${CI} == "Yes" ]; then
    ${MAKEDEV} dvb_ci
    ${MAKEDEV} dvb_ca
  else
    ${MAKEDEV} dvb_ca_no
  fi
  if [ ${TUNER} == "2" ] || [ ${TUNER} == "3" ]; then
    ${MAKEDEV} dvb_2nd
    if [ ${CI} == "Yes" ]; then
      ${MAKEDEV} dvb_ca_2nd
    else
      ${MAKEDEV} dvb_ca_2nd_no
    fi
  fi
  if [ ${TUNER} == "3" ]; then
    ${MAKEDEV} dvb_3rd
    if [ ${CI} == "Yes" ]; then
      ${MAKEDEV} dvb_ca_3rd
    else
      ${MAKEDEV} dvb_ca_3rd_no
    fi
  fi

  ${MAKEDEV} mme bpamem
  ${MAKEDEV} vfd
  ${MAKEDEV} rc
  ${MAKEDEV} hdmi
  if [ ${HDMI_CEC} == "Yes" ]; then
    ${MAKEDEV} hdmi_cec
  fi
  ${MAKEDEV} ttyusb
  ${MAKEDEV} rfkill
  for TARGET in spark7162 ipbox-9900hd; do
    if [ "${HOST}" == "${TARGET}" ]; then
      ${MAKEDEV} rfmod
      break
    fi
  done
  if [ "${HOST}" == "fortis" ]; then
    ${MAKEDEV} sci_fortis
  else
    ${MAKEDEV} sci
  fi

  ##rm -rf pts
  mkdir pts
  ##chown root.root pts
  ##chmod 0755 pts
  ##rm -rf shm
  mkdir shm
  ##chown root.root shm
  ##chmod 0755 shm

  touch .devfsd

  # Additional box specific stuff
  for TARGET in spark spark7162; do
    if [ "${HOST}" == "${TARGET}" ]; then
      ${MAKEDEV} uinput
      break
    fi
  done

  echo "[makedev] done."
}

case "$1" in
  start)
    start
    ;;
  *)
    echo $"Usage: $0 {start}"
    RETVAL=0
esac
