
#!/bin/csh -f

echo ""
echo " *----------------------------------------------------------------------------*"
echo " |           NMRPipe System (c) Frank Delaglio, 1995-2026                     |"
echo " |            Installation Script Version 2026.006.15.19                      |"
echo " |                 https://www.ibbr.umd.edu/nmrpipe                           |" 
echo " |              frank.delaglio@nist.gov delaglio@umd.edu                      |" 
echo " |                                                                            |"
echo " |           For more information, use: install.com +help                     |"
echo " *----------------------------------------------------------------------------*"
echo ""

@ errCount    = 0
@ tarCount    = 0
@ patchCount  = 0
@ pluginCount = 0

set cmdList  = (cp rm chmod date grep ls mkdir mv tar cat zcat wc)
set x11List  = (xset xterm)

set helpFlag   = 0
set protFlag   = 0
set testFlag   = 0
set postFlag   = 1
set quoteFlag  = 0
set fontFlag   = 0
set typeFlag   = 0
set xtermFlag  = 0
set txtManFlag = 0
set unpackFlag = 1
set initFlag   = 1
set cshrcFlag  = 1
set vncFlag    = 1
set nmrboxFlag = 0
set autoFlag   = 0

set tclFontMode  = 2
set tclVarFont   = Default
set tclFixedFont = Default

set xnotifyExe = xnotify.exe
set binType    = unknown
set trueType   = unknown
set homeDir    = home
set binDir     = nmrbin
set manPath    = man
set x11Path    = /usr/X11R6/bin
set srcDir     = .
set baseDir    = .

#
# First, test for recursive request to update protections and exit:

