Procedure for downloading and installation jana2000

Written in C-shell. This procedure should work in most Mac OS X workstations and also in other UNIX workstations. It is not part of the standard distribution of Jana2000, because it has been only tested with five MacOS X 10.3 workstations and two LINUX workstations. The adopting to the local needs requires a moderate experience in UNIX and C-shell.

#! /bin/tcsh -f

if "$1" == "skip" goto skip
 
set instdir = /Applications
 
echo " "
echo "This procedure will download and install jana2000 (beta version)"
echo "----------------------------------------------------------------"
echo -n "continue? [y] "
set pom = $<
if "$pom" == "" set pom = "y"
if ("$pom" != "y" && "$pom" != "yes") exit

echo " "
echo "Making check if g77,gcc and X11 is available ... "
echo "Warning: commands wc,grep and find must be available for the check."
 
set jeg77 = `g77 |& grep -v -i "command not found" | wc -l`
if $jeg77 == 0 then
  set jeg77 = `/sw/bin/g77 |& grep -v -i "command not found" | wc -l`
  if $jeg77 != 0 set path = ($path /sw/bin)
endif
if $jeg77 == 0 then
  echo " "
  echo "cannot find g77"
  echo "please add path to g77 to PATH"
  goto konec
endif
echo "g77 is available"
 
set jegcc = `gcc |& grep -v -i "command not found" | wc -l`
if $jegcc == 0 then
  set jegcc = `/sw/bin/gcc |& grep -v -i "command not found" | wc -l`
  if $jegcc != 0 set path = ($PATH /sw/bin)
endif
if $jegcc == 0 then
  echo " "
  echo "cannot find gcc"
  echo "please add path to gcc to PATH"
  goto konec
endif
echo "gcc is available"

echo "working ......"
set jex11 = `find /usr -name Xlib.h -print |& grep Xlib.h | wc -l`
if $jex11 == 0 then
  echo '#include ' > Xtest.c
  echo 'static Display *display;' >> Xtest.c
  echo 'main()' >> Xtest.c
  echo '{' >> Xtest.c
  echo 'char *display_name = NULL;' >> Xtest.c
  echo 'display = XOpenDisplay(display_name);' >> Xtest.c
  echo 'XCloseDisplay(display);' >> Xtest.c
  echo '}' >> Xtest.c
  if -f Xtest rm Xtest
  gcc -o Xtest Xtest.c -lX11 >& /dev/null
  if -f Xtest set jex11 = "1"
endif
if $jex11 == 0 then
  echo " "
  echo "cannot find Xlib.h in any subdirectory of /usr"
  echo "Xlib.h belongs to X11 development tools"
  goto konec
endif
echo "X11 is available"

echo " "
echo "Users of jana2000 need write permission to some files in the"
echo "installation directory. This procedure will set write permission"
echo "for all files of jana2000. Here it can also be decided which "
echo "user from which group will own the installed files. By default "
echo "they are owned by the one who runs the installation."
echo " "
echo "Current directory: "`pwd`
echo " "
echo -n "Installation directory? [$instdir] "
set pom = "$<"
if "$pom" == "" set pom = $instdir
set instdir = $pom

set finaluser_def=`whoami`
echo -n "User that will own jana files? [$finaluser_def] "
set pom = "$<"
if "$pom" == "" set pom = $finaluser_def
set finaluser = $pom

set finalgroup_def=`id $finaluser | awk '{for(i=1;i<=NF;i++){if(index($i,"gid=")==1)print $i"(xxxx)"}}' | awk -F= '{print $2}' | awk -F\( '{print $2}' | awk -F\) '{print $1}'`
echo -n "To which group $finaluser belongs? [$finalgroup_def] "
set pom = "$<"
if "$pom" == "" set pom = $finalgroup_def
set finalgroup = $pom
 
echo " "
echo "This procedure will download jana2000Pack.exe to directory $instdir"
echo "Jana2000 will be installed in $instdir/jana2000"
echo "The files will be owned by $finaluser from group $finalgroup"
echo "JANA2000 MUST BE CLOSED in the final stage of compilation"
echo "---------------------------------------------------------"
echo -n "continue? [y] "
set pom = $<
if "$pom" == "" set pom = "y"
if ("$pom" != "y" && "$pom" != "yes") exit

# .netrc contains ftp commands. In case of sudo ftp is called by 
# root but it uses .netrc of the user that calls sudo
# => this is correct place for .netrc for both root and rootless installation
 
