#!/bin/bash

echo "" > /tmp/temp.tmp

#Use .bashrc if it exists, otherwise .bash_profile
b=.bashrc
if test ! -f "$HOME/$b" ; then
  b=.bash_profile
fi
bp=$HOME/$b

idir=/Applications/NEURON-@PACKAGE_VERSION@

# if cannot find nrniv, then perhaps modify PATH
echo "PATH=$PATH" >> /tmp/temp.tmp
# can nrniv be found in the users path?
if test -f "$bp" ; then
  (source $bp ; echo "srcpath $PATH" >> /tmp/temp.tmp ; echo `which nrniv` >> /tmp/temp.tmp ; which nrniv)
  a=$?
  echo "a=$a" >> /tmp/temp.tmp
  if test "$a" = "0" ; then
    addpath=no
  else
    addpath=yes
  fi
  echo "PATH=$PATH" >> /tmp/temp.tmp
  echo "in source addpath=$addpath" >> /tmp/temp.tmp
else
  addpath=yes
fi
echo "PATH=$PATH" >> /tmp/temp.tmp

echo "addpath=$addpath" >> /tmp/temp.tmp

if test "$addpath" = "yes" ; then
a=`osascript -e 'tell app "Installer" to display dialog "
Modify PATH to allow launching nrniv from Terminal?
  ... will append a line to  $HOME/'$b'
" buttons {"No", "Yes"} default button 1'`

  echo "a=$a" >> /tmp/temp.tmp
  if test "$a" = "button returned:Yes" ; then
    addpath=yes
  else
    addpath=no
  fi
fi
echo "addpath=$addpath" >> /tmp/temp.tmp

if test "$addpath" = "yes" ; then
  if test  ! -f "$bp" ; then
    echo '#!bin/bash' > "$bp"
  fi
  echo "export PATH=$idir/nrn/x86_64/bin:\$PATH #added by NEURON installer" >> "$bp"
  chown $USER "$bp"
fi

if true && test "$addpath" = "yes" ; then # Python environment
  addpyenv=no

  a=`osascript -e 'tell app "Installer" to display dialog "
Modify $HOME/'$b' to allow NEURON + Python
  ... will set some Python related environment variables.
" buttons {"No", "Yes"} default button 1'`

  echo "a=$a" >> /tmp/temp.tmp
  if test "$a" = "button returned:Yes" ; then
    addpyenv=yes
  else
    addpyenv=no
  fi

  if test "$addpyenv" = "yes" ; then
    echo "export PYTHONPATH=$idir/nrn/lib/python #added by NEURON installer" >> "$bp"
    tmpfile=/tmp/$$.tmp
    (source $bp ; echo "srcpath $PATH" >> /tmp/temp.tmp ; $idir/nrn/x86_64/bin/nrnpyenv.sh |sed -n 's/\(^export.*\)/\1 #added by NEURON installer/p' > $tmpfile)
    sed '/PYTHONPATH/d' $tmpfile >> "$bp"
    rm -f $tmpfile
  fi
  chown $USER "$bp"
fi

if true ; then # notice that Command Line Tools and XQuartz also need to be installed.
osascript -e 'tell app "Installer" to display dialog "NEURON @PACKAGE_VERSION@ was successfully installed, but ...
Mod files cannot be linked into NEURON unless
Command Line Tools is installed,
and NEURON cannot launch unless XQuartz is installed.
If either or both are not installed, you will be asked
if you wish to install them. You may wish to install
XQuartz last as it requires logging out at the end.
(After closing this dialog, you can close the NEURON installer).

" buttons {"Continue"}'
fi

# install command line tools?
if gcc -v ; then 
  true
else
  true
fi

#single click button action for x11 when entering a window
#focus follows mouse
sudo -u $USER defaults write org.macosforge.xquartz.X11 wm_ffm -bool true
echo "defaults write" >> /tmp/temp.tmp

# install XQuartz?
if /usr/X11/bin/xauth version ; then
  true
else
  true
fi