if ($#argv > 0) then
   if ($argv[1] == +help || X$argv[1] == X-help) then
      set helpFlag = 1
   endif

   if ($argv[1] == +prot || X$argv[1] == X-prot) then
      set protFlag = 1
      set binDir   = $argv[2]
   endif
endif

set binTypeList = (linux linux9 linux212_64 linux231_64 linux235_64 linux235_arm64 linux239_64 mac mac11 mac11_64 mac_arm64 winxp sol sgi6x)

if ($protFlag) then
   foreach i (man nmrtxt nmrtcl nmruser format talos talosplus talosn sparta spartaplus promega acme mfr dynamo pdb XView)
      if (-d $i) then
         chmod -R a+rw $i
      endif
   end

   foreach i (com nmrtcl dynamo/com dynamo/tcl talos/bin talosplus/bin talosn/bin sparta/bin spartaplus/bin promega/bin $binDir $binDir/lib)
      if (-d $i) then
         chmod -R a+rwx $i
      endif
   end

   foreach i ($binTypeList)
      if (-d nmrbin.$i) then
         chmod -R a+rwx nmrbin.$i
      endif

      if (-d nmrbin.$i/lib) then
         chmod -R a+rwx nmrbin.$i/lib
      endif

      if (-d nmrbin_extra.$i) then
         chmod -R a+rwx nmrbin_extra.$i
      endif

      if (-d nmrbin_extra.$i/lib) then
         chmod -R a+rwx nmrbin_extra.$i/lib
      endif
   end

   exit 0
endif

#
# Informational text:

if ($helpFlag == 1) then
   echo "Install the NMRPipe system. Usage:"
   echo " install.com"
   echo " install.com destDir"
   echo " install.com [OPTIONS]"
   echo "OPTIONS:"
   echo " [+type osType]  Set the Operating System Type Manually:"
   echo ""
   echo "                 Linux Versions:     linux9  linux212_64 linux231_64 linux235_64"
   echo "                                     linux239_64 linux235_arm64"
   echo "                 Max OS X Versions:  mac11_64  mac_arm64"
   echo "                 Legacy Versions:    linux mac mac11 winxp sol sgi6x"
   echo ""
   echo " [+src srcDir]   Set the location of the .tZ install input files."
   echo " [+dest destDir] Set name for referring to the install directory."
   echo " [+sfu homeDir]  Create the given home directory (winxp only)."
   echo " [+nopost]       Suppress pre and post-install software and graphics tests."
   echo " [+test]         Test OS Type only, no install performed."
   echo " [+nounpack]     Do not unpack installation archives."
   echo " [+noinit]       Do not create com/nmrInit.*.com file."
   echo " [+nocshrc]      Do not create ~/.cshrc file."
   echo " [+vnc]          Use font initialization that checks for local X11 connection."
   echo " [+nmrbox]       Install for NMRbox Use (https://www.nmrbox.org)."
   echo " [+auto]         Automatically download and install NMRPipe."
   echo " [+autoDir]      Set directory for Auto Install (Default: ~/pipe)."
#  echo " [+quoteEnv]     Add Double Quotes for PATH (for paths with space chars)."
   echo ""
   echo " [+tclFontMode  mode]  0 = No Reset, 1 = Always Reset, 2 = Reset When Needed."
   echo " [+tclVarFont   font]  Examples: Default family=Lucida,size=9,weight=bold"
   echo " [+tclFixedFont font]  Examples: Default family=Fixed,size=9,weight=normal"
   echo "-------------------------------------------------------------------------------"
   echo "Example Auto Installation (Default Auto-Install Directory is ~/pipe):"
   echo ""
   echo "  (Review the install web page)"
   echo "  (Rename or delete existing NMRPipe installation if any)."
   echo "  (Remove NMRPipe initializations from the .cshrc file in your login directory)"
   echo "  (Delete the .cshrc file in your login directory if it has no other commands)"
   echo "  (Start terminal window)"
   echo ""
   echo "     csh"
   echo "     cd"
   echo ""
   echo "  (On Linux) wget    https://www.ibbr.umd.edu/nmrpipe/install.com"
   echo "  (On Mac)   curl -O https://www.ibbr.umd.edu/nmrpipe/install.com"
   echo ""
   echo "     chmod a+rx install.com"
   echo "     ./install.com +auto"
   echo ""
   echo "The Auto-Install will download ~800MB of installation files."
   echo "After installation, log out, log in, and check the installation:"
   echo ""
   echo "     which nmrPipe"
   echo "     printenv | grep NMR"
   echo "     nmrPipe -help"
   echo ""
   echo "In a typical session, use cd to go to an NMR data directory. Then:"
   echo ""
   echo "     bruker  (for Bruker directory with: ser pulseprogram* acqus ...)"
   echo "     varian  (for Varian directory with: fid procpar)"
   echo "     nmrDraw"

   echo "-------------------------------------------------------------------------------"
   echo "Notes:"
   echo "  1. This script must be run from the C-Shell (not bash or sh);"
   echo "     use the chsh command or other method to set the default shell."
   echo "  2. This script should be run at the graphics console of"
   echo "     the workstation where the software will be used,"
   echo "     using an xterm terminal window."
   echo "  3. All files will ALWAYS be installed under the current directory;"
   echo "     the +dest destination directory parameter is used to override"
   echo "     the directory name setting in the NMRPipe initialization script,"
   echo "     to help support file server configurations." 
   echo "  4. If specifying a destination directory with the +dest option,"
   echo "     specify the the full path beginning with the / character."
   echo "  5. This script expects to find the installation archive"
   echo "     files NMRPipeX.tZ etc., in the current directory; use the"
   echo "     +src option to override this." 
   echo "  6. This script uses the auxillary script binval.com, that"
   echo "     should be present along with the install.com script."
   echo "  7. If the current executable path is too long (i.e. long enough to"
   echo "     generate a warning message) this install procedure may not work."
   echo "  8. NMRPipe facilities often capture output produced by C-shell"
   echo "     scripts; therefore, .cshrc and similar initialization files"
   echo "     MUST NOT produce output or require input when invoked in"
   echo "     non-interactive mode."
   echo "  9. Windows XP: Use the +sfu homeDir option to automatically"
   echo "     establish a HOME environment.  Use +sfu none to skip this step."
   echo " 10. This script performs post-install software and graphics tests."
   echo "     In some cases, the tests will generate warning or error messages"
   echo "     even if the install completes properly.  In some cases, these"
   echo "     tests might fail in a way that interrupts the completion of the"
   echo "     installation.  If there are problems, try the +nopost option"
   echo "     to suppress these post-install tests."
   echo " 11. RESTART the computer if possible after NMRPipe installation,"
   echo "     or after adjustment of the .cshrc file or other configuration"
   echo "     changes."
   echo " 12. This script requires that the following programs are"
   echo "     already on the executable path and are not aliased:"
   echo ""
   echo "     $cmdList $x11List"
   echo "-------------------------------------------------------------------------------"
   echo " NMRPipe:  https://ibbr.umd.edu/nmrpipe http://www.nmrpipe.com"
   echo " Contact:  frank.delaglio@nist.gov"
   echo "-------------------------------------------------------------------------------"
   exit 0
endif

set origDir = (`pwd`)
set autoDir = "~/pipe" 

#
# Auto mode: 

@ i = 1

while ($i <= $#argv)
   if ($argv[$i] == +auto || X$argv[$i] == X-auto) then
      set autoFlag = 1
   endif

   if ($argv[$i] == +autoDir || X$argv[$i] == X-autoDir) then
      @ i++
      set autoDir = $argv[$i]
   endif

   @ i++
end

if ($autoFlag) then
   if ($?NMRBASE) then
      echo "NMRPipe Auto-Install: an NMRPipe Environment Already Exists."
      echo ""
      echo "Remove NMRPipe initialization commands from the .cshrc file"
      echo "in your login directory and log out before continuing."
      echo ""
      echo "If the .cshrc file has no other commands, delete it; it will"
      echo "be re-created automatically during the NMRPipe installation."
      exit 0
   endif

   if (-e $autoDir) then
       echo "NMRPipe Install: Install Directory $autoDir already exists."
       echo "Delete or rename it before performing the installation."
       echo "Set the Auto-Install directory via the +autoDir option."
       exit 0
   endif

   mkdir $autoDir
   cd    $autoDir

   set testCmd = (`which curl |& grep -F -iv ridiculous `)

   if ($#testCmd == 1) then
      set getCmnd = "curl -O"
   else
      set getCmnd = "wget"
   endif

   foreach f (install.com binval.com NMRPipeX.tZ s.tZ dyn.tZ talos_nmrPipe.tZ plugin.smile.tZ)
      echo "NMRPipe Auto-Install: $getCmnd $f"
      /bin/rm -f $f
      $getCmnd $f
   end

   chmod a+rx *.com
   chmod a+r  *.tZ 

   echo ""
endif
 
#
# Find the binary type for this system (binval.com sets binType and trueType):

unset noclobber

echo "Finding executable type ..."

if (!(-e ./binval.com)) then
   if (-e ./binval.com.sh) then
       echo "Notice: copying binval.com.sh to binval.com ..."
      /bin/cp binval.com.sh binval.com
   endif
endif

if (!(-e ./binval.com)) then
   echo "Error: missing file binval.com ... exiting now."
   exit 1
endif

set    noEcho = 1
source ./binval.com
unset  noEcho

#
# Extract arguments, reset type, source, dest dir if needed:

set i = 1

if ($#argv == 1) then
   set baseDir = $argv[1]
endif

while ($i <= $#argv)
   if ($argv[$i] == +test) then
      set testFlag = 1
   endif

   if ($argv[$i] == +src || X$argv[$i] == X-src) then
      @ i++
      set srcDir = $argv[$i]
   endif

   if ($argv[$i] == +dest || X$argv[$i] == X-dest) then
      @ i++
      set baseDir = $argv[$i]
   endif

   if ($argv[$i] == +type || X$argv[$i] == X-type) then
      @ i++
      set binType  = $argv[$i]
      set typeFlag = 1
   endif

   if ($argv[$i] == +sfu || X$argv[$i] == X-sfu) then
      @ i++
      set homeDir = $argv[$i]
   endif

   if ($argv[$i] == +tclFontMode || X$argv[$i] == X-tclFontMode) then
      @ i++
      set tclFontMode = $argv[$i]
   endif

   if ($argv[$i] == +tclVarFont || X$argv[$i] == X-tclVarFont) then
      @ i++
      set tclVarFont = $argv[$i]
   endif

   if ($argv[$i] == +tclFixedFont || X$argv[$i] == X-tclFixedFont) then
      @ i++
      set tclFixedFont = $argv[$i]
   endif

   if ($argv[$i] == +quoteEnv || X$argv[$i] == X-quoteEnv) then
      set quoteFlag = 1
   endif

   if ($argv[$i] == +noquoteEnv || X$argv[$i] == X-noquoteEnv) then
      set quoteFlag = 0
   endif

   if ($argv[$i] == +post || X$argv[$i] == X-post) then
      set postFlag = 1
   endif

   if ($argv[$i] == +nopost || X$argv[$i] == X-nopost) then
      set postFlag = 0
   endif

   if ($argv[$i] == +nounpack || X$argv[$i] == X-nounpack) then
      set unpackFlag = 0
   endif

   if ($argv[$i] == +nocshrc || X$argv[$i] == X-nocshrc) then
      set cshrcFlag = 0
   endif

   if ($argv[$i] == +noinit || X$argv[$i] == X-noinit) then
      set initFlag = 0
   endif

   if ($argv[$i] == +vnc || X$argv[$i] == X-vnc) then
      set vncFlag = 1
   endif

   if ($argv[$i] == +nmrbox || X$argv[$i] == X-nmrbox) then
      set nmrboxFlag = 1
   endif

   @ i++
end

if ($baseDir == ".") then
   set baseDir = (`pwd`)
endif

#
# Special handling:

set txtManFlag = 0
set xnotifyExe = xnotify.exe

setenv NMR_TCLTK8 TRUE

if ($binType == mac || $binType == mac11 || $binType == mac11_64 || $binType == mac_arm64) then
   set xnotifyExe = "xnotify.app -timer 10"
endif

if ($trueType == FreeBSD) then
   limit descriptors 200
endif

if ($binType == winxp) then
   set xnotifyExe = xnotify.exe
   set txtManFlag = 1
   setenv SHELL /bin/csh
endif

#
# Test for shell:

if ($postFlag == 0) then
   echo "Skipping Pre-Install Test for account with C-Shell default ..."
else
   if ($?SHELL) then
      set shellInfo = (`printenv SHELL | grep -F csh | wc`)

      if ($shellInfo[1] == 0) then
          echo ""
          echo "*** WARNING ***"
          echo " The current account might not be be using a C-Shell as default."
          echo " Installation and use of NMRPipe usually requires use of the"
          echo " C-Shell /bin/csh or /bin/tcsh."
          echo ""
          echo "You can ignore this message if you are sure that you are"
          echo "currently running the C-shell."
          echo ""
      endif
   else
      echo ""
      echo "*** NOTICE ***"
      echo " There is no environment variable SHELL defining the default shell."
      echo " Installation and use of NMRPipe usually requires use of the"
      echo " C-Shell /bin/csh or /bin/tcsh."
      echo ""
      echo "You can ignore this message if you are sure that you are"
      echo "currently running the C-shell."
      echo ""
   endif
endif

#
# Test that non-interactive C-shell scripts produce no extra output.

if ($postFlag == 0) then
   echo "Skipping Pre-Install Test for extra output from non-interactive C-shell scripts."
else
   echo "Testing that non-interactive C-shell scripts produce no extra output."

   if (-e testcsh.com) then
      /bin/rm testcsh.com
   endif

   echo "#\!/bin/csh" >  testcsh.com
   echo "echo 1"      >> testcsh.com

   chmod a+x testcsh.com

   set cInfo = (`./testcsh.com | wc -c` 666)

   if ($cInfo[1] >= 3) then
      echo "*** IMPORTANT WARNING ***"
      echo "A test C-shell script produced unexpected output."
      echo "  NMRPipe requires that C-shell initialization does not produce"
      echo "  any output; adjust .cshrc or related files as needed." 
   endif
 
   /bin/rm testcsh.com
endif

#
# Report results:

echo "Executable type for this installation is $binType ..."

if ($binType != $trueType) then
   echo "Actual type of this system is $trueType ..."
endif

if ($testFlag == 1) then
   echo "Test mode ON, now exiting ..."
   exit 0
endif

#
# Check for custom files that should not be replaced:

if ($unpackFlag) then
   if (-d nmrtxt) then
      echo ""
      echo "Warning: There seems to be an existing version of NMRPipe."
      echo " It is recommended to backup the existing version, remove"
      echo " it, and perform a clean installation.  Before installing,"
      echo " remove any NMRPipe initialization commands in  the  home"
      echo " directory file .cshrc and log out.  If the  .cshrc  file"
      echo " contains only NMRPipe  initialization  commands,  delete"
      echo " it; a new version will be created during installation."
      echo ""
   endif
 
   if (-e std/com) then
      if (!(-d prev)) then
         mkdir prev
      endif

      mv std prev/std.$$
 
      echo ""
      echo "Renaming existing directory std to prev/std.$$"
      echo ""
   endif
endif

#
# Find the base directory for installation:

set optList   = (std talos talos_nmrPipe dyn mfr pdbH acme prime data fdm valpha_all valpha valpha_hn valpha_time valpha_demo)
set patchList = (s v nmrpatch nmrpatch1 nmrpatch2 nmrpatch3 nmrpatch4)
set dateStr   = (`date | grep -F -iv ridiculous`)

echo "Checking installation directory ..."

switch ($baseDir)
   case /\*
      echo "Installation directory is specified as a full path ..."
      breaksw
   default:
      echo "Error: installation directory is not a full path."
      @ errCount++
      exit 1
      breaksw   
endsw

if (!(-e $baseDir)) then
   echo "Notice: failed to find installation directory $baseDir"
   @ errCount++
endif

echo "Install source:      $srcDir"
echo "Install destination: $baseDir"

#
# Unpack tar files if needed:
# Test for all-inclusive archives:

if ($unpackFlag) then
   date >> tar.log

   foreach ii (NMRPipeX NMRPipeL)
      set i = $srcDir/$ii
   
      if (-e $i.tZ) then
         echo "Unpacking file $i.tZ, logfile is tar.log ..."
   
         if (!(-r $i.tZ)) then
            echo Attempting to update protection of archive to +r
            chmod a+r $i.tZ
         endif

         cat $i.tZ | zcat | tar xvf - >>& tar.log
         @ tarCount ++
      endif
   end

   foreach ii ($optList)
      set i = $srcDir/$ii

      if (-e $i.tZ) then
         echo "Unpacking file $i.tZ, logfile is tar.log ..."

         if (!(-r $i.tZ)) then
            echo Attempting to update protection of archive to +r
            chmod a+r $i.tZ
         endif

         cat $i.tZ | zcat | tar xvf - >>& tar.log
      else if (-e $i.tgz) then
         echo "Unpacking file $i.tgz, logfile is tar.log ..."

         if (!(-r $i.tgz)) then
            echo Attempting to update protection of archive to +r
            chmod a+r $i.tgz
         endif

         cat $i.tgz | zcat | tar xvf - >>& tar.log
      else if (-e $i.tar.Z) then
         echo "Unpacking file $i.tar.Z, logfile is tar.log ..."

         if (!(-r $i.tar.Z)) then
            echo Attempting to update protection of archive to +r
            chmod a+r $i.tar.Z
         endif

         cat $i.tar.Z | zcat | tar xvf - >>& tar.log
      else if (-e $i.tar) then
         echo "Unpacking file $i.tar, logfile is tar.log ..."

         if (!(-r $i.tar)) then
            echo Attempting to update protection of archive to +r
            chmod a+r $i.tar
         endif

         tar xvf $i.tar >>& tar.log
      endif
   end

   if ($tarCount == 0) then
      echo "Warning: no software was installed."
   endif
endif

#
# Adjust linux9 to linux if needed:

set testCmd1 = (0 0 0)
set testCmd9 = (0 0 0)

if ($typeFlag == 0) then
   if ($binType == linux9) then
      if (-e nmrbin.linux9/bin2pipe) then
         echo "Checking if linux9 version is suitable ..."

         chmod a+rwx nmrbin.linux9/bin2pipe

         set testCmd9 = (`nmrbin.linux9/bin2pipe -help |& wc | grep -F -iv ridiculous`)

         if ($testCmd9[1] > 100) then
            echo " Test program for version +type linux9 succeeded;"
         else
            echo " Test program for version +type linux9 failed."

            if (-e nmrbin.linux/bin2pipe) then
               chmod a+rwx nmrbin.linux/bin2pipe

               set testCmd1 = (`nmrbin.linux/bin2pipe -help |& wc | grep -F -iv ridiculous`)

               if ($testCmd1[1] > 100) then
                  echo " Test program for version +type linux succeeded;"
                  echo " Resetting install type from linux9 to linux."

                  set binType = linux
               endif
            else
               echo "*** Warning: try installing with the +type linux option."
               @ errCount++
            endif
         endif
      endif
   endif

   if ($binType == linux231_64) then
      if (-e nmrbin.linux231_64/bin2pipe) then
         echo "Checking if linux231_64 version is suitable ..."

         chmod a+rwx nmrbin.linux231_64/bin2pipe

         set testCmd231 = (`nmrbin.linux231_64/bin2pipe -help |& wc | grep -F -iv ridiculous`)

         if ($testCmd231[1] > 100) then
            echo " Test program for version +type linux231_64 succeeded;"
         else
            echo " Test program for version +type linux231_64 failed."

            if (-e nmrbin.linux212_64/bin2pipe) then
               chmod a+rwx nmrbin.linux212_64/bin2pipe

               set testCmd212 = (`nmrbin.linux212_64/bin2pipe -help |& wc | grep -F -iv ridiculous`)

               if ($testCmd212[1] > 100) then
                  echo " Test program for version +type linux212_64 succeeded;"
                  echo " Resetting install type from linux231_64 to linux212_64."

                  set binType = linux212_64
               endif
            else
               echo "*** Warning: tests indicate that NMRPipe versions might not be"
               echo "    suitable for this version of linux."
               @ errCount++
            endif
         endif
      endif
   endif
endif

#
# Check for nmrbin nmrtxt nmrtcl com:

set binDir      = nmrbin.$binType
set extraBinDir = nmrbin_extra.$binType

if ($unpackFlag) then
   foreach i ($binDir nmrtxt nmrtcl com)
      if (!(-d $i)) then
         echo "Error: Failed to find software directory $i"
         echo "Check that the proper install files are available."
         echo "Check that the base directory is specified correctly."
         echo "Check write permissions and free space on the base directory."
         @ errCount++
         exit 1
      endif
   end
endif

#
# Manual installation and MANPATH:

if ($unpackFlag) then
   if (!(-d man)) then
      echo "Creating directory $baseDir/man ..."
      mkdir man
   endif

   if ($txtManFlag) then
      if (!(-d man/cat1)) then
         echo "Creating directory $baseDir/man/cat1 ..."
         mkdir man/cat1
      endif
   else
      if (!(-d man/man1)) then
         echo "Creating directory $baseDir/man/man1 ..."
         mkdir man/man1
      endif
   endif

   if ($txtManFlag) then
      echo "Installing manual pages man_txt/*.1 ..."
      cp nmrtxt/man_txt/*.1 man/cat1
   else
      if ($binType == sgi6x) then
         set testCmd = (no troff)
      else
         set testCmd = (`which troff |& grep -F -iv ridiculous`)
      endif

      if ($#testCmd == 1) then
         echo "Installing manual pages man_1/*.1 ..."
         cp nmrtxt/man_1/*.1 man/man1
      else
         echo "Installing manual pages man_z/*.z ..."
         cp nmrtxt/man_z/*.z man/man1
      endif
   endif
endif

set manPath  = $baseDir/man

set testList = (/usr/man /usr/share/man /usr/catman /usr/share/catman /X11/usr/man /usr/lang/man /usr/local/man)

foreach i ($testList)
   if (-d $i) then
      set manPath = ($manPath':'$i)
   endif
end

#
# Protections and paths for Tk/Tcl scripts:

echo "Setting environment variables and aliases ..."

alias  fgrep 'grep -F'
alias  egrep 'grep -E'

setenv NMR_CHECKARGS   ALL
setenv NMR_FSCHECK     NO
setenv NMRBASE         $baseDir
setenv NMRTXT          $baseDir/nmrtxt
setenv NMRBIN          $baseDir/$binDir

setenv TCL_LIBRARY     ${NMRBASE}/nmrtcl/tcl8.4
setenv TK_LIBRARY      ${NMRBASE}/nmrtcl/tk8.4
setenv BLT_LIBRARY     ${NMRBASE}/nmrtcl/blt2.4
setenv NMRPIPE_TCL_LIB ${NMRBASE}/nmrtcl/tcl8.4
setenv NMRPIPE_TK_LIB  ${NMRBASE}/nmrtcl/tk8.4
setenv NMRPIPE_BLT_LIB ${NMRBASE}/nmrtcl/blt2.4

setenv TCLPATH     $baseDir/com
setenv TALOS_DIR   $baseDir/talos
setenv TALOSP_DIR  $baseDir/talosplus
setenv TALOSN_DIR  $baseDir/talosn
setenv SPARTAP_DIR $baseDir/spartaplus
setenv PROMEGA_DIR $baseDir/promega
setenv DYNAMO_DIR  $baseDir/dynamo
setenv SURF_DIR    $baseDir/dynamo/surface
setenv DC_DIR      $baseDir/DC
setenv PDBH_DIR    $baseDir/pdb/pdbH
setenv PDBH_LIST   $baseDir/pdb/pdbH.list

if ($binType == winxp) then
   echo "Updating protections (this step is slow under Windows XP) ..."
else
   echo "Updating protections ..."
endif

./install.com +prot $binDir

#
# Plugins:

set pluginList = (`find $srcDir -maxdepth 1 -name plugin.\*.tZ`)

set fnStr   = ""
set exeStr  = ""
set infoStr = ""

if ($unpackFlag) then
   foreach p ($pluginList)
      set pInfo = (`echo $p | tr '.' ' '`)
      set pDir  = $pInfo[2]

      echo "Unpacking plugin $pDir from file $p, logfile is tar.log ..."
      cat $p | zcat | tar xvf - >>& tar.log

      if (!(-d $pDir)) then
         echo " Error finding expected plugin directory $pDir"
         echo " Skipping installation of plugin $pDir"
         continue
      endif

      if (!(-e $pDir/NMR_PLUGIN_FN)) then
         echo " Error finding expected plugin file $pDir/NMR_PLUGIN_FN"
         echo " Skipping installation of plugin $pDir"
         continue
      endif

      if (!(-e $pDir/NMR_PLUGIN_EXE)) then
         echo " Error finding expected plugin file $pDir/NMR_PLUGIN_EXE"
         echo " Skipping installation of plugin $pDir"
         continue
      endif

      if (!(-e $pDir/NMR_PLUGIN_INFO)) then
         echo " Error finding expected plugin file $pDir/NMR_PLUGIN_INFO"
         echo " Skipping installation of plugin $pDir"
         continue
      endif

      @ pluginCount++

      set thisFn   = (`cat $pDir/NMR_PLUGIN_FN`)
      set thisExe  = (`cat $pDir/NMR_PLUGIN_EXE`)
      set thisInfo = (`cat $pDir/NMR_PLUGIN_INFO | tr '[]' ' '`)

      if ($pluginCount == 1) then
         set fnStr   = (`printf "%s" "$thisFn"`)
         set exeStr  = (`printf "%s" "$thisExe"`)
         set infoStr = (`printf "%s" "$thisInfo"`)
      else
         set fnStr   = (`printf "%s:%s" "$fnStr"   "$thisFn"`)
         set exeStr  = (`printf "%s:%s" "$exeStr"  "$thisExe"`)
         set infoStr = (`printf "%s:%s" "$infoStr" "$thisInfo"`)
      endif
   end
endif

#
# Special handling for patches:

if ($unpackFlag) then
   foreach ii ($patchList)
      set i = $srcDir/$ii

      if (-e $i.tZ) then
         echo "Unpacking update file $i.tZ, logfile is tar.log ..."
         cat $i.tZ | zcat | tar xvf - >>& tar.log
         @ patchCount++

         if ($binType == winxp) then
            echo "Updating protections (this step is slow under Windows XP) ..."
         else
            echo "Updating protections ..."
         endif

         ./install.com +prot $binDir
      else if (-e $i.tgz) then
         echo "Unpacking update file $i.tgz, logfile is tar.log ..."
         cat $i.tgz | zcat | tar xvf - >>& tar.log
         @ patchCount++

         if ($binType == winxp) then
            echo "Updating protections (this step is slow under Windows XP) ..."
         else
            echo "Updating protections ..."
         endif

         ./install.com +prot $binDir
      endif
   end
endif

#
# Special handling for establishing winxp Interix environ:

rehash

if ($binType == winxp) then
   if (!($?DISPLAY)) then
      setenv DISPLAY 127.0.0.1:0
   endif

   if ($homeDir == home) then
      set homeDir = $baseDir/home
   endif

   if ($homeDir != none) then
      switch ($homeDir)
         case /\*
            echo "Home directory is specified as a full path ..."
            breaksw
         default:
            echo "Error: home directory is not a full path."
            echo $homeDir
            @ errCount++
            exit 1
            breaksw
      endsw

      if (!(-e $homeDir)) then
         mkdir $homeDir
      endif

      echo setenv HOME  $homeDir  >  nmrbin.winxp/winxp/csh.login2
      echo setenv SHELL /bin/csh  >> nmrbin.winxp/winxp/csh.login2

      cat nmrbin.winxp/winxp/csh.login1 \
          nmrbin.winxp/winxp/csh.login2 \
          nmrbin.winxp/winxp/csh.login3 > nmrbin.winxp/winxp/csh.login

      echo cd $homeDir  >  nmrbin.winxp/winxp/profile.lcl
      echo csh -l       >> nmrbin.winxp/winxp/profile.lcl
      echo exit 0       >> nmrbin.winxp/winxp/profile.lcl

      /bin/csh nmrbin.winxp/winxp/update.csh

      if (!(-e $homeDir/.cshrc)) then
         echo Creating $homeDir/.cshrc

         cat nmrbin.winxp/winxp/dot.cshrc  \
         | sed -e  "s,NMRBASE,$baseDir," \
         | sed -e "s,//,/," > $homeDir/.cshrc

         chmod u+rwx $homeDir/.cshrc
      endif
   endif
endif

#
# Create font initialization, even if it will not be used:

set fontFlag = 0

if ($?NMRFONTSET) then
   echo "Font settings for a previous installation of NMRPipe exist."
   set fontFlag = 1
endif

if ($binType == sgi5 || $binType == sgi6) then
   set fontType = bdf
   set fontFlag = 1
else if ($binType == linux || $binType == linux9 || $binType == linux212_64 || $binType == linux231_64 || $binType == linux235_64) then 
   set fontType = gz
else
   set fontType = None
endif

if (!($unpackFlag)) then
   mkdir com
endif

echo "#\!/bin/csh -f" > com/font.com 
echo ""  >> com/font.com 
echo "#" >> com/font.com 
echo "# NMRPipe System Font script, $dateStr." >> com/font.com
echo "# If this script does not work for your system, try"  >> com/font.com
echo "# interchanging the font definitions from gz to bdf"  >> com/font.com
echo ""  >> com/font.com
echo "if ("'$?NMRFONTSET'") then" >> com/font.com
echo "   exit 0" >> com/font.com
echo "endif" >> com/font.com
echo ""  >> com/font.com
echo "if (!(-d $baseDir/XView/fonts/$fontType)) then" >> com/font.com
echo "   exit 0" >> com/font.com
echo "endif" >> com/font.com
echo ""  >> com/font.com
echo "if ("'$?DISPLAY'") then" >> com/font.com

if ($vncFlag) then
   echo 'if ("`echo $DISPLAY | cut -c 1`" != ":") then' >> com/font.com
   echo "   setenv NMRFONTSET TRUE"                     >> com/font.com
   echo "   exit 0"                                     >> com/font.com 
   echo "endif"                                         >> com/font.com
   echo ""                                              >> com/font.com
endif

echo "   set fontInfo = " '(`xset -q | grep -F XView/fonts | wc`)' >> com/font.com
echo ""  >> com/font.com
echo "   if ("'$fontInfo[1]'" == 0) then" >> com/font.com
echo "      xset fp+ $baseDir/XView/fonts/$fontType/75dpi/"     >> com/font.com
echo "      xset fp+ $baseDir/XView/fonts/$fontType/misc/"      >> com/font.com
echo "      xset fp+ $baseDir/XView/fonts/$fontType/X11/75dpi/" >> com/font.com
echo "      xset fp  rehash" >> com/font.com
echo "#     echo NMR Fonts: $baseDir/XView/fonts/$fontType" >> com/font.com
echo "#  else" >> com/font.com
echo "#     echo NMR font path is already set." >> com/font.com
echo "   endif" >> com/font.com
echo "endif" >> com/font.com
echo ""   >> com/font.com
echo "setenv NMRFONTSET TRUE" >> com/font.com

chmod a+rx com/font.com

#
# Find likely X11 path:

set testCmd = (`which xset |& grep -F -iv ridiculous`)
set x11Path = ""

if ($#testCmd != 1) then
   foreach i (/usr/X11R6/bin /usr/X11/bin /usr/bin/X11 /usr/openwin/bin)
      if (-x $i/xset) then
         set x11Path = $i
         break
      endif
   end
endif

#
# Build the user initialization script:

if ($initFlag == 0) then
   set scriptName = script.out

   echo "Skipping Creation of initialization script com/nmrInit.$binType.com ..."
   echo ""

   echo "#\!/bin/csh -f" > $scriptName
   chmod a+rx $scriptName
else
   set scriptName = com/nmrInit.$binType.com
   
   echo "Creating initialization script $scriptName ..."
   echo ""

   echo "#\!/bin/csh -f" > $scriptName
   chmod a+rx $scriptName

   cd com
      ln -s nmrInit.$binType.com nmrInit.link.com
   cd ../
endif

echo ""  >> $scriptName
echo "#" >> $scriptName
echo "# NMRPipe System Environment script, $dateStr." >> $scriptName
echo ""  >> $scriptName
echo ""  >> $scriptName
echo "#" >> $scriptName
echo "# Standard Expiration Override:"  >> $scriptName
echo "# setenv NMR_CONT CORRECT_ALL"    >> $scriptName
echo ""  >> $scriptName
echo "#" >> $scriptName
echo "# NMRPipe I/O Optimization." >> $scriptName
echo "# These settings may improve NMRPipe pipeline performance:" >> $scriptName
echo "" >> $scriptName
echo "setenv NMR_IO_TIMEOUT 0" >> $scriptName
echo "setenv NMR_IO_SELECT  0" >> $scriptName
echo "setenv NMR_AUTOSWAP   1" >> $scriptName
echo ""  >> $scriptName

if ($pluginCount != 0) then
   echo "#" >> $scriptName
   echo "# NMRPipe Plugins:" >> $scriptName
   echo ""
   echo "setenv NMR_PLUGIN_FN   " $fnStr   >> $scriptName
   echo "setenv NMR_PLUGIN_EXE  " $exeStr  >> $scriptName
   echo "setenv NMR_PLUGIN_INFO " $infoStr >> $scriptName
   echo ""
endif

echo ""  >> $scriptName
echo "#" >> $scriptName
echo "# Useful definitions:" >> $scriptName
echo "" >> $scriptName
echo "set prompt  = "'"$cwd% "'"" >> $scriptName
echo "set history = 256" >> $scriptName
echo "" >> $scriptName
echo "alias u      'chmod a+rx *.com *.tcl'"  >> $scriptName
echo "alias fgrep  'grep -F'"                 >> $scriptName
echo "alias egrep  'grep -E'"                 >> $scriptName
echo ""  >> $scriptName
echo "alias h      'history'" >> $scriptName
echo "alias rm     'rm -i'" >> $scriptName
echo "alias cd     'cd  \\!*; set prompt = "'"$cwd% "'"'" >> $scriptName
echo "alias dirs   'ls -l | grep -E -e ^d'" >> $scriptName
echo "alias links  'ls -l | grep -E -e ^l'" >> $scriptName
echo ""  >> $scriptName
echo "if ("'$?MANPATH'") then" >> $scriptName
echo "   setenv MANPATH $manPath"':${MANPATH}' >> $scriptName
echo "else" >> $scriptName
echo "   setenv MANPATH $manPath"              >> $scriptName
echo "endif" >> $scriptName
echo ""  >> $scriptName

if ((-e $extraBinDir) || (-e $baseDir/$extraBinDir)) then
   echo "set path = (. $baseDir/$binDir $baseDir/com $baseDir/$extraBinDir" $x11Path '$path'")" >> $scriptName
else
   echo "set path = (. $baseDir/$binDir $baseDir/com" $x11Path '$path'")" >> $scriptName
endif

echo ""  >> $scriptName

if ($trueType == FreeBSD) then
   echo ""  >> $scriptName
   echo "limit descriptors 200" >> $scriptName
   echo ""  >> $scriptName
endif

if (-e /opt/X11/lib/flat_namespace) then
   set altXLib = "/opt/X11/lib/flat_namespace:"
else
   set altXLib = ""
endif

if (($binType == sol) && (-d /usr/openwin)) then
   set val = LD_LIBRARY_PATH
   echo "" >> $scriptName
   echo "if ("'$?LD_LIBRARY_PATH'") then" >> $scriptName
   echo "   setenv $val "'${LD_LIBRARY_PATH}:/usr/openwin/lib'  >> $scriptName
   echo "else" >> $scriptName
   echo "   setenv $val /usr/openwin/lib" >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
   echo "if ("'!($?OPENWINHOME)'") then" >> $scriptName
   echo "   setenv OPENWINHOME /usr/openwin" >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
endif

if ($binType != hp) then
   set val = "LD_LIBRARY_PATH $altXLib$baseDir/$binDir/lib"

   echo "" >> $scriptName
   echo "if ("'$?LD_LIBRARY_PATH'") then" >> $scriptName
   echo "   setenv $val"':${LD_LIBRARY_PATH}'  >> $scriptName
   echo "else" >> $scriptName
   echo "   setenv $val" >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
else
   set val = "SHLIB_PATH $altXLib$baseDir/$binDir/lib"
   echo "" >> $scriptName
   echo "if ("'$?SHLIB_PATH'") then" >> $scriptName
   echo "   setenv $val"':${SHLIB_PATH}'  >> $scriptName
   echo "else" >> $scriptName
   echo "   setenv $val" >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
endif

if ($binType == mac || $binType == mac11 || $binType == mac11_64 || $binType == mac_arm64) then
   set val = "DYLD_LIBRARY_PATH"
   echo "" >> $scriptName
   echo "if ("'$?DYLD_LIBRARY_PATH'") then" >> $scriptName
   echo "   setenv $val "$altXLib'${DYLD_LIBRARY_PATH}:${LD_LIBRARY_PATH}' >> $scriptName
   echo "else" >> $scriptName
   echo "   setenv $val "'${LD_LIBRARY_PATH}' >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
   echo "setenv LC_CTYPE C" >> $scriptName
   echo "setenv LC_ALL   C" >> $scriptName
   echo "" >> $scriptName
endif

echo "" >> $scriptName
echo "if ("'!($?OPENWINHOME)'") then" >> $scriptName
echo "   if (-d $baseDir/$binDir/openwin) then" >> $scriptName
echo "      setenv OPENWINHOME $baseDir/$binDir/openwin" >> $scriptName
echo "   endif" >> $scriptName
echo "endif" >> $scriptName
echo "" >> $scriptName

set dStr = "defaults write com.apple.x11"

if ($binType == mac || $binType == mac11 || $binType == mac11_64 || $binType == mac_arm64) then
   echo "" >> $scriptName
   echo "if ("'$?DISPLAY'" && "'!($?NMR_MACX11FIX)'") then" >> $scriptName
   echo "    $dStr wm_ffm -bool true"           >> $scriptName
   echo "    $dStr wm_click_through -bool true" >> $scriptName
   echo "    setenv NMR_MACX11FIX true"         >> $scriptName
   echo "endif" >> $scriptName
   echo "" >> $scriptName
endif

echo "setenv NMR_CHECKARGS   ALL"                    >> $scriptName
echo "setenv NMR_FSCHECK     NO"                     >> $scriptName
echo "setenv NMRBASE         $baseDir"               >> $scriptName
echo "setenv NMRBINTYPE      $binType"               >> $scriptName
echo "setenv NMRTXT          $baseDir/nmrtxt"        >> $scriptName
echo "setenv NMRBIN          $baseDir/$binDir"       >> $scriptName
echo "setenv TCLPATH         $baseDir/com"           >> $scriptName
echo "setenv TALOS_DIR       $baseDir/talos"         >> $scriptName
echo "setenv TALOSP_DIR      $baseDir/talosplus"     >> $scriptName
echo "setenv TALOSN_DIR      $baseDir/talosn"        >> $scriptName
echo "setenv SPARTAP_DIR     $baseDir/spartaplus"    >> $scriptName
echo "setenv PROMEGA_DIR     $baseDir/promega"       >> $scriptName
echo "" >> $scriptName

echo "" >> $scriptName
echo "setenv NMR_TCLTK8      TRUE" >> $scriptName
echo "setenv TCL_LIBRARY     ${NMRBASE}/nmrtcl/tcl8.4"   >> $scriptName
echo "setenv TK_LIBRARY      ${NMRBASE}/nmrtcl/tk8.4"    >> $scriptName
echo "setenv BLT_LIBRARY     ${NMRBASE}/nmrtcl/blt2.4"   >> $scriptName
echo "setenv NMRPIPE_TCL_LIB ${NMRBASE}/nmrtcl/tcl8.4"   >> $scriptName
echo "setenv NMRPIPE_TK_LIB  ${NMRBASE}/nmrtcl/tk8.4"    >> $scriptName
echo "setenv NMRPIPE_BLT_LIB ${NMRBASE}/nmrtcl/blt2.4"   >> $scriptName
echo ""  >> $scriptName
echo "#" >> $scriptName
echo "# setenv NMR_TCL_SETFONTS  0 = Never, 1 = Always, 2 = When Needed" >> $scriptName
echo "# setenv NMR_TCL_VARFONT   family=Lucida,size=9,weight=bold"       >> $scriptName
echo "# setenv NMR_TCL_FIXEDFONT family=Fixed,size=9,weight=normal"      >> $scriptName
echo ""  >> $scriptName
echo "setenv NMR_TCL_SETFONTS  $tclFontMode"             >> $scriptName
echo "setenv NMR_TCL_VARFONT   $tclVarFont"              >> $scriptName
echo "setenv NMR_TCL_FIXEDFONT $tclFixedFont"            >> $scriptName

if ($nmrboxFlag) then
   echo "" >> $scriptName
   echo "setenv NMR_NMRBOX      1"                       >> $scriptName
endif

set smallfont  = "-adobe-helvetica-medium-r-*-*-*-100-*-*-*-*-*-*"
set bigfont    = "-adobe-helvetica-medium-r-*-*-*-180-*-*-*-*-*-*"
set stdfont    = "-adobe-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*"
set boldfont   = "-adobe-helvetica-bold-r-*-*-*-120-*-*-*-*-*-*"
set fixedfont  = "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*"
set typingfont = "-*-courier-medium-r-*-*-*-100-*-*-*-*-*-*"
set axisfont   = "-*-lucidatypewriter-bold-r-*-*-*-120-*-*-*-*-*-*"

echo "" >> $scriptName
echo "# setenv NMRPIPE_SMALLFONT  "'"'"$smallfont"'"'   >> $scriptName
echo "# setenv NMRPIPE_BIGFONT    "'"'"$bigfont"'"'     >> $scriptName
echo "# setenv NMRPIPE_STDFONT    "'"'"$stdfont"'"'     >> $scriptName
echo "# setenv NMRPIPE_BOLDFONT   "'"'"$boldfont"'"'    >> $scriptName
echo "# setenv NMRPIPE_FIXEDFONT  "'"'"$fixedfont"'"'   >> $scriptName
echo "# setenv NMRPIPE_TYPINGFONT "'"'"$typingfont"'"'  >> $scriptName
echo "# setenv NMRPIPE_AXISFONT   "'"'"$axisfont"'"'    >> $scriptName
echo "" >> $scriptName

#
# Test the installation.

if ($postFlag == 0) then
   echo "Skipping Post-Install Software Tests ..."
else 
   echo "Testing the installation ... use option +nopost to suppress tests ..."

   source $scriptName
   rehash

   set testCmd = (`which bin2pipe |& grep -F -iv ridiculous`)

   if ($#testCmd == 1) then
      echo "Found the test program correctly ..."
   else
      echo "Warning: failed to find the test program."
      @ errCount++
   endif

   set testCmd = (`bin2pipe -help |& wc | grep -F -iv ridiculous`)

   if ($testCmd[1] > 100) then
      echo "Executed the test program correctly ..."
   else
      echo "Warning: failed to execute the test program."
      @ errCount++
   endif

   set testCmd = (`man bin2pipe |& wc | grep -F -iv ridiculous`)

   if ($testCmd[1] > 1000) then
      echo "Found the test manual page correctly ..."
   else
      echo "Warning: failed to find the test manual page."
      @ errCount++
   endif
endif

#
# Test for X11 utilities:

set xtermFlag = 0

if ($postFlag == 0) then
   echo "Skipping Post-Install X11 Utilities Tests ..."
else
   echo "Testing for X11 utilities ..."

   set testCmd = (`which xset |& grep -F -iv ridiculous `)

   if ($#testCmd == 1) then
      echo "Found the X11 command xset ..."
   else
      echo ""
      echo "Warning: failed to find the X11 command xset;"
      echo "this command may be required to set font paths."
      echo ""
   endif

   set testCmd = (`which xterm |& grep -F -iv ridiculous`)

   if ($#testCmd == 1) then
      echo "Found the X11 command xterm ..."
      set xtermFlag = 1
   else
      echo ""
      echo "Warning: failed to find the X11 command xterm;"
      echo "this command is required by several NMRPipe programs."
      echo "It is also required for completing the NMRPipe installation."
      echo ""
      @ errCount++
   endif
endif

#
# Test the graphics and fonts:

@ graphCount = 0

if ($postFlag == 0) then
   echo "Skipping Post-Install Font Tests ..."
else
   if (!($?DISPLAY)) then
      echo ""
      echo "Notice: No DISPLAY environment is specified;"
      echo "no graphics tests can be performed."
      echo "Retry the installation with the proper X11 graphics environment."
      echo ""
      set xtermFlag = 0
   endif

   set f1 = '-b&h-lucida-medium-r-normal-sans-*-120-*-*-*-*-*-*'
   set f2 = '-b&h-lucida-medium-r-*-*-*-120-*-*-*-*-*-*'

   if ($binType == linux || $binType == linux9 || $binType == linux212_64 || $binType == linux231_64 || $binType == linux235_64) then 
      echo "Testing for XView Fonts ..."

      if ($xtermFlag == 0) then
         echo "Warning: the xterm program is needed to test for required fonts." 
      else
         set a1 = (`xterm -fn "$f1" -e date |& grep -F lucida | wc`) 
         set a2 = (`xterm -fn "$f2" -e date |& grep -F lucida | wc`) 

         if ($a1[1] || $a2[1]) then
            echo "This system seems to require XView fonts."
            set fontFlag = 1
         else
            echo "This system already seems to have the required XView fonts."
         endif
      endif
   endif

   if ($fontFlag == 1) then
      if (!(-d XView/fonts)) then
         echo ""
         echo "Warning: failed to find font directory $baseDir/XView/fonts"
         echo "If there are font-related problems when using nmrDraw,"
         echo "check the file $baseDir/com/font.com"
         echo ""
      endif
   endif
endif

if ($postFlag == 0) then
   echo "Skipping Post-Install Graphics Tests ..."
else
   if ($?DISPLAY) then
      if ($fontFlag == 1) then
         echo "Executing font initialization script ..."
         com/font.com
      endif

      if ($binType == mac || $binType == mac11 || $binType == mac11_64 || $binType == mac_arm64) then
         if (-e com/fixMacX11.com) then
            echo "Adjusting mac window parameters for XView graphics ..."
            source com/fixMacX11.com
         endif
      endif

      echo "Testing X11/XView graphics, log file is graph.log ..."

      $baseDir/$binDir/$xnotifyExe \
       -Wfsdb "Executed the graphics test correctly ..." \
      |& grep -F -iv ridiculous > graph.log &

      sleep 5

      set testCmd = (`grep -F -i "cannot open display" graph.log | wc`)

      if ($testCmd[1] != 0) then
         echo "Error: graphics connection failed."
         @ graphCount++
      endif

      set testCmd = (`grep -F -i "cannot load font" graph.log | wc`)

      if ($testCmd[1] != 0) then
         echo "Error: graphics program failed to load fonts."
         @ graphCount++
      endif

      set testCmd = (`grep -F -i "error" graph.log | wc`)

      if ($testCmd[1] != 0) then
         @ graphCount++
      endif

      set testCmd = (`grep -F -i "warning" graph.log | wc`)

      if ($testCmd[1] != 0) then
         @ graphCount++
      endif

      if ($graphCount == 0) then
         echo "Executed graphics test correctly ..."
      else
         echo "Error: graphics test generated errors/warnings."
         @ errCount++
      endif
   endif
endif

#
# Done.

set f = README_NMRPIPE_USERS
set c = example.cshrc

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

if (-e $c) then
   /bin/rm $c
endif

if (-e nmrtxt/version.txt) then
   set verTxt = (`cat nmrtxt/version.txt`)
else
   set verTxt = "NMRPipe Version: Unknown"
endif

echo "#\!/bin/csh" >> $c
echo ""  >> $c
echo "#" >> $c
echo "# Echo NMRPipe .cshrc commands created on `date`" >> $c
echo "# Echo see file $baseDir/README_NMRPIPE_USERS for more." >> $c
echo "# $verTxt" >> $c
echo "" >> $c


echo "alias h      'history'" >> $c
echo "alias rm     'rm -i'" >> $c
echo "alias u      'chmod a+x *.com *.tcl'" >> $c
echo "alias cd     'cd  \\!*; set prompt = "'"$cwd% "'"'" >> $c
echo "alias dirs   'ls -l | egrep -e ^d'" >> $c
echo "alias links  'ls -l | egrep -e ^l'" >> $c
echo "" >> $c

echo "---------------------------------------------------------------------"  >> $f
echo " NMRPipe Installation on `date`"                                        >> $f
echo "  $verTxt"                                                              >> $f
echo "  Write to Frank Delaglio with any NMRPipe-related messages."           >> $f
echo "  frank.delaglio@nist.gov"                                              >> $f
echo "---------------------------------------------------------------------"  >> $f

if ($errCount == 0) then
   echo "  The installation seems to have completed without errors." >> $f
endif

if ($errCount != 0) then
   echo "  *** Warning: Errors occurred during the installation.   " >> $f
endif

echo "---------------------------------------------------------------------" >> $f
echo " Previous versions of NMRPipe might have an expiration date."      >> $f
echo " To override the expiration date, use the following environment"   >> $f
echo " variable setting:"                                                >> $f
echo ""                                                                  >> $f
echo "    setenv NMR_CONT CORRECT_ALL"                                   >> $f
echo "---------------------------------------------------------------------" >> $f
echo " NMRPipe requires the C-shell as the default shell; you might"     >> $f
echo " first need to use command chsh or another method  to  change"     >> $f
echo " the default shell to /bin/csh ... see the installation notes"     >> $f
echo " for details about how to change the default shell."               >> $f
echo "---------------------------------------------------------------------" >> $f
echo "" >> $f
echo "  In order to use NMRPipe, users must first execute an"         >> $f
echo "  initialization script,  for instance by adding these"         >> $f
echo "  lines to the end of their .cshrc file:"                       >> $f
echo "" >> $f
echo "     if (-e $baseDir/$scriptName) then" >> $f
echo "        source $baseDir/$scriptName"    >> $f
echo "     endif" >> $f
echo "" >> $f

echo " if (-e $baseDir/$scriptName) then" >> $c
echo "    source $baseDir/$scriptName"    >> $c
echo " endif" >> $c
echo "" >> $c

echo " if (-e $baseDir/pype/env/bin/activate.csh) then" >> $c
echo "    setenv VIRTUAL_ENV_DISABLE_PROMPT 1" >> $c
echo "    source $baseDir/pype/env/bin/activate.csh"   >> $c
echo " endif" >> $c
echo "" >> $c

if (-e dynamo) then
   echo "  In order to use DYNAMO options, users must first execute"  >> $f
   echo "  an initialization script,  for  instance by adding these"  >> $f
   echo "  lines to their .cshrc file after NMRPipe initialization:"  >> $f
   echo "" >> $f
   echo "     if (-e $baseDir/dynamo/com/dynInit.com) then" >> $f
   echo "        source $baseDir/dynamo/com/dynInit.com"    >> $f
   echo "     endif" >> $f

   echo " if (-e $baseDir/dynamo/com/dynInit.com) then" >> $c
   echo "    source $baseDir/dynamo/com/dynInit.com"    >> $c
   echo " endif" >> $c
   echo "" >> $c
endif

if ($binType == mac || $binType == mac11 || $binType == mac11_64 || $binType == mac_arm64) then
   echo "" >> $f
   echo "  Mac OS X Users may also need to adjust X11 Preferences:" >> $f
   echo "  Under Windows Tab, Checkbox ON For: "  >> $f
   echo "      x Click through Inactive Windows"  >> $f
   echo "      x Focus Follows Mouse"             >> $f
   echo "      x Focus on New Windows"            >> $f
   echo "  Mac OS X Users may also need to Terminal Preferences:"      >> $f
   echo "  Under Profiles Settings, Advanced Tab, Checkbox OFF For: "  >> $f
   echo "        Set locale environment variables on startup"          >> $f
endif

if ($binType == linux9 || $binType == linux || $binType == linux212_64 || $binType == linux231_64 || $binType == linux235_64) then
     echo "" >> $f
     echo "Linux systems might require installation of additional fonts,"   >> $f
     echo "commands, and libraries. In particular, 32-bit libraries will"   >> $f
     echo "be needed for some or all of NMRPipe's applications. Installing" >> $f
     echo "the required components will usually require root access."       >> $f
     echo "" >> $f
     echo "Example for CentOS 7 64-bit Linux:" >> $f
     echo "" >> $f
     echo "   Edit the file /etc/yum.conf and add the following line:"      >> $f
     echo "" >> $f
     echo "      multilib_policy=all" >> $f
     echo "" >> $f
     echo "   After adjusting the yum configuration file above:"            >> $f
     echo "" >> $f
     echo "      yum install libgcc"                           >> $f
     echo "      yum install glibc"                            >> $f
     echo "      yum install libX11.so.6"                      >> $f
     echo "      yum install libXext.so.6"                     >> $f
     echo "      yum install libstdc++.so.6"                   >> $f
     echo "      yum install xorg-x11-fonts-75dpi"             >> $f
     echo "      yum install xorg-x11-fonts-ISO8859-1-75dpi"   >> $f
     echo "" >> $f
     echo "Similar linux systems might need additional fonts:" >> $f
     echo "      yum install xorg-x11-fonts-100dpi"            >> $f
     echo "      yum install xorg-x11-fonts-ISO8859-1-100dpi"  >> $f
     echo "      yum install xorg-x11-fonts-misc"              >> $f
     echo "" >> $f
     echo "Example for Ubuntu 20 64-bit Linux:"  >> $f
     echo "" >> $f
     echo "   apt-get install tcsh"          >> $f
     echo "   apt-get install xterm"         >> $f
     echo "   apt-get install lib32z1"       >> $f
     echo "   apt-get install libx11-6:i386" >> $f
     echo "   apt-get install libxext6:i386" >> $f
     echo "   apt-get install xfonts-75dpi"  >> $f
     echo "   apt-get install msttcorefonts" >> $f
     echo "   apt-get install default-jre"   >> $f
     echo "   apt-get install default-jdk"   >> $f
     echo "" >> $f
     echo "" >> $f
endif

echo "" >> $f
echo "  If the nmrDraw graphical application does not run, users" >> $f
echo "  might also need to execute the font initialization script:" >> $f
echo "" >> $f
echo "     if (-e $baseDir/com/font.com) then" >> $f
echo "        source $baseDir/com/font.com"    >> $f
echo "     endif" >> $f

if ($fontFlag == 1) then
   echo "" >> $c
   echo " if (-e $baseDir/com/font.com) then" >> $c
   echo "    source $baseDir/com/font.com"    >> $c
   echo " endif" >> $c
   echo "" >> $c
else
   echo "" >> $c
   echo "# if (-e $baseDir/com/font.com) then" >> $c
   echo "#    source $baseDir/com/font.com"    >> $c
   echo "# endif" >> $c
   echo "" >> $c
endif

echo "" >> $f
echo "  NOTE: RESTART the computer after installing software, adjusting" >> $f
echo "        configurations, or changing the .cshrc file."              >> $f
echo "----------------------------------------------------------------------" >> $f
echo "NMRPipe Home:     www.ibbr.umd.edu/nmrpipe www.nmrpipe.com"        >> $f
echo "NMRPipe Contact:  frank.delaglio@nist.gov"                         >> $f
echo "----------------------------------------------------------------------" >> $f

if ($initFlag != 0) then
   cat $f

   echo ""
   echo The above instructions are recorded in the file $f
   echo An example .cshrc is given in file $c
endif

if ($cshrcFlag == 0) then
   echo ""
   echo "Skipping Creation of an initial ~/.cshrc file ..."
else
   if ($binType != winxp && `whoami` != "root") then
      if (!(-e ~/.cshrc)) then
         echo Attempting to create an initial ~/.cshrc file ...
         cp $c ~/.cshrc
      endif
   endif
endif

#
# Test for unexpected C-shell output again, and for dual .cshrc .tcshrc

if ($postFlag) then
   if (-e testcsh.com) then
      /bin/rm testcsh.com
   endif

   echo "#\!/bin/csh" >  testcsh.com
   echo "echo 1"      >> testcsh.com

   chmod a+x testcsh.com

   set cInfo = (`./testcsh.com | wc -c` 666)

   if ($cInfo[1] >= 3) then
      echo "" 
      echo "*** IMPORTANT WARNING ***"
      echo "A test C-shell script produced unexpected output."
      echo "  NMRPipe requires that C-shell initialization does not produce"
      echo "  any output; adjust .cshrc or related files as needed."
   endif

   /bin/rm testcsh.com

   if ((-e ~/.cshrc) && (-e ~/.tcshrc)) then
      echo "" 
      echo "*** IMPORTANT WARNING ***"
      echo "Two conflicting C-Shell initialization files have been found:"
      echo "" 
      echo "  " ~/.cshrc 
      echo "  " ~/.tcshrc 
      echo "" 
      echo "Adjust or combine these files accordinging so that only one or"
      echo "the other exists." 
   endif
endif

if ($autoFlag) then
   echo ""

   foreach f (NMRPipeX.tZ s.tZ dyn.tZ talos.tZ talos_nmrPipe.tZ plugin.smile.tZ)
      if (-e $f) then
         echo "NMRPipe Auto-Install: Removing File $f"
         /bin/rm -f $f
      endif
   endif
endif

#
# Bottom.