#!/bin/csh -f
#!/bin/csh -fx


# Remove any leading components of the pathname.
set MYNAME = $0
set MYNAME = $MYNAME:t

# Check the names although wrong names should not happen.
if ( $MYNAME != pti.INSTALL && \
     $MYNAME != pti.LOAD && $MYNAME != pti.UNLOAD && \
     $MYNAME != pti.MKDEV && $MYNAME != pti.RMDEV ) then
  echo -n "Invocation names: pti.INSTALL pti.LOAD "
  echo    "pti.UNLOAD pti.MKDEV pti.RMDEV"
  exit 1
endif



# Check for superuser status.
touch /tmp/sutest$$
chown root /tmp/sutest$$ >& /dev/null
if ($status != 0) then
  echo "${MYNAME}: must be super user."
  rm -f /tmp/sutest$$
  exit 1
endif
rm -f /tmp/sutest$$


# Install or upgrade the loadable driver. It is presumed that at this
# point two files have been read into the current directory which may
# or may not be /dev. They are pti.INSTALL and pti.o. Both may be
# symbolic links to another location, such as /sys/sbusdev. 
#

if ($MYNAME == pti.INSTALL) then
  if ($cwd != /dev) then
    rm -f /dev/pti.INSTALL 
    rm -f /dev/pti.o

    ln -s $cwd/pti.INSTALL /dev/pti.INSTALL
    ln -s $cwd/pti.o /dev/pti.o

    cd /dev
    pti.INSTALL
    sync
    exit $status
  endif

  #
  # Check for an entry in /etc/rc.local to invoke /etc/loadable if 
  # /etc/loadable exists. If this entry does not exist, append it.
  # This single entry will serve all loadable modules. For safety,
  # we do not actually edit the /etc/rc.local file. /etc/rc.local
  # is too critical to the booting process to take chances with!!!
  #
  grep -s "/etc/loadable" /etc/rc.local
  if ($status == 1) then
    # no match and no error
    echo "#"        >> /etc/rc.local
    echo "# Load loadable modules."   >> /etc/rc.local
    echo "#"        >> /etc/rc.local
    echo "if [ -f /etc/loadable ]; then"  >> /etc/rc.local
    echo "  sh /etc/loadable > /dev/console" \
              >> /etc/rc.local
    echo "fi"       >> /etc/rc.local
    echo ""         >> /etc/rc.local
  endif
  sync        # for safety after writing /etc

  #
  # if it does not exist, create /etc/loadable for all to use.
  # Make it executable and flag it as a borne shell script to
  # allow calling by root as /etc/loadable.
  #
  if (! -e /etc/loadable) then
    echo ":" > /etc/loadable
    chmod u+x /etc/loadable
  endif
  sync        # for safety after writing /etc

  #
  # Make an entry in /etc/loadable to invoke /dev/pti.LOAD if 
  # /dev/pti.AUTOLOAD exists. 
  # A maximum of one such entry will be appended. Because this 
  # script can be run multiple times and we do not want to edit
  # the /etc/loadable file, the automatic loading of dmapt on reboot
  # is conditional on both /dev/dmapt.LOAD and /dev/dmapt.AUTOLOAD.
  #
  grep -s "PTI_DRIVER_INSTALLED" /etc/loadable
  if ($status == 1) then
    # no match and no error
    echo "#"        >> /etc/loadable
    echo "# Load the pti driver if desired." \
              >> /etc/loadable
    echo "#"        >> /etc/loadable
    echo "rm -f /dev/pti.ID"    >> /etc/loadable
    echo "if [ -f /dev/pti.LOAD ]; then" \
              >> /etc/loadable
    echo "  if [ -f /dev/pti.AUTOLOAD ]; then" \
              >> /etc/loadable
    echo -n "   csh /dev/pti.LOAD" \
              >> /etc/loadable
    echo "  # PTI_DRIVER_INSTALLED" >> /etc/loadable
    echo "  fi"       >> /etc/loadable
    echo "fi"       >> /etc/loadable
    echo ""         >> /etc/loadable
  
  endif
  sync        # for safety after writing /etc

  # rm -f /dev/pti.AUTOLOAD

  # Remove any previous versions of this script.
  rm -f /dev/pti.LOAD 
  rm -f /dev/pti.UNLOAD 
  rm -f /dev/pti.MKDEV 
  rm -f /dev/pti.RMDEV

  # Since /dev may be a different filesystem, copy pti.INSTALL to pti.LOAD.
  cp /dev/pti.INSTALL /dev/pti.LOAD

  # Make the rest of the links.
  ln /dev/pti.LOAD /dev/pti.UNLOAD
  ln /dev/pti.LOAD /dev/pti.MKDEV
  ln /dev/pti.LOAD /dev/pti.RMDEV

  # Do a pti.LOAD to load the driver and make the device nodes.
  /dev/pti.LOAD
  sync        # for safety after writing /dev
  exit $status
