#!/bin/sh

TMPCFG=.config.h
FINALCFG=config.h

[ -z "$BASH" ] && { echo "syscheck requires bash" 1>&2; exit 1; }

function header () {
  echo "/*" > $TMPCFG
  echo " *  Configuration file for compilation of the GUS driver" >> $TMPCFG
  echo " *  Generated by syscheck script..." >> $TMPCFG
  echo " */" >> $TMPCFG
  echo >> $TMPCFG
} 

function readln () {
  echo -n "$1"
  IFS='@' read ans </dev/tty || exit 1
  if [ -z $ans ]; then
    ans=$2
  fi
}

function value8 () {
  old=$[ $[ $3 + 1 ] - 1 ]
  while :; do
    readln "drv: $2 ($1) [$old] " "$old"
    if ( expr "$ans" ">" 0 > /dev/null ) && \
       ( expr "$ans" "<" 256 > /dev/null ) ; then
      echo "#define $1 $ans" >> $TMPCFG
      break
    fi
  done
}

function bool () {
  case "$3" in
    "y") defprompt="Y/n" ;;
    "n") defprompt="N/y" ;;
  esac
  while :; do
    readln "drv: $2 ($1) [$defprompt] " "$3"
    case "$ans" in
      [yY] | [yY]es )
         echo "#define $1" >> $TMPCFG
         bool_result="y"
         break;;
      [nN] | [nN]o )
         echo "#undef $1" >> $TMPCFG
         bool_result="n"
         break;;
    esac
  done
}

function bool1 () {
  case "$2" in
    "y") defprompt="Y/n" ;;
    "n") defprompt="N/y" ;;
  esac
  while :; do
    readln "drv: $1 [$defprompt] " "$2"
    case "$ans" in
      [yY] | [yY]es )
         bool_result="y"
         break;;
      [nN] | [nN]o )
         bool_result="n"
         break;;
    esac
  done
}

function comment () {
  echo "     $1"
}

echo -n "driver: "

RESULT=n
if [ -r /usr/include/linux/kernel.h ] &&
   [ -r /usr/include/linux/soundcard.h ] &&
   [ -r /usr/include/linux/ultrasound.h ]; then
  RESULT=y
fi  

if [ "$RESULT" = "n" ]; then
  echo "Kernel header files not found..."
  exit 1
fi

if [ -r ./config.h ]; then
  echo "Configuration ok... Remove src/driver/config.h file for new one.."
else
  echo "Configuration requested..."
  header
  comment ""
  comment "Major device number for is used for communication between driver and"
  comment "application. OSS (Lite) have number 14 (which is default for this"
  comment "driver). In special cases if you want use both drivers (OSS and GUS)"
  comment "at one time, you can choose different value (60 isn't bad)."
  comment ""
  value8 GUSCFG_MAJOR "Major device number for the driver (1-255)?" 14
  bool GUSCFG_ALL "Support for all Gravis UltraSound cards?" y
  if [ "$bool_result" = "n" ]; then
    bool GUSCFG_GUSSTD "Do you have GUS Classic/ACE card?" n
    if [ "$bool_result" = "y" ]; then
      bool GUSCFG_GUSDB16 "Do you have 16-bit Daughter Board?" n
    else
      echo "#undef GUSCFG_GUSDB16" >> $TMPCFG
    fi
    bool GUSCFG_GUSMAX "Do you have GUS MAX card?" y
    bool GUSCFG_GUSPNP "Do you have InterWave (GUS PnP) card?" n
    bool GUSCFG_GUSEXTREME "Do you have GUS Extreme card?" n
  fi
  comment ""
  comment "MIDI API is interface for both MIDI emulation and MIDI port."
  comment "Turn this option on if you want use 'ultramidi', 'musserver'"
  comment "and other programs based on MIDI API."
  comment ""
  bool GUSCFG_MIDI "MIDI API support? " y

  comment ""
  comment "Emulation of OSS (Lite) sequencer provides full and enhanced"
  comment "compatibility with /dev/sequencer and /dev/music devices."
  comment "Turn this option on if you want use 'playmidi', 'gmod' and"
  comment "other programs based on OSS sequencer."
  comment ""
  bool GUSCFG_OSS "Emulation of OSS (Lite) sequencer?" y

  comment ""
  comment "Turn on the option below if you want to leave mixer settings"
  comment "after driver is removed from kernel. This option may cause some"
  comment "HW conflicts if you want use resources (IRQ & DMAs) used by GUS"
  comment "card with other devices."
  comment ""
  bool GUSCFG_LEAVE_MIXER "Leave mixer settings after unload?" n
  comment ""
  comment "Debugging mode is used for driver developing."
  comment "Note: Debug routines eat some kernel memory and CPU time."
  comment ""
  bool GUSCFG_DEBUG "Debugging mode?" n
  if [ "$bool_result" = y ]; then
    bool GUSCFG_DEBUG_MEMORY "debug: Memory?" y
    bool GUSCFG_DEBUG_INSTRUMENTS "debug: Instruments?" y
    bool GUSCFG_INTERRUPTS_PROFILE "debug: Interrupts?" y
    bool GUSCFG_DEBUG_DETECT "debug: Verbose autodetection?" n
    bool GUSCFG_DEBUG_ROM "debug: Verbose ROM detection?" n
  fi
  mv $TMPCFG $FINALCFG
fi

echo "driver: Success..."
exit 0
