#!/bin/csh

set auto = 0
if ($#argv > 0) then
  if ($1 == '-h' || $1 == '--help') then
    echo "Supported options: -arch=<architecture>"
    echo "Supported architectures:"
    ls RULES.* | cut -d "." -f 2
    exit
  else
     set OS = `eval echo $1 | sed 's/arch=//g' | sed 's/-//g'`
    if (-e RULES.$OS) then
      set auto = 1
      set INSTALL = /usr/local/bin
      set LIBX11 = /usr/X11R6/lib
      set LANGUAGE = ENGLISH
      set MEM = 64
    else
      echo "*** Bad option" $1
      echo "*** Supported options: -arch=<architecture>"
      echo "*** Supported architectures:"
      ls RULES.* | cut -d "." -f 2
      exit
    endif
  endif
endif

if ($auto == 0) then
  echo "Thanks for choosing xd3d"
  echo "Please answer a few questions:"
  echo " "
  echo "Configuration files have been found for the following OS:"

  foreach i (RULES.*)
    echo $i:e
  end

  echo '>>> Choose your OS, or the "closest" one, in the above list [linux]'
  set OS = $<
  if ($OS == '') then
    set OS = linux
  endif

  if (!(-e RULES.$OS)) then
    echo "*** Bad answer"
    echo "'>>> Choose your OS in the proposed list \!"
    set OS = $<
    if (!(-e RULES.$OS)) then
      exit
    endif
  endif

  echo " "
  if ($OS == "macOSX") then
    set default_install = /sw/bin
  else
    set default_install = /usr/local/bin
  endif
  echo '>>> Install directory ? ['$default_install']'
  set INSTALL = $<
  if ($INSTALL == '') then
    set INSTALL = $default_install
  endif
  if ($INSTALL == $default_install) then
    echo "*** Be careful. You have to be root to write into" $INSTALL
  endif
endif
 
if (!(-d $INSTALL)) then
  echo $INSTALL "does not exist. I create it."
#  mkdirhier $INSTALL
  mkdir -p $INSTALL
endif

if (!(-d ./lib)) then
  mkdir -p lib
endif

set ICI = `pwd`
cd $INSTALL
set INSTALL = `pwd`
cd $ICI
echo "I'll put xd3d and its related programs in:" $INSTALL

echo " "

if ($auto == 0) then
  echo 'On linux systems the answer to the next question is'
  echo 'obtained by typing "locate libX11.a"'
  echo '(type only the leading path of the result e.g. /usr/X11R6/lib)'

  echo " "
  echo '>>> Location of libX11.a [/usr/X11R6/lib]'
  set LIBX11 = $<
  if ($LIBX11 == '') then
    set LIBX11 = /usr/X11R6/lib
  endif

  echo " "
  echo '>>> Preferred language (french or english) ? [english]'
  set LANGUAGE = $<
  if ($LANGUAGE == 'french') then
    set LANGUAGE = FRANCAIS
  else
    set LANGUAGE = ENGLISH
  endif

  echo " "
  if (-e /proc/meminfo) then
    set bcpresent = `which bc`
    if ($#bcpresent == 1) then
      set mm = `grep -i MemTotal /proc/meminfo`
      if ($#mm > 2) then
        echo $mm[2]"/1000" > bidon$$
        echo "quit" >> bidon$$
        set memhard = `bc < bidon$$`
        /bin/rm bidon$$
        echo "Memory found on your computer =" $memhard "MBytes"
      endif
    endif
  endif
  echo ">>> Max memory allowed to xd3d in MBytes ? [64]"
  set MEM = $<
  if ($MEM == '') then
    set MEM = 64
  endif
endif

/bin/rm -f RULES
set ICI = `pwd | sed "s/\//\\\//g"`
set INSTALL = `eval echo $INSTALL | sed "s/\//\\\//g"`
set LIBX11  = `eval echo $LIBX11  | sed "s/\//\\\//g"`

cat RULES.$OS | sed "s/XD3D_DIR = Please_Configure/XD3D_DIR = $ICI/g" \
              | sed "s/INSTALL_DIR = Please_Configure/INSTALL_DIR = $INSTALL/g" \
              | sed "s/LIBX11 = Please_Configure/LIBX11 = $LIBX11/g" \
              | sed "s/LANGUAGE = Please_Configure/LANGUAGE = $LANGUAGE/g" \
              | sed "s/MEMORY_XD3D = Please_Configure/MEMORY_XD3D = $MEM/g" \
           > RULES

./makedeps
echo " "
echo "Congratulations \!"
echo "You can know type"
echo '"make clean ; make ; make install" and cross your fingers...'
echo 'If you have troubles, try to modify manually the file "RULES"'
echo "according to your local settings"