if -f ~/.netrc mv ~/.netrc ~/.netrc_sav
echo "machine ftp.fzu.cz" > ~/.netrc
echo "login anonymous" >> ~/.netrc
echo "password dusek@fzu.cz" >> ~/.netrc
echo "macdef init" >> ~/.netrc
echo "cd /pub/cryst/beta2000" >> ~/.netrc
echo "dir" >> ~/.netrc
echo "binary" >> ~/.netrc
echo "get jana2000Pack.exe" >> ~/.netrc
echo "quit" >> ~/.netrc
echo "" >> ~/.netrc
chmod go-rwx ~/.netrc

set calledby=`whoami`

# do we need root access? 

set asroot=0
set ownership = 0
if(("$finaluser" != "$finaluser_def") || ("$finalgroup" != "$finalgroup_def"))then
  set ownership = 1
endif
if("$finaluser_def" == "root")goto doakce
echo " "
echo "The procedure will test if it can be executed by the current user"
echo "or if root access is necessary. Here you can choose that you want"
echo "to run it as a root in any case."
echo "-----------------------------------------------------------------"
echo -n "Run as a root? [n] "
set pom = $<
if "$pom" == "" set pom = "n"
if ("$pom" == "y" || "$pom" == "yes")then
  set asroot=1
  set ownership = 1
  goto buderoot
endif
if("$asroot" == "1")goto doakce
touch $instdir/pppp.tmp >& /dev/null
if $status == 0 then
  rm $instdir/pppp.tmp
else
  set asroot=1
  echo "$finaluser_def does not have permission to create files in $instdir"
endif
# we need root also for changing of ownership
if(("$finaluser" != "$finaluser_def") || ("$finalgroup" != "$finalgroup_def"))then
  echo "$finaluser does not have permission to change ownership of files"
  set asroot=1
  set ownership = 1
endif
# old installation of jana2000 may be owned by somebody else
set pom1 = 0
if(-d $instdir/jana2000)then
  set pom = `ls -ld $instdir/jana2000 | awk '{print $3}'`
  if("$pom" != "$finaluser_def")set pom1 = 1
  set pom = `ls -ld $instdir/jana2000 | awk '{print $4}'`
  if("$pom" != "$finalgroup_def")set pom1 = 1
endif
if($pom1 == 1)then
  set asroot = 1
  echo "$instdir/jana2000 is not owned by $finaluser_def or its group is not "
  echo "$finalgroup_def. Therefore we need root access."
endif
if($asroot == 0)then
  goto doakce
else
  echo "Root access is necessary"
endif

echo " "
echo "This procedure must be run by root"

buderoot:
echo " "
echo "Please type your password to get root access by sudo"
# This calls the same procedure as root, the settings come as parameters
sudo -u root $0 skip $instdir $finaluser $finalgroup `whoami` $ownership
exit
 
skip:
if $# != 6 then
  echo "Bad number of arguments"
  goto konec
endif
set instdir=$2
set finaluser=$3
set finalgroup=$4
set calledby=$5
set ownership=$6


# `whoami' returns "root"
if `whoami` != "root" then
  echo This must be run as root
  goto konec
else
  echo sudo su accepted, root logged in
  sleep 2
endif

doakce:
cd $instdir
echo "We are in directory "`pwd`

if (-f jana2000Pack.exe) rm jana2000Pack.exe
ftp ftp.fzu.cz
if -f ~$calledby/.netrc_sav cp ~$calledby/.netrc_sav ~$calledby/.netrc
if !(-f jana2000Pack.exe)then
  echo " "
  echo "Download failed\!\! The ftp server may be down."
  goto konec
endif

/bin/tcsh -c "source jana2000Pack.exe"
# jana2000 executable was deleted at the beginning of compilation
if !(-f $instdir/jana2000/jana2000) goto spatny_konec

if($ownership == 1)then
  chown -R $finaluser":"$finalgroup jana2000
endif
chmod -R ugo+r jana2000
chmod -R ugo+w jana2000/test

konec:
goto kkonec

spatny_konec:
echo "The executable jana2000 has not been created."
echo "It seems that the compilation failed."
echo "It may help to rerun the whole installation as a root."
echo "You are encouraged to ask for help in dusek@fzu.cz."

kkonec:
echo "Press  to exit"
echo "(If you run it from X11 menu this will close the window)"
set pom = $<

exit