#!/bin/sh

function clean()
{
  # remove device nodes
  rm -f /dev/dmx*

  # clean up /etc/modules.conf
  cp -f /etc/modules.conf /etc/modules.conf.bak
  sed -e '/dmxdev/d' < /etc/modules.conf.bak > /etc/modules.conf
}

function distclean()
{
  clean
  groupdel dmx
}

function install()
{
  # get minor numbers
  DMXROOT=..
  grep MINOR < $DMXROOT/config.mk > /tmp/$$.tmp
  source /tmp/$$.tmp
  rm -f /tmp/$$.tmp

  # clean old settting from /etc/modules.conf and /dev/dmx*
  clean

  #  add a new group for dmx activity
  groupadd dmx

  # create device nodes
  mknod /dev/dmx     c 10 ${DMXOUTMINOR}
  mknod /dev/dmxin   c 10 ${DMXINMINOR}
  chown root.dmx /dev/dmx /dev/dmxin
  chmod 660 /dev/dmx /dev/dmxin

  # fix /etc/modules.conf
  cp -f /etc/modules.conf /etc/modules.conf.old
  echo "alias char-major-10-${DMXOUTMINOR} dmxdev" >> /etc/modules.conf
  echo "alias char-major-10-${DMXINMINOR} dmxdev" >> /etc/modules.conf
}

if [ "$1" = "install" ]
then install
elif [ "$1" = "distclean" -o "$1" = "uninstall" ]
then distclean
else echo "usage: setup_devs install   - create device nodes in /dev"
     echo "    or setup_devs distclean - remove device nodes in /dev"
     exit 1
fi