endif





#
# Handle invocation as pti.LOAD.
# Multiple invocations of pti.LOAD are intended to be benign.
#

if ($MYNAME == pti.LOAD) then
  #
  # Modload the driver, arranging for /dev/pti.MKDEV to be called.
  #
  echo -n "pti: "
  modload /dev/pti.o -entry _pti_vdcmd -exec /dev/pti.MKDEV 
  set exit_status = $status
  # clean up from modload
  # rm -f /dev/pti
  sync        # for safety after writing /dev
  exit $exit_status
endif



#
# Handle invocation as pti.UNLOAD.
# Multiple invocations of pti.UNLOAD are intended to be benign.
#
if ($MYNAME == pti.UNLOAD) then
  if ($#argv != 0) then
    echo "Usage: $MYNAME"
    exit 1
  endif
  modunload -id `cat /dev/pti.ID` 
  if ($status == 0) then          # remove /dev info after device
    	      /dev/pti.RMDEV        # unloaded successfully
  endif 
  sync        # for safety after writing /dev
  exit $status
endif



#
# If we got here, then I was invoked as pti.MKDEV or pti.RMDEV.
#

if ($MYNAME == pti.MKDEV) then
  if ($#argv != 4) then
    echo -n "Usage: $MYNAME "
    echo "<module_id> <module_type> <block_major> <char_major>"
    exit 1
  endif
endif


#
# At 4.0.3c modunload passes 4 args to the -exec file.
# At 4.1    modunload passes 2 args to the -exec file.
#
#if ($MYNAME == pti.RMDEV) then
# if ($#argv != 4 && $#argv != 2) then
#   echo -n "Usage: $MYNAME <module_id> <module_type> "
#   echo "[<block_major> <char_major>]"
#   exit 1
# endif
#endif

#
# Note that even though this is a hex number, no 0x is used in it...
#
if ($MYNAME == pti.MKDEV) then
  if ("$2" != "12345600") then
    echo "${MYNAME}: ${2}: bad module type."
    exit 1
  endif
endif


#
# Handle invocation as pti.MKDEV.
# Multiple invocations of pti.MKDEV are intended to be benign.
#
if ($MYNAME == pti.MKDEV) then
  set CHARMAJOR = $4
  set MODULE_ID = $1

  # If a previous pti driver was loaded, try to unload it.
  if (-f /dev/pti.ID) then
    set OLD_ID = `cat /dev/pti.ID`
    echo ${MYNAME}: A pti driver was already loaded.
    echo ${MYNAME}: Removing previous driver number $OLD_ID.
    /dev/pti.UNLOAD
    if ($status != 0) then
      echo -n "${MYNAME}: Unable to remove previous pti driver."
	    exit -1
    endif
  endif


  # Store the module ID for future use...
  echo $MODULE_ID >! /dev/pti.ID 

  # Make the device nodes...
	rm -f /dev/pti_clk /dev/pti_par /dev/pti_ctrl
	mknod /dev/pti_ctrl c $CHARMAJOR 0
	mknod /dev/pti_clk c $CHARMAJOR 1
	mknod /dev/pti_par c $CHARMAJOR 2
	chmod 666 /dev/pti_clk /dev/pti_par /dev/pti_ctrl

  sync        # for safety after writing /dev
  exit 0
endif



#
# Handle invocation as pti.RMDEV.
# Multiple invocations of pti.RMDEV are intended to be benign.
#
if ($MYNAME == pti.RMDEV) then
  rm -f /dev/pti.ID

  # Remove the device nodes...
  rm -f /dev/pti_clk /dev/pti_par

  sync        # for safety after writing /dev
  exit 0
endif

