#!/bin/bash
#
# This shell script takes care of the ICEPIC driver - picdrv.  See icepic.hlp
#
#    added support for systemd
#
# description: make, installation, and init script for the ICEPIC driver
#
#   /sys/devices/system/node/online
#
#    added comment lines parsed by chkconfig for init management under
#    IRIX, late Solaris, and RedHat-based Linux. Suse uses INIT INFO block
# chkconfig: 345 05 05
#
### BEGIN INIT INFO
# Provides: icepic
# Required-Start:
# Required-Stop:
# X-United-Linux-Required-Start: $ALL
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Description: IcePic Card Drive Configuration at boot or hotplug
### END INIT INFO

# global strings
module="picdrv"
group="sys"
mode="666"
ostype="UNIX"
osname="LINX"

# current kernel version (Stew Weber's syntax)
VER=${VER:-`uname -r`}
#VER=`cat /proc/version | awk '{ print $3 }' `

# location for executables and optional icepic_pre, icepic_post, and icepic_check scripts
bindir="/usr/bin"
NRLIM=10
rbfile="/var/local/.icereboot"
unitfile="/etc/systemd/system/icepic.service"

# configuration options
chk=-1
smp=-1
kmv=-1
kam=-1
vma=2
new=1
mdr=0
mxd=12
lat=10
vbm=1
# devIce boot options
nvm=-1
nio=-1
ifs="fstab"
ios="none"

# key tray config from ICEOS or nvm>=0
if cat /etc/os-release | grep -q "ID=iceos" ; then
  nvm=0 
  ios="true"
  bindir="/etc/iceos"
fi

# DMA RAM setup default parameters (USE kernel boot parameters to over-ride)
#   ram_alloc = DMA RAM allocator {-1=Auto 0=Manual, 1=Kernel<4G, 2=Kernel>4G, 3=Kernel>ram_start, 4=PreKernel}
#   ram_start = IceDisk RAM Start in Mbytes (-1 or AUTO for top of the OS ram set by mem=xxxM in boot cmdline)
#   ram_sized = IceDisk RAM in Mbytes (-1 or AUTO for all memory available above mem=xxxM)
#   ram_sizem = Mappable RAM in Mbytes (at least 24 for PIC self tests)
#
ram_alloc=AUTO
ram_nodes=1
ram_start=-1
ram_sized=2048
ram_sizem=24

minrate=-1
maxrate=-1

# boot cmdline overrides
CMDMEM=0
CMDMAP="NONE"
CMDLN=`cat /proc/cmdline`
if [ "$1" == "test" ]; then 
  CMDLN=$2
elif [ -e /var/etc/icepic.conf ]; then
  CMDLN=`cat /var/etc/icepic.conf`
  echo "Using ICE driver settings from /var/etc/icepic.conf: $CMDLN"
elif [ -e /etc/icepic.conf ]; then
  CMDLN=`cat /etc/icepic.conf`
  echo "Using ICE driver settings from /etc/icepic.conf: $CMDLN"
fi
ARG2=$2
ARG3=$3
for arg in $CMDLN
do
  values=`echo $arg | sed s/.*=//`
  value=`echo $values | sed s/M.*//`
  # echo " Arg=$arg Values=$values Value=$value"
  case "$arg" in
    "mem="*)    CMDMEM=$value ;;
    "memmap="*) CMDMAP=$values ;;
    "ice.ra="*) ram_alloc=$value ;;
    "ice.rn="*) ram_nodes=$value ;;
    "ice.rs="*) ram_start=$value ;;
    "ice.rd="*) ram_sized=$value ;;
    "ice.rm="*) ram_sizem=$value ;;
    "ice.lat="*) lat=$value ;;
    "ice.nc="*) mdr=$value ;;
    "ice.mc="*) mxd=$value ;;
    "ice.nv="*) nvm=$value ;;
    "ice.nvm="*) nvm=$value ;;
    "ice.nio="*) nio=$value ;;
    "ice.fs="*) ifs=$value ;;
    "ice.os="*) ios=$value ;;
    "ice.vb="*) vbm=$value ;;
    "ice.minrate="*) minrate=$value ;;
    "ice.maxrate="*) maxrate=$value ;;
    "picdrv.ice_ra="*) ram_alloc=$value ;;
    "picdrv.ice_rn="*) ram_nodes=$value ;;
    "picdrv.ice_rs="*) ram_start=$value ;;
    "picdrv.ice_rd="*) ram_sized=$value ;;
    "picdrv.ice_rm="*) ram_sizem=$value ;;
    "picdrv.ice_lat="*) lat=$value ;;
  esac
