#! /bin/csh -f

#************************************
# file last changed on: 04-Mar-98
#************************************

#************************************
#  version number:
#************************************

 set ver = v1.5

#************************************


 set a = a$1

 if( $a == "a-f") then
  echo ' '
  echo '-f option: forced re-running of fakerat_init'
  echo ' '
  /bin/rm -f .lock
 endif

 if( -e .lock) then
   cat .lock
   exit
 endif
  

 echo ' '
 echo '*****************************************************************'
 echo ' In order for fakerat_init to work the environmental variable   '
 echo ' FDIR must be set as follows:                                  '
 echo '                                                               '
 echo "  setenv FDIR x/fakerat_${ver}                                  "
 echo ' '
 echo "(where x is the directory in which the fakerat_${ver}.tar file resides)"  
 echo '                                             '
 echo '*****************************************************************'
 echo ' '


if(-d $FDIR) then 

 set user = $cwd
 cd $FDIR
 set fdir = $cwd
 cd $user

 echo ' '
 echo 'using a fakerat directory of ' $fdir
 echo ' '
else
 echo ' '
 echo 'the directory: '
 echo ' '
 echo $FDIR
 echo  ' '
 echo 'does NOT exist, exiting from fakerat_init'
 echo  ' '

 goto 400

endif

if(! -d $FDIR/doc) then
 echo $FDIR/doc 'does NOT exist ' 
 echo ' '
 goto 400
endif

if(! -d $FDIR/data) then
 echo $FDIR/data 'does NOT exist '
 echo ' '
 goto 400
endif

#if(! -d $FDIR/sun4.1.x) then
# echo $FDIR/sun4.1.x 'does NOT exist '
# echo ' '
# goto 400
#endif






echo ' '
echo '(fakerat_init only works with BSD UNIX )'
echo '(the $< command is used to read an input line)'
echo ' '

echo ' '
echo 'Do you want to perform automatic model-fitting ?'
echo ' '
echo '(automatic model-fitting increase the size of the '
echo ' fakerat by 10 Mbytes )'
echo ' '
echo 'First time users of fakerat should answer n to this question'
echo ' '
echo 'answer y or n '

set mod_fit = $<

set ask_question = 0;

if($ask_question) then

# set a = n if NOT using Sun OS 4.1.x
# set a = y if NOT using Sun OS 4.1.x

echo ' '
echo 'Are you on a SUN workstation that runs Sun OS 4.1.x ?'
echo ' '
echo 'answer y or n '
set a = $<

else

set a = 'n'

endif





if($a != "n" && $a != "y") then 
 echo  ' '
 echo ' *** ERROR exiting: fakerat_init  ***'
 echo ' Run fakerat_init again '
 echo ' please answer y or n to this question '
 echo ' '
 exit(1)
endif

echo ' '
echo 'removing old files'
echo ' '

/bin/rm $user/.worldmap
/bin/rm $user/.worldmap2
/bin/rm $user/fakerat
/bin/rm $user/fakerat_menu
/bin/rm $user/menurat_defaults
/bin/rm $user/menu_colors
/bin/rm $user/fakeret_system


echo ' '

set MODEL = $fdir/model

if(! -e $user/model) mkdir $user/model

echo ' '
echo 'copying the following files '
echo 'from: ' $MODEL
echo 'to:   ' $user/model
echo ' '
ls -1 $MODEL


