#!/bin/csh
echo Running setup script for InfoI exercise 1

# remove the tar file in case this was not yet done
echo "#InfoI: Deleting ex1.tar in home directory (if not yet done)"
rm ~/ex1.tar >& /dev/null

#
# Add INFOI_SETUP material at the rear of the student's ~/.cshrc
#

set done = 0

if ( -f ~/.cshrc ) then
  if ( `cat ~/.cshrc | grep "INFOI_SETUP" | wc -l` != 0 ) then
    echo "#InfoI: Initial infoI web_exe_get setup was already performed, all is fine"
    set done = 1
  endif
endif

if ( $done == 0 ) then

cat -<< _EOD_ >> ~/.cshrc
#
# -------------------- aliases for INFOI_SETUP ----------------------
#
# alias to get the exercise material directory for a given exercise
alias web_exe_get 'rm -rf ~/ex\!:1P >& /dev/null; curl https://ethz.ch/content/dam/ethz/special-interest/chab/imps/csms-dam/doc/exe/ex\!:1.tar > ~/ex\!:1.tar; (cd; tar -xpvf ~/ex\!:1.tar); rm ~/ex\!:1.tar; ~/ex\!:1P/setup/setup'
#
# -------------------------------------------------------------------
_EOD_

echo "InfoI: Initial InfoI web_exe_get setup has just been performed, all is fine"

endif

# install link to perm in the /tmp dir
rm -rf /tmp/${USER} >& /dev/null
mkdir /tmp/${USER}
mkdir /tmp/${USER}/ex1P
cp -r ~/ex1P/setup/perm /tmp/${USER}/ex1P/perm
rm -rf ~/ex1P/perm >& /dev/null
ln -s /tmp/${USER}/ex1P/perm ~/ex1P/perm
chmod 500 ~/ex1P/perm/executable
chmod 400 ~/ex1P/perm/read_only.txt
chmod 600 ~/ex1P/perm/writeable.txt
chmod 777 ~/ex1P/perm/all_for_all

#
# Phil - 10.10.2018
# ---> setup slightly changed to avoid login issues -
#      now .bash_profile and .bashrc will both be set to
#      end with "csh"
#

# make csh the default shell
# -> bypassed: makes login problems
goto 100

echo "#InfoI: Making sure csh your default shell..."

set doprof = 0

if ( -f ~/.bash_profile ) then
  if ( `cat ~/.bash_profile | tail -1 | grep '^ *csh *$' | wc -l` == 1 ) then
      echo "#InfoI: Your default shell in ~/.bash_pofile is already csh"
  else
    echo "#InfoI: Moving your current ~/.bash_profile to ~/.bash_profile_OLD"
    mv ~/.bash_profile ~/.bash_profile_OLD
    set doprof = 1
  endif
else
  set doprof = 1
endif

set dorc = 0

if ( -f ~/.bashrc ) then
  if ( `cat ~/.bashrc | tail -1 | grep '^ *csh *$' | wc -l` == 1 ) then
      echo "#InfoI: Your default shell in ~/.bashrc is already csh"
  else
    echo "#InfoI: Moving your current ~/.bashrc to ~/.bashrc_OLD"
    mv ~/.bashrc ~/.bashrc_OLD
    set dorc = 1
  endif
else
  set dorc = 1
endif

if ( $doprof == 1 ) then
  echo "#InfoI: Making csh your default shell in ~/.bash_profile"
  echo "#InfoI: Making csh your default shell in ~/.bash_profile" >> ~/.bash_profile
  echo "csh" >> ~/.bash_profile
endif

if ( $dorc == 1 ) then
  echo "#InfoI: Making csh your default shell in ~/.bashrc"
  echo "#InfoI: Making csh your default shell in ~/.bashrc" >> ~/.bashrc
  echo "csh" >> ~/.bashrc
endif

if ( $doprof == 1 || $dorc == 1 ) then
  echo "PHIL has just made csh your default shell:"
  echo "From now on, you no longer need to type 'csh'"
  echo "when you login or start a new terminal; if you"
  echo "don't like this, just change back your"
  echo "~/.bash_profile and ~/.bashrc"
endif

100:
exit