done

# reinstall branch
if [ "$1" == "all" ]; then
  $0 stop
  $0 remove
  $0 make $2
  $0 install
  $0 start
  exit 0
fi

echo "Ice RAM Settings: Alloc=$ram_alloc Nodes=$ram_nodes Start=$ram_start Disk=$ram_sized Mappable=$ram_sizem"

# now convert all parameters to numeric
if [ "$ram_start" == "AUTO" ]; then ram_start=-1; fi
if [ "$ram_sized" == "AUTO" ]; then ram_sized=-1; fi
if [ "$ram_alloc" == "AUTO" ]; then ram_alloc=-1; fi
if [ "$ram_alloc" == "MANUAL" ]; then ram_alloc=0; fi
if [ "$ram_alloc" == "KERNEL" ]; then ram_alloc=1; fi

rbs=$ram_sized
if [ $ram_alloc = -1 ]; then
  if [ $ram_start = -1 ]; then
    ram_alloc=2;
  else
    ram_alloc=0;
  fi
fi

# auto modes cannot address below 4G or above 256G
aram_min=4096
aram_max=262144

# auto detection of manual mode options
if [ $ram_alloc = 0 ]; then
  # determine end of usable ram
  ram_end=$aram_min
  lines=`dmesg | grep BIOS-e820 | grep usable | sed 's/- 0/-0x0/' | sed 's/.*-0x/0x/' | cut -c 1-18 `
  for line in $lines
  do
    let addrM=($line+1)/1048576
    #echo $addrM
    if [ $addrM -gt $ram_end ]; then ram_end=$addrM ; fi
  done
  if [ $ram_end -gt $aram_max ]; then ram_end=$aram_max ; fi

  # process memmap=A$B syntax - make sure $ is in mapmem value
  maptst=`echo $CMDMAP | awk -F $ '{ print $1 }' ` 
  if [ $CMDMAP != $maptst ]; then
    mapsz=`echo $CMDMAP | awk -F $ '{ print $1 }' | sed s/M.*//` 
    mapst=`echo $CMDMAP | awk -F $ '{ print $2 }' | sed s/M.*//` 
    let aram_min=$mapst
    let aram_max=$mapst+mapsz
  fi

  # automatic ram start ?
  if [ $ram_start = -1 ]; then
    ram_start=$CMDMEM
    if [ $ram_start -lt $aram_min ]; then
      echo "icepic ram_start=-1 only valid for mem=xxxM when xxx >= $aram_min. Setting to $aram_min."
      ram_start=$aram_min
    fi
  fi

  #automatic ram size ?
  if [ $ram_sized = -1 ]; then
    let ram_sized=$ram_end-$ram_sizem-$ram_start
  fi
  let ram_stop=$ram_start+$ram_sized-$ram_sizem
  if [ $ram_stop -gt $aram_max ]; then
    let ram_sized=$aram_max-$ram_sizem-$ram_start
    echo "Limiting ram_sized to $ram_sized to keep under $aram_max limit"
  fi
fi
  
if [ "$1" == "test" ]; then
  echo "Ice RAM Settings: Alloc=$ram_alloc Start=$ram_start Disk=$ram_sized Mappable=$ram_sizem"
  exit 0
fi

numa=1
if [ $ram_nodes -gt 1 ]; then
  nuno=`cat /sys/devices/system/node/online`
  if [ "$nuno" == "0-1" ]; then numa=2; fi
  if [ "$nuno" == "0-3" ]; then numa=4; fi
  if [ "$nuno" == "0-7" ]; then numa=8; fi
  if [ $ram_nodes -gt $numa ]; then
    echo "Asking for $ram_nodes memory nodes with only $numa NUMA nodes present. Aliasing entries across available NUMA nodes."
  fi
fi

if [ -d /etc/init.d ]; then 
  initd=init.d
  rcd=
else
  initd=rc.d/init.d
  rcd=rc.d
fi

if [ $new = 1 ]; then
  kmv=1
fi

if [ $smp = -1 ]; then
  if echo `uname -a` | grep -q SMP ; then
    smp=1
  else
    smp=0
  fi
fi

if [ $kmv = -1 ]; then
  if ksyms -a | grep pci_devices | grep -q _R ; then
    kmv=1
  else
    kmv=0
  fi
fi

if [ $kam = -1 ]; then
  if [ -f icekam.c ]; then
    kam=1
  elif [ -f icekam.cx ]; then
    kam=2
  else
    kam=0
  fi
fi

if [ $vma = -1 ]; then
  vma=1
fi

if [ $chk = -1 ]; then
  sysd=`pidof -s systemd`
  if [ "$sysd" != "" ]; then
    chk=3
  elif hash chkconfig 2> /dev/null; then
    chk=1
  elif hash update-rc.d 2> /dev/null; then
    chk=2
  else 
    chk=0
  fi
fi

# check to insure systemd start|stop are from the unit file
if [ "$1" == "start" -o "$1" == "stop" ]; then
  if [ $chk = 3 -a "$2" != "1" ]; then
    echo "Using 'systemctl start|stop icepic'"
    systemctl $1 icepic
    exit 1
  fi
fi

# reboot file counter incrementer 
rbinc() {
  nrebs=0
  # check if reboot file exists
  if [[ -f $rbfile ]]; then
    # read the file, check for consecutive # of reboots
    while read line
    do  
      # look for the number of reboots
      if [[ $line == NR=* ]]; then
        nrebs=${line:3}
        break
      fi  
    done < "$rbfile"
    if (( $nrebs < 0 )) || (( $nrebs > $NRLIM )); then
      nrebs=$NRLIM
    fi
  fi  

  # echo number of reboots into the reboot file
  let "nrebs += 1"
  printf "NR=$nrebs\n" > $rbfile
  sync
}

# function to get current number of devices
getdevices() {
  export ndevs=0
  if [ -e /proc/driver/icepic ]; then 
    ndevs=`cat /proc/driver/icepic | grep Devices | awk '{ print $3 }'` 
  fi
  export devlist=""
  for i in {0..11} ; do
    if [ $i -lt $ndevs ]; then devlist="$devlist $i"; fi
  done
}

