#! /bin/sh
#
# Script to allow editing of minc files
#
# Usage: mincedit <minc file> [<editor>]
#
# Modifications: 
#   $Log: mincedit,v $
#   Revision 6.9  2008-06-18 03:55:25  stever
#   Remove illegitimate escape sequences (\n) from echo statements.
#   See Debian bug 486048 for details.
#
#   Revision 6.8  2008/02/05 13:48:45  rotor
#    * added code for checking VISUAL and EDITOR before falling back to emacs
#
#   Revision 6.7  2008/01/16 00:45:34  rotor
#    * replaced mincdump -h with mincheader to preserver image ranges
#
#   Revision 6.6  2008/01/09 14:42:24  rotor
#    * tidied up the man page and removed problematic errexit option
#
#   Revision 6.5  2008/01/09 12:39:46  rotor
#    * added errexit option
#
#   Revision 6.4  2008/01/09 12:31:08  rotor
#    * complete rewrite in sh
#
#   Revision 6.3  2004/06/16 16:27:27  bert
#   Use mincgen instead of ncgen
#
#   Revision 6.2  2000/09/12 15:43:37  neelin
#   Changed default TMPDIR to look for /var/tmp, /usr/tmp, /tmp.
#
#   Revision 6.1  1999/10/19 14:45:21  neelin
#   Fixed Log subsitutions for CVS
#
#   Revision 6.0  1997/09/12 13:23:34  neelin
#   Release of minc version 0.6
#    
#   Revision 5.0  1997/08/21  13:24:35  neelin
#   Release of minc version 0.5
#  
#   Revision 4.0  1997/05/07  20:00:36  neelin
#   Release of minc version 0.4
#  
#   Revision 3.0  1995/05/15  19:31:09  neelin
#   Release of minc version 0.3
#  
#   Revision 2.0  1994/09/28  10:33:58  neelin
#   Release of minc version 0.2
#  
#   Revision 1.6  94/09/28  10:33:55  neelin
#   Pre-release
#   
#   Revision 1.5  93/08/25  11:24:48  neelin
#   Added checking for -h or -help options.
#   
#   Revision 1.4  93/08/11  15:19:09  neelin
#   Added RCS logging in source.
# 
#
#
# Copyright 2008 Andrew Janke
# Based upon original version of Peter Neelins (1993)
#
# McConnell Brain Imaging Centre, 
# Montreal Neurological Institute, 
# McGill University.
#
# Permission to use, copy, modify, and distribute this
# software and its documentation for any purpose and without
# fee is hereby granted, provided that the above copyright
# notice appear in all copies.  The author and McGill University
# make no representations about the suitability of this
# software for any purpose.  It is provided "as is" without
# express or implied warranty.


# simple little function to emulate perl's die();
die () {
    echo >&2 $@
    echo ""
    exit 1
}

me=`basename $0`
usage="Usage: $me <minc file> [<editor>]"

# create tmpdir
tmpdir=${TMPDIR:-/tmp}/mincedit.$$
trap 'rm -rf "$tmpdir"' 0
trap ' exit ' 0 1 2 3 15
(umask 077 && mkdir $tmpdir) || {
   echo "$me: Could not create temporary directory! Exiting." 1>&2 
   exit 1
   }

# check arguments
case $# in
   1)
      if [ "$1" = "-help" -o "$1" = "-h" -o "$1" = "" ]
      then
         echo $usage
         exit 0
      fi
      
      # if no editor is specified, find a suitable one
      if [ -n "$VISUAL" ]
      then
         editor=${VISUAL}
      else
         if [ -n "$EDITOR" ]
         then
            editor=${EDITOR}
         else
            editor="emacs"
         fi
      fi
      ;;
   
   2)
      editor="$2"
      ;;
   
   *)
      echo $usage
      exit 0
esac

# only allow one input file and check for it
[ $# -eq 1 -o $# -eq 2 ] || die $usage
[ -f $1 ] || die "$me: no such file $1"

# set up the file names
infile="$1"
backup_infile=`echo ${infile} | sed -e "s/\.mnc/\.bu-$$\.mnc/"`
cdl_prefix=mincedit-$$
cdl_orig=${tmpdir}/${cdl_prefix}-orig.cdl
cdl_edit=${tmpdir}/${cdl_prefix}-edit.cdl

# dump the file
mincheader $infile > $cdl_orig
status=$?
if [ $status -ne 0 ]
then
   echo "${0}: Error reading file '$infile'"
   exit 1
fi

# make a copy to fiddle with
cp $cdl_orig $cdl_edit

# loop until successful file generation
do_edit=1
while [ $do_edit -ne 0 ]
do
   # edit the cdl file
   $editor $cdl_edit
   status=$?
   if [ $status -ne 0 ] 
   then
      echo "$me: Error editing with editor '$editor'"
      exit 1
   fi
   do_edit=0
   
   # compare the files for difference
   diff $cdl_orig $cdl_edit > /dev/null 2>&1
   status=$?
   if [ $status -eq 0 ] 
   then
      echo "$me: File $infile not modified"
      exit 0
   fi
   
   # rename the original file and generate a new one
   mv "$infile" "$backup_infile"
   status=$?
   mincgen -o $infile $cdl_edit
   status=$?
   if [ $status -ne 0 ]
   then
      mv $backup_infile $infile
      echo -n "$me: Error generating new file. Redit the file (y/n)? (def=n):"
      read answer
      case "$answer" in
         y*)
            do_edit=1
            continue
            ;;
      esac
      exit 0
   fi

   # copy the data back in
   minccopy $backup_infile $infile
   status=$?
   if [ $status -ne 0 ]
   then
      mv $backup_infile $infile
      echo -n "Error copying image data. Redit the file (y/n)? (def=n):"
      read answer
      case "$answer" in
         y*)
            do_edit=1
            continue
            ;;
      esac
      exit 0
   fi

done