cp $MODEL/* $user/model

echo ' '

echo 'copying the following files to ' $user

set DATA = $fdir/data

if(-e  $DATA/menurat_defaults) then

 cp $DATA/menurat_defaults menurat_defaults
echo ' '
echo 'copying: ' $DATA/menurat_defaults
echo 'to:      ' $user/menurat_defaults
echo ' '

else
 echo $DATA/menurat_defaults ' does NOT exist '
 echo  ' '
 goto 400
 endif

if(-e  $DATA/menu_colors) then
 cp $DATA/menu_colors menu_colors
echo ' '
echo 'copying: ' $DATA/menu_colors 
echo 'to:      ' $user/menu_colors
echo ' '
else
 echo 'file: $DATA/menu_colors does NOT exist'
 echo  ' '
 goto 400
endif

if(-e  $DATA/run) then
 cp $DATA/run . 
echo ' '
echo 'copying: ' $DATA/run
echo 'to:      ' $user/run
echo ' '
else
 echo 'file: $DATA/run does NOT exist'
 echo  ' '
 goto 400
endif

if(-e  $DATA/stations.cat) then
 cp $DATA/stations.cat stations.cat
 echo 'copying:  ' $DATA/stations.cat
 echo 'to:       ' $user/stations.cat 
 echo ' '
 cp $DATA/stations.cat .stations.cat.old
 echo 'copying:  ' $DATA/stations.cat
 echo 'to:       ' $user/.stations.cat.old 
 echo ' '
else
 echo $DATA/stations.cat ' does NOT exist'
 echo  ' '
 goto 400
endif

if(-e  $DATA/sources.cat) then
 cp $DATA/sources.cat sources.cat
 echo 'copying:  ' $DATA/sources.cat
 echo 'to:       ' $user/sources.cat 
 echo ' '
else
 echo $DATA/sources.cat ' does NOT exist'
 echo  ' '
 goto 400
endif




echo ' '


echo 'setting up the following soft links in ' $user
echo ' ' 

if(-e  $DATA/fakerat_system) then
 ln -s $DATA/fakerat_system fakerat_system
 echo 'from:   ' $DATA/fakerat_system
 echo 'to:     ' $user/fakerat_system 
 echo ' '
else
 echo  $DATA/fakerat_system ' does NOT exist'
 echo  ' '
 goto 400
endif


if(-e  $DATA/worldmap-worlddata) then
 ln -s $DATA/worldmap-worlddata  .worldmap
 echo 'from:   ' $DATA/worldmap-worlddata
 echo 'to:     ' $user/.worldmap 
 echo ' '
else
 echo  $DATA/worldmap-worldata ' does NOT exist'
 echo  ' '
 goto 400
endif

if(-e  $DATA/worldmap-jpl) then
 ln -s $DATA/worldmap-jpl .worldmap2
 echo 'from: ' $DATA/worldmap-jpl
 echo 'to:   ' $user/.worldmap2 
 echo ' '
else
 echo $DATA/worldmap-jpl 'does NOT exist'
 echo  ' '
 goto 400
endif

 /bin/rm  $user/tex

 echo ' '
 ln -s $fdir/doc/tex $user/tex
 echo 'from: ' $fdir/doc/tex
 echo 'to:   ' $user/tex
echo ' '

echo 'switch('$a$mod_fit')'


switch ($a$mod_fit)

case yy:

# model-fitting enabled for sun 4.1.x version

echo 'mod_fit is: ' $mod_fit

if($mod_fit == 'y') then
 echo 'yes'
else
echo 'no'
endif

 echo ' '
 echo 'automatic model-fitting is enabled'
 echo ' '

if(-e  $FDIR/sun4.1.x/fakeratm ) then
 ln -s $fdir/sun4.1.x/fakeratm fakerat
 echo 'from: ' $fdir/sun4.1.x/fakeratm
 echo 'to:   ' $user/fakerat
 echo ' '
else
 echo $fdir/sun4.1.x/fakeratm 'does NOT exist'
 echo  ' '
 goto 400
endif


if(-e  $FDIR/sun4.1.x/fakerat_menu) then
 ln -s $fdir/sun4.1.x/fakerat_menu fakerat_menu
 echo 'from: ' $fdir/sun4.1.x/fakerat_menu 
 echo 'to:   ' $user/fakerat_menu
 echo ' '
else
 echo  $fdir/fakerat_menu ' does NOT exist'
 echo  ' '
 goto 400
endif


breaksw

case yn:


 echo ' '
 echo 'automatic model-fitting is disabled'
 echo ' '


if(-e  $FDIR/sun4.1.x/fakeratm ) then
 ln -s $fdir/sun4.1.x/fakerat fakerat
 echo 'from: ' $fdir/sun4.1.x/fakerat
 echo 'to:   ' $user/fakerat
 echo ' '
else
 echo $fdir/sun4.1.x/fakerat 'does NOT exist'
 echo  ' '
 goto 400
endif


if(-e  $FDIR/sun4.1.x/fakerat_menu) then
 ln -s $fdir/sun4.1.x/fakerat_menu fakerat_menu
 echo 'from: ' $fdir/sun4.1.x/fakerat_menu 
 echo 'to:   ' $user/fakerat_menu
 echo ' '
else
 echo  $fdir/fakerat_menu ' does NOT exist'
 echo  ' '
 goto 400
endif

breaksw

case ny:

 if(! -e $fdir/fakerat )  then 
  echo ' '  
  echo $fdir/fakerat 'does NOT exist'
 endif

 if(! -e $fdir/fakerat_menu )  then 
  echo ' '  
  echo $fdir/fakerat_menu 'does NOT exist'
 endif

  echo ' '

if(-e  $FDIR/fakeratm &&  -e $FDIR/fakerat_menu) then
 ln -s $FDIR/fakeratm fakerat
 echo 'from: ' $fdir/fakeratm
 echo 'to:   ' $user/fakerat
 echo ' '
 ln -s $FDIR/fakerat_menu fakerat_menu
 echo 'from: ' $fdir/fakerat_menu 
 echo 'to:   ' $user/fakerat_menu
 echo ' '
else

 echo 'to make fakerat and fakerat_menu : '
 echo ' '
 echo 'step 1: '
 echo ' '
 echo '   IF the Caltech PGPLOT package is not installed on your machine'
 echo '   you need to obtain the PGPLOT package by anonymous ftp from '
 echo '   from astro.caltech.edu and install it'
 echo ' '
 echo 'step 2: '
 echo ' '
 echo echo 'change directory to the relevant installation directory: '
 echo ' '
 echo '   cd ' $fdir 
 echo ' '
 echo '   IF you are not working on either a SUN or HP machines you will have '
 echo '   adapt the Makfile to suit your needs '
 echo ' '
 echo 'step 3: '
 echo ' '
 echo '   Edit ' $fdir/Makefile 'so that the PGDIR is set to the location'
 echo '   of the PGPLOT libraries (for example libpgplot.a) '
 echo ' '
 echo 'step 4: '
 echo ' '
 echo '   IF you have the Caltech VLBI package installed on your machine: '
 echo ' '
 echo '  '
 echo '   Edit ' $fdir/Makefile 'so that the VLBDIR is set to the location'
 echo '   of the CIT libraries (for example vlblib.a) '
 echo ' '
 echo 'step 5: '
 echo '  '
 echo 'If you are on a SUN worksation that has the Caltech VLBI package:'
 echo ' '
 echo  'make sun'
 echo ' '
 echo 'If you are on a SUN worksation that does NOT have the Caltech VLBI package:'
 echo ' '
 echo  'make sun.libraries'
 echo ' '
 echo 'If you are on a HP worksation that has the Caltech VLBI package:'
 echo ' '
 echo  'make hp'
 echo ' '
 echo 'If you are on a HP worksation that does NOT have the Caltech VLBI package:'
 echo ' '
 echo  'make hp.libraries'
 echo ' '
 echo 'step 6: '
 echo ' '
 echo 'remove unwanted .o files: '
 echo ' '
 echo 'make clean'
 echo ' '
 echo 'step 7: '
 echo ' '
 echo '   re-run fakerat_init in the directory in which you wish to run fakerat:' 
 echo ' '
 echo '   cd ' $user
 echo '   fakerat_init '
 echo ' '
 
 exit(1)
endif


breaksw

case nn:

 if(! -e $fdir/fakerat )  then 
  echo ' '  
  echo $fdir/fakerat 'does NOT exist'
 endif

 if(! -e $fdir/fakerat_menu )  then 
  echo ' '  
  echo $fdir/fakerat_menu 'does NOT exist'
 endif

  echo ' '

if(-e  $FDIR/fakerat &&  -e $FDIR/fakerat_menu) then
 ln -s $FDIR/fakerat fakerat
 echo 'from: ' $fdir/fakerat 
 echo 'to:   ' $user/fakerat
 echo ' '
 ln -s $FDIR/fakerat_menu fakerat_menu
 echo 'from: ' $fdir/fakerat_menu 
 echo 'to:   ' $user/fakerat_menu
 echo ' '
else

 echo 'to make fakerat and fakerat_menu : '
 echo ' '
 echo 'step 1: '
 echo ' '
 echo '   IF the Caltech PGPLOT package is not installed on your machine'
 echo '   you need to obtain the PGPLOT package by anonymous ftp from '
 echo '   from astro.caltech.edu and install it'
 echo ' '
 echo 'step 2: '
 echo ' '
 echo echo 'change directory to the relevant installation directory: '
 echo ' '
 echo '   cd ' $fdir 
 echo ' '
 echo '   IF you are not working on either a SUN or HP machines you will have '
 echo '   adapt the Makfile to suit your needs '
 echo ' '
 echo 'step 3: '
 echo ' '
 echo '   Edit ' $fdir/Makefile 'so that the PGDIR is set to the location'
 echo '   of the PGPLOT libraries (for example libpgplot.a) '
 echo ' '
 echo 'step 4: '
 echo ' '
 echo '   IF you have the Caltech VLBI package installed on your machine: '
 echo ' '
 echo '  '
 echo '   Edit ' $fdir/Makefile 'so that the VLBDIR is set to the location'
 echo '   of the CIT libraries (for example vlblib.a) '
 echo ' '
 echo 'step 5: '
 echo '  '
 echo 'If you are on a SUN worksation that has the Caltech VLBI package:'
 echo ' '
 echo  'make sun'
 echo ' '
 echo 'If you are on a SUN worksation that does NOT have the Caltech VLBI package:'
 echo ' '
 echo  'make sun.libraries'
 echo ' '
 echo 'If you are on a HP worksation that has the Caltech VLBI package:'
 echo ' '
 echo  'make hp'
 echo ' '
 echo 'If you are on a HP worksation that does NOT have the Caltech VLBI package:'
 echo ' '
 echo  'make hp.libraries'
 echo ' '
 echo ' '
 echo 'step 6: '
 echo ' '
 echo 'remove unwanted .o files: '
 echo ' '
 echo 'make clean'
 echo ' '
 echo 'step 7: '
 echo ' '
 echo '   re-run fakerat_init in the directory in which you wish to run fakerat:' 
 echo ' '
 echo '   cd ' $user
 echo '   fakerat_init '
 echo ' '

 exit(1)
endif


breaksw


endsw


# file /vlbaips/vlb/pgplot/sys_sun/grsy00.f determines where grfont.dat
# is located via parameter UNIX


 echo '**************** IMPORTANT NOTE *******************'
 echo ' '
 echo ' PGPLOT needs to know the location of the font file'
 echo ' grfont.dat'
 echo ' '
 echo ' For versions of PGPLOT 5.0 or higher the full pathname'
 echo ' of the grfont.dat files is specified using the '
 echo ' PGPLOT_FONT enviromental variable'
 echo ' '
 echo ' Older versions (pre 5.0) of PGPLOT also search for grfont.dat in: '
 echo ' '
 echo ' /usr/local/vlb/pgplot on SUN workstations '
 echo ' /usr/src/pgplot       on HP  workstations '
 echo '  '

 set found  = 0

 echo ' '
 echo 'checking to see if PGPLOT_FONT is set ....'
 echo ' '

if($?PGPLOT_FONT != 0) then
 echo 'PGPLOT_FONT is set to be: ' $PGPLOT_FONT 
 set found = 1
else
 echo ' PGPLOT_FONT is unset'
endif

if($?PGPLOT_FONT != 0) then
 if( -d $PGPLOT_FONT) then
 echo 'the file ' $PGPLOT_FONT ' exists'
endif
endif


switch ($a)

case y:

 if(-e $fdir/sun4.1.x/grfont.dat) then


 echo 'Sun OS 4.1.x installation:'
 echo  ' '

 echo ' edit the c-shell script file run and add'
 echo ' the following line: '
 echo ' '
 echo 'setenv PGPLOT_FONT ' $fdir/sun4.1.x/grfont.dat
 echo ' '

 else

  echo ' cannot find file: ' $fdir/sun4.1.x/grfont.dat
  echo ' this file should exist'
  echo ' exiting installation'
  exit

 endif

breaksw

case n:

if( $found == 0 ) then

echo ' '
echo ' Edit the c-shell script file run and add'
echo ' the following line: '
echo ' '
echo 'setenv PGPLOT_FONT <pathname of grfont.dat file>'
echo ' '

endif

breaksw

endsw

 echo ' '
 echo '***************************************************'
 

###### pgxwin_server search ###################

 echo '    '
 echo '*************** IMPORTANT NOTE ******************'
 echo ' '
 echo ' PGPLOT versions 5.0 or higher also need the '
 echo ' pgxwin_server program. This program must exist '
 echo ' either in a directory specified via the enviromental '
 echo ' variable PGPLOT_DIR or it must exist in a directory '
 echo ' in your path. '
 echo ' '
 echo ' searching for pgxwin_server program ...'
 echo ' '

 set f = pgxwin_server

if(-e $f) /bin/rm $f

 set found = 0

# set i = `printenv |grep PGPLOT_DIR |wc -l`
# if($i != 1) then

 if($?PGPLOT_DIR == 0) then
   echo ' PGPLOT_DIR enviromental variable does NOT exist'
   echo
   goto 200
 else
   echo ' PGPLOT_DIR enviromental variable does exist'
 endif

if(-d $PGPLOT_DIR) then
  echo ' PGPLOT_DIR is set to: ' $PGPLOT_DIR
else
 echo ' '
 echo ' directory ' $PGPLOT_DIR 'specified by PGPLOT_DIR does not exist'
 goto 200
endif

 if(-e $PGPLOT_DIR/$f) then
  set found = 1
  echo ' '$f ' is in: ' $PGPLOT_DIR 
  goto 100
 else
   echo ' '$PGPLOT_DIR/$f ' does not exist'
 endif

200:
 

 echo ' '
 echo ' searching directories in your path ...'
 echo ' '

foreach dir ($path)
# echo ' searching in: ' $dir
if(-e  $dir/$f ) then
  set found = 1

  set user = $cwd
  cd  $dir
  set pgxwin_dir = $cwd
  cd $user

  echo ' Found '$f ' it is in: ' 
  echo ' '
  echo ' '$pgxwin_dir
  echo ' '
  echo ' Therefore no need to set PGPLOT_DIR enviromental variable '
  echo ' '
  set found = 1
  goto 100
endif
end

100:

if ($found == 1) then
 goto 300
endif

 echo ' '
 echo '*** CANNOT find '$f' ***'
 echo ' '

echo 'a is: '$a

switch ($a)

case y:

 echo ' '
 echo ' therefore creating the soft link: ' 
 echo ' from:   ' $fdir/sun4.1.x/$f
 echo ' to:     ' $user/$f
 echo ' '

 if(-e $fdir/sun4.1.x/$f) then

 ln -s $fdir/sun4.1.x/$f $f
 set found = 1

 else

 echo 'problems creating soft link'
 echo $fdir/sun4.1.x/$f 'does NOT exist'


 endif


breaksw

case n:

 echo ' '
 echo ' PGPLOT versions 5.0 or higher also need the '
 echo ' pgxwin_server program. This program must exist '
 echo ' either in a directory specified via the enviromental '
 echo ' variable PGPLOT_DIR or it must exist in a directory '
 echo ' in your path. '
 
breaksw

endsw


300:

echo '   '    
echo '*****************************************************'
echo ' '

echo '****** fakerat_init was completed successfully ******'
if ($found == 0) then
echo ' '
echo '(with the exception that the pgxwin_server was not found'
echo ' this is only needed for PGPLOT versions 5.0 or higher )'
echo ' '
endif

echo ' '
echo  ' '
echo ' To run the software you have to do two more things:'
echo ' '
echo ' (1) Edit the c-shell script, run, to setup PGPLOT' 
echo '     environmental variables (if required)'
echo ' '
echo ' (2) Type run to run the fakerat software.'
echo ' '
echo '*******************************************************'
echo ' '


 cat > .lock << EOD

 *** fakerat_init has already been run successfully   ***  
 *** there is no need to run it again                 *** 
 ***                                                  ***
 *** to run the fakerat software simply type: run     ***
 ***                                                  ***
 *** (if you REALLY want to run fakerat_init again    ***
 ***  type: fakerat_init -f)                          ***

EOD

exit

400:

echo  ' '
 echo 'on command line type :'
 echo "setenv FDIR x/fakerat_${ver} "
 echo "(where x is the directory in which the fakerat_${ver}.tar resides)"
 echo ' '
echo

exit(1)