getactive() {
  export nactive=0
  if [ $ndevs -gt 0 ]; then
  for i in $devlist ; do
    dline=`cat /proc/driver/icepic | grep Device#$i` 
    nact=${dline#*Active=}
    let nactive=$nactive+$nact
  done
  fi
}

# function to check for ICEROOT env variable
geticeroot() {
  # check if variable exists
  if [[ -z $ICEROOT ]]; then
    cdir=$(pwd)
    # look at cwd, then look at some "common" iceroot dirs
    cwdx=$(echo $cdir | awk -F /icexxx '{ printf $1 }')
    if [[ -f $bindir/icepic_root ]]; then
      . $bindir/icepic_root
    elif [[ $cwdx != $cdir ]]; then
      export ICEROOT=$cwdx/icexxx
    else
      for path in /opt/icexxx /opt/ice/pkg/icexxx /user/opt/icexxx ; do
        if [[ -d $path ]]; then export ICEROOT=$path ; fi
      done
    fi
  fi
}

make_drv() {
  cd $ICEROOT/drv/lnx
  echo "Making ICE driver for SymMultiProc=$smp ModVer=$kmv NewVma=$vma New=$new"
  echo "/* Defines generated by './icepic make' - do NOT edit */" > picdrv.h
  echo "#define RAM_START $ram_start" >> picdrv.h
  echo "#define RAM_SIZED $ram_sized" >> picdrv.h
  echo "#define RAM_SIZEM $ram_sizem" >> picdrv.h
  echo "#define _$ostype 1" >> picdrv.h
  echo "#define _$osname 1" >> picdrv.h
  echo "#define _NEW $new" >> picdrv.h
  echo "#define _VMA $vma" >> picdrv.h
  echo "#define _KAM $kam" >> picdrv.h
  if [ $new != 0 ]; then
    unset ARCH
    echo "Used system Makefile for new=1"
    echo "obj-m := picdrv.o" > Makefile
    if [ "$ARG2" == "bb" ]; then
      echo "Making ALL binary blob from original sources"
      echo "#define _ALL 1" >> picdrv.h
      echo "#define _KAM 1" >> picdrv.h
    elif [ "$ARG2" == "ifs" ]; then
      echo "Making ALL binary blob from obfuscated source icekbfs.cx"
      echo "#define _ALLOBF 1" >> picdrv.h
    elif [ "$ARG2" != "" ]; then
      echo "Unzipping precompiled binary blob for ver=$ARG2"
      tar -xzf picdrv_$ARG2.tz
      echo "picdrv-y := picdrv_bb.o" >> Makefile
      for fn in icekam.c icebd.c icefs.c ; do
        if [ ! -f $fn ]; then cp dummy.c $fn ; fi
      done
    fi

    make -C /lib/modules/$VER/build M=$(pwd)
    if [ "$ARG2" == "bb" ]; then
      cp picdrv.o picdrv_bb.o_shipped
      cp .picdrv.o.cmd .picdrv_bb.o.cmd
      tar -czf picdrv.tz picdrv_bb.o_shipped .picdrv_bb.o.cmd 
      if [ "$ARG3" != "" ]; then mv picdrv.tz picdrv_$ARG3.tz ; fi
      rm picdrv_bb.o* 
    elif [ "$ARG2" == "ifs" ]; then
      echo ""
    elif [ "$ARG2" != "" ]; then
      for fn in icekam.c icebd.c icefs.c ; do
        ans=`diff -sq $fn dummy.c | grep ident`; 
	if [ "$ans" != "" ]; then rm -f $fn ; fi
      done
    fi
  else
    incdir="/lib/modules/$VER/build/include"
    if [ ! -d $incdir ]; then
      incdir="/usr/src/linux/include"
    fi
    INCLUDES="-I$incdir -I/usr/include -I../../inc"
    FLAGS="-D__KERNEL__ -DMODULE -DEXPORT_SYMTAB"
    if [ $kmv != 0 ]; then
      INCLUDES="$INCLUDES -include $incdir/linux/modversions.h"
      FLAGS="$FLAGS -DMODVERSIONS"
    fi
    if [ $smp != 0 ]; then
      FLAGS="$FLAGS -D__SMP__"
    fi
    if echo `uname -m` | grep -q x86_64 ; then
      FLAGS="$FLAGS -mcmodel=kernel -mno-red-zone"
    fi
    gcc -c picdrv.c $FLAGS -O -Wall $INCLUDES 
    echo "For 2.6+ kernels, set the new=1 flags in this script"
  fi
}

make_other() {
  cd $ICEROOT/drv/lnx
  if [ -f iceqos.c ] ; then cc -o iceqos -D_$ostype -D_$osname iceqos.c ; fi
  cc -o setram -D_$ostype -D_$osname -I../../inc ../../test/setram.c 
  cc -o test   -D_$ostype -D_$osname -I../../inc ../../test/test.c ../../lib/icelib.c -lm -pthread -ldl
  cc -o ipmice -D_$ostype -D_$osname -I../../inc ../../test/ipmice.c ../../lib/icelib.c -lm -pthread -ldl
}

install_drv() {
  cd $ICEROOT/drv/lnx
  echo "Installing ICE driver for kernel build=$VER chk=$chk"
  # make getroot file
  rm -f getroot
  echo '#!/bin/sh' > getroot
  echo "export ICEROOT=$ICEROOT" >> getroot 
  chmod +x getroot
  # install modules
  if [ $new != 0 ]; then
    if [ ! -d /lib/modules/$VER/build ]; then echo "No /lib/modules/$VER/build found for install"; exit; fi
    make -C /lib/modules/$VER/build M=$(pwd) modules_install
#    install -c picdrv.ko /lib/modules/$VER/updates
  else
    install -d /lib/modules/$VER/misc 
    install -c picdrv.o /lib/modules/$VER/misc
  fi
  install -c getroot $bindir/icepic_root
  install -c test $bindir/icepic_test
  install -c setram $bindir/icepic_setram
  install -c check $bindir/icepic_check
  install -c icepic $bindir/icepic_script
  if [ $chk = 3 ]; then
    install -m 644 icepic.service $unitfile
    systemctl daemon-reload
    echo "Using systemctl enable"
    systemctl enable icepic.service
  else
    install -c icepic /etc/$initd/icepic
    if [ $chk = 1 ]; then
      echo "Using chkconfig utility"
      chkconfig --add icepic
    elif [ $chk = 2 ]; then
      echo "Using update-rc.d utility"
      update-rc.d icepic start 05 3 4 5 . stop 05 0 1 2 6 .
    else
      ln -sf /etc/$initd/icepic /etc/$rcd/rc5.d/S05icepic
      ln -sf /etc/$initd/icepic /etc/$rcd/rc4.d/S05icepic
      ln -sf /etc/$initd/icepic /etc/$rcd/rc3.d/S05icepic
    fi
  fi
  depmod -a
  if [ ! -d /etc/iceos ]; then 
    mkdir -pm 755 /etc/iceos
  fi
  if [ "$ios" = "mimic" ]; then
    echo "Copying IceOS scripts to /etc/iceos/"
    cp -p $ICEROOT/os/* /etc/iceos/
  fi
}

remove_drv() {
  if [ $new != 0 -a ! -d /lib/modules/$VER/build ]; then
    echo "Cannot remove driver. No /lib/modules/$VER/build found for reinstall"
    exit
  fi
  if [ -e /dev/icepic0 ]; then
    stop_drv
  fi
  if [ $chk = 3 ]; then
    echo "Using systemctl disable"
    systemctl disable icepic.service
    rm -f $unitfile
    rm -f $bindir/icepic
  else
    if [ $chk = 1 ]; then
      echo "Using chkconfig utility"
      chkconfig --del icepic
    elif [ $chk = 2 ]; then
      echo "Using update-rc.d utility"
      update-rc.d -f icepic remove
    else
      rm -f /etc/$initd/icepic
      rm -f /etc/$rcd/rc3.d/S05icepic
      rm -f /etc/$rcd/rc4.d/S05icepic
      rm -f /etc/$rcd/rc5.d/S05icepic
      rm -f /etc/pcmcia/icepic
    fi
    rm -f /etc/$rcd/rc*.d/K*icepic
    rm -f /etc/$rcd/rc*.d/S*icepic
  fi
  # previous install files
  rm -f /dev/pmem
  rm -f /lib/modules/$VER/*/picdrv.ko
  # remove icepic bindir files
  rm -f $bindir/icepic_*
}

start_drv() {
  if [ $mxd -lt 0 ]; then exit 0 ; fi

  # remove stale nodes
  rm -f /dev/icepic*
  rm -f /dev/iceram*

  # prep for possible iceblk storage
  mkdir -p /var/preserve

  # load driver module
  if [ $ram_alloc -le 3 ]; then
    rmmod $module 2>/dev/null
    echo "ICEPIC: $module ice_ra=$ram_alloc ice_rs=$ram_start ice_rd=$ram_sized ice_rm=$ram_sizem ice_lat=$lat ice_nvm=$nvm ice_vb=$vbm"
    modprobe $module ice_ra=$ram_alloc ice_rs=$ram_start ice_rd=$ram_sized ice_rm=$ram_sizem ice_lat=$lat ice_mc=$mxd ice_rb=$rbs ice_nvm=$nvm ice_vb=$vbm
  fi

  # extract dynamic major number
  major=`cat /proc/devices | grep icepic | awk '{print $1}'`
  if [ "$major" = "" ]; then
    echo "No icepic driver found"
    exit 1
  fi

  # extract number of devices
  getdevices

  # increment the reboot counter
  rbinc
  # auto reboot if minimum devices not found
  if [[ $ndevs -lt $mdr && $nrebs -le $NRLIM ]]; then
    echo "Executing warm boot to pick up $mdr cards"
    sleep 2
    reboot
    exit 1
  fi

  # create new icepic nodes 
  if [ $ndevs -gt 0 ] ; then
    for i in $devlist ; do
      mknod /dev/icepic$i c $major $i
    done
    chgrp $group /dev/icepic*
    chmod $mode /dev/icepic*
  fi

  # create nodes for access to driver ram
  rmajor=`cat /proc/devices | grep iceram | awk '{print $1}'`
  if [ "$rmajor" = "" ]; then
    ln -sf icepic0 /dev/pmem
  else
    nrams=$ram_nodes
    mknod /dev/iceram c $rmajor 0
    mknod /dev/pmem c $rmajor 0
    for i in {0..8} ; do  
      if [ $i -lt $nrams ]; then mknod /dev/iceram$i c $rmajor $i ; fi
    done
    chgrp $group /dev/iceram*
    chmod $mode /dev/iceram*
    chmod $mode /dev/pmem
  fi

  # the block access devices are made internally by the driver code as /dev/iceramb
  bmajor=`cat /proc/devices | grep icebd | awk '{print $1}'`

  # start PSM daemon
  if [ -x /sbin/psm_iced ]; then /sbin/psm_iced 0 ; fi

  # initialize device mode of icepic0 and network ports if ice.nio!=0
  if [[ $nio -ge 0 && -f $bindir/icepic_test && $ndevs -gt 0 ]]; then
    # nothing else should be touching card so dont risk a stuck lock in init
    $bindir/icepic_test pic 0 niop=$nio "NOLOCK"
  fi

  # initialize device mode of icepic0 and attached nvme drives if ice.nv!=0
  if [[ $nvm -ge 0 && -f $bindir/icepic_test && $ndevs -gt 0 ]]; then
    # nothing else should be touching card so dont risk a stuck lock in init
    $bindir/icepic_test pic 0 nvme=$nvm "PM=K8M,VERBOSE=0x10,NOLOCK"
    partprobe
  fi

  # mount icefs auto disks 
  if [ "$ifs" = "auto" -o "$ifs" = "AUTO" ]; then
    # check if the word icefs is in the fstab but not commented out
    if cat /etc/fstab | grep -w icefs | grep -qv '#' ; then
      mount -a -t icefs
    elif [ -b /dev/iceblk0n1p1 ]; then
      mount -t icefs /dev/iceblk0n1p1 /mnt/icefs
    elif [ -b /dev/nvme0n1p1 ]; then
      mount -t icefs /dev/nvme0n1p1 /mnt/icefs
    else
      echo "No drives found with ice.fs=auto "
    fi
  fi
  # mount icefs fstab configured disks
  if [ "$ifs" = "fstab" -o "$ifs" = "FSTAB" ]; then
    mount -a -t icefs
  fi
  # check arduino configuration 
  if [ "$ios" = "mimic" ]; then
    /etc/iceos/checkArduino 1
    /etc/iceos/checkReboot "Arduino"
    if [ -e /var/etc/asserts.conf ]; then
      /etc/iceos/checkAsserts /var/etc/asserts.conf
      /etc/iceos/checkReboot "Devices"
    fi
  fi

  # look to run check script
  if [[ $mdr -gt 0 && $nrebs -le $NRLIM ]]; then
    if [[ -f $bindir/icepic_check && -f $bindir/icepic_test && -n $minrate && -n $maxrate ]]; then
      # run script
      $bindir/icepic_check $bindir/icepic_test $mdr $minrate $maxrate
      rv=$?
      if (( $rv != 0 )); then
        # give the user time to see what failed
        echo "Rebooting, attempt $nrebs out of $NRLIM"
        sleep 2
        # reboot and throw 
        reboot
        exit 1
      fi
    fi
    # print (success) status into the reboot file
    printf "$(date)\nSuccesfully booted at attempt $nrebs\nNumber of cards: $mdr\nMin Rate: $minrate\nMax Rate: $maxrate\n" > $rbfile
    sync
  fi
}

stop_drv() {
  if [ $mxd -lt 0 ]; then exit 0 ; fi
  if [ $nactive -gt 0 ]; then
    echo "There are $nactive active DMAs running on PIC cards.  Driver will not stop."
    exit 1
  fi
  if [[ -d /mnt/icefs/.etc  && -d /var/preserve ]]; then
    if [[ $nvm -ge 0 && -f $bindir/icepic_test && $ndevs -gt 0 ]]; then
      $bindir/icepic_test pic 0 reset "PM=K8M,VERBOSE=0x10,NOLOCK"
      sleep 1
    fi
    dd if=/mnt/icefs/.etc/sb.prm of=/var/preserve/sb.prm bs=16384 iflag=direct
    dd if=/mnt/icefs/.etc/sb.det of=/var/preserve/sb.det bs=16384 iflag=direct
  fi
  # unmount icefs disks 
  umount -a -t icefs
  sleep 1
  # unload driver modules
  rmmod $module
  # remove stale nodes
  rm -f /dev/icepic*
  rm -f /dev/iceram*
  if [ "$ios" = "mimic" ]; then
    /etc/iceos/checkArduino 0
  fi
}

scan_drv() {
  pci_ids=`ls /sys/bus/pci/devices`
  for id in $pci_ids
  do
    device=`cat /sys/bus/pci/devices/$id/device`
    if [ $device = "0x7777" ]; then
      echo "Removing icepic at pci id=$id $device"
      echo 1 >/sys/bus/pci/devices/$id/remove
    fi
  done
  echo "Rescanning pci bus"
  echo 1 >/sys/bus/pci/rescan
}

# run pre script if it exists
if [ -f $bindir/icepic_pre ]; then $bindir/icepic_pre $1 ; fi

geticeroot
getdevices
getactive

case "$1" in
  make)
	make_drv
	make_other
	;;
  maked)
	make_drv
	;;
  install)
	install_drv
	;;
  remove)
	remove_drv
	;;
  start)
	start_drv
	;;
  startd)
	vbm=2
	start_drv
	;;
  stop)
	stop_drv
	;;
  stopd)
	stop_drv
	;;
  restart)
	stop_drv
	start_drv
	;;
  rescan)
	stop_drv
	scan_drv
	start_drv
	;;
  reinstall)
	stop_drv
	install_drv
	start_drv
	;;
  numa)
	cat /sys/devices/system/node/online
	if [ -f /usr/bin/numactl ]; then numactl -H ; fi
	;;
  status)
	cat /proc/driver/icepic
	;;
  clear)
	# reset RAM allocation
	$bindir/icepic_setram $ram_start $ram_sized $ram_sizem
	;;
  clean)
	rm -rf .*
	rm -rf *.o 
	rm -rf modules.* *.mod* 
	;;
  check)
	;;
  help)
	cat icepic.hlp
	;;
  liveboot)
	make_drv
	install_drv
	start_drv
	;;
  grub2)
	vi /etc/default/grub
	grub2-mkconfig -o /boot/grub2/grub.cfg
	;;
  mount)
	mount -a -t icefs
	;;
  *)
	echo "Usage: icepic {help|make|install|remove|start|stop|restart|status}"
	exit 1
    ;;
esac

# run post script if it exists
if [ -f $bindir/icepic_post ]; then $bindir/icepic_post $1 ; fi

exit 0
