#! /bin/tcsh -f

#
# browse-sessions
#
# This is just a wrapper for unpack_mnc.tcl.  It sets the TCL and TK
# libraries to be version 8.0 (which is uncompatible with other programs)
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:13 $
#    $Revision: 1.10 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA).
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#



echo "browse-sessions is no longer supported, use findsession."
echo "  findsession is a command-line (not graphical utitlity)."
echo "  To learn how to use it, run: findsession -help."
exit 1;


set VERSION = '$Id: browse-sessions,v 1.10 2007/01/06 00:01:13 nicks Exp $';


# Uncomment one of these to set the  default unpacking method.
# Either method can be invoked from the command line by calling
# browse-sessions with either 'ima' or 'minc' (no quotes) as an
# argument. 'ima' will unpack going through minc first. 'ima2'
# will unpack directly to bshorts/COR.

set DefaultUnpackCmd = $DEV/scripts/unpack_ima.tcl;
#set DefaultUnpackCmd = unpack_mnc.tcl;

# When unpacking IMAs is it possible to use minc as an intermediate
# format or to go directly from IMAs to bshorts/COR. To go directly,
# set UnpackOption = "-unpackimadir2", otherwise leave UnpackOption
# empty.
#set UnpackOption = ();
set UnpackOption = "-unpackimadir2";

set UnpackCmd = $DefaultUnpackCmd;
set doshift = 0;
if($#argv == 1) then
  if("$argv[1]" == "minc") then
    set UnpackCmd = unpack_mnc.tcl;
    set doshift = 1;
  else if("$argv[1]" == "ima") then
    set UnpackCmd = unpack_ima.tcl;
    set doshift = 1;
  else if("$argv[1]" == "ima2") then
    set UnpackCmd = unpack_ima.tcl;
    set UnpackOption = "-unpackimadir2";
    set doshift = 1;
  else
    set UnpackCmd = $DefaultUnpackCmd;
  endif
endif

if($doshift) shift;

set os = `uname -s`
if($os != Linux) then
  echo "ERROR: you must be on a Linux computer to run this program.";
  exit 1;
endif

set h = `hostname`;
if($h == www || $h == gate || $h == door) then
  echo ""
  echo "ERROR: you cannot run browse-sessions from $h."
  echo "$h is a gateway machine and should not be used for"
  echo "unpacking or data processing."
  echo ""
  exit 1;
endif


if( ! $?DISPLAY ) then
  if( ! $?REMOTEHOST ) then
    echo " "
    echo "ERROR: DISPLAY variable not set.  Try running:"
    echo "          setenv DISPLAY computername:0.0"
    echo "where 'computername' is the name of the computer at which you are sitting."
    echo " "
    exit 1;
  else
    setenv DISPLAY $REMOTEHOST":0.0"
    echo "INFO: setting DISPLAY to $DISPLAY"
  endif
endif

echo "DISPLAY is set to $DISPLAY";

if($DISPLAY == ":0.0") then
  echo "INFO: display is set to the console.  If you have remotely logged in,"
  echo "then the browser will fail.  If this happens, run"
  echo "      setenv DISPLAY computername:0.0"
  echo "where 'computername' is the name of the computer at which you are sitting."
endif

#if(! $?ARCHIVE_DIR ) then
#  echo "WARNING: ARCHIVE_DIR variable not set"
#else
#  echo "ARCHIVE_DIR set to $ARCHIVE_DIR"
#endif

set cmd = `which $UnpackCmd`;
if(! -e "$cmd") then
  echo "ERROR: cannot find $UnpackCmd. "
  exit 1;
endif

echo "Starting browser, please wait ..."

setenv TCL_LIBRARY   $LOCAL_DIR/lib/tcl8.3
setenv TK_LIBRARY    $LOCAL_DIR/lib/tk8.3

set msgfile = /tmp/unpack_mnc-$$.msg
wish $cmd $UnpackOption $argv -name Browse >& $msgfile
set st = $status;
if($st) then
  echo " "
  set msgtmp = "refused by server"
  set n = `grep "$msgtmp" $msgfile | wc -l`;
  if($n != 0) then
    echo " "
    echo "ERROR: it appears that `hostname` cannot create a window on "
    echo "your local computer screen. Try running:  "
    echo "       xhost + `hostname` "
    echo "on your local computer before running browse-sessions again.";
    echo " "
  else
    echo "ERROR: $UnpackCmd failed with status $st"
    cat $msgfile
  endif
endif

rm -r $msgfile
exit $st;
###########################################################
