#!/bin/bash
#************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: acsSwitchEnv,v 1.2 2010/07/09 12:48:42 alopatin Exp $"
#
# who       when        what
# --------  ----------  -------------------------------------------------
# mpasquat  2003-NOV-26 created
# alopatin  2010-JUL-07 switched from ksh to bash

#*******************************************************************************
# ALMA - Atacama Large Millimeter Array
# Copyright (c) ESO - European Southern Observatory, 2014
# (in the framework of the ALMA collaboration).
# All rights reserved.
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#*******************************************************************************

#************************************************************************
#   NAME 
#   acsSwitchEnv - switch between acs environments
# 
#   SYNOPSIS
#   acsSwitchEnv bash_profile_FileName 
# 
#   DESCRIPTION
#   This script changes the acs environment 
#   sourcing a specific bash profile defined by the user.
#
#   It opens a new xterminal in which the environment is re-setted.
#   
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#   0 - no errors have been produced
#   1 - bad options have been passed or some operation did not work
#
#   CAUTIONS
#
#   EXAMPLES
#   acsSwitchEnv bash_profile_FileName                           
#   The bash_profile_FileName is sourced 
#                                       
#   SEE ALSO
#
#   BUGS     
#
#------------------------------------------------------------------------

# DISPLAY variable check
if [[ ! -n $DISPLAY ]]
then
   echo " "
   echo " *** DISPLAY variable is not set!"
   echo " "
   exit 1
fi

# Checking arguments
# Only one argument MUST be a file
if [[ $# -eq 1  ]]
then
   if [[ ! -f $1 ]]
   then
     echo " " 
     echo "*** The argument you provided is a not a file!"
     echo " "
     exit 1
   else
     isFile="bash"
   fi   
fi

# Arguments checking 
if [[ $# -lt 1  || $# -gt 1 ]]
then 
  if  [[ $# -lt 1 ]]
  then
     echo " "
     echo " *** Argument/s missing!"
  else
     echo " "
     echo " *** Too many arguments!"
  fi
   echo " "
   echo " usage: acsSwitchEnv bash_profile_FileName"
   echo " "
   echo " Examples:"
   echo " ---------"
   echo " acsSwitchEnv bash_profile_FileName"                          
   echo " -The bash_profile_FileName is sourced."
   echo " "
   exit 1
fi


# There is a bash profile to be sourced

#cp $HOME/.acs/.bash_profile.acs $HOME/.acs/.bash_profile.acs.temporary
#if [[ "$?" != "0" ]]
#then
#   echo " "
#   echo "*** No write permission on directory $HOME/.acs!"
#   echo " "
#   exit 1
#fi
#cp  $1 $HOME/.acs/.bash_profile.acs
#xterm -e sh -c ". $HOME/.acs/.bash_profile.acs; mv $HOME/.acs/.bash_profile.acs.temporary $HOME/.acs/.bash_profile.acs; bash" &

xterm -e sh -c ". $1; bash" &

echo " "
echo " The NEW xterm has the customized environment settings."
echo " "
