#! /bin/sh

### fixinflatedavg: marty sereno -- repair poles in homemade inflated_avg
# 16/12/06 -- 01a: initial hack
# 16/12/07 -- 01b: labels from $CSURF_DIR/subjects/fsaverage-ADDITIONS, confirm

if [ $# -ne 1 ]; then
  echo ""
  echo "use: fixinflatedavg <average_subject>"
  echo ""
  echo "  --repairs a few huge triangles at N/S icosahedral poles inflated_avg"
  echo "  --also updates area/curv/sulc files (orig files appended w/ORIG)"
  echo "  --blocks redo if ORIGs there (to redo: move ORIGs back)"
  echo "  --N.B.: automatically done if avg made by csurf cross-sess AVGSURF"
  echo '  --N.B.: first cd into $SUBJECTS_DIR before running fixinflatedavg'
  echo "  --N.B.: req's {rh,lh}-FIX-POLES.label in csurf fsaverage-ADDITIONS"
  echo ""
  echo "                                                          version: 01b"
  exit
fi
subj=$1

### filename parts
areaext="area"
curvext="curv"
sulcext="sulc"
inflatedext2="inflated_avg"
surfdir="surf"
labeldir="label"
extlist="$inflatedext2 $areaext $curvext $sulcext"

### check inputs exist
if [ "`which tksurfer`" == "" ]; then
  echo 'fixinflatedavg: ### tksurfer not on $path   ...quitting'
  exit
fi
if [ -z "$SUBJECTS_DIR" ]; then
  echo "fixinflatedavg: ### no SUBJECTS_DIR env var (use export)  ...quitting"
  exit
fi
if [ -z "$CSURF_DIR" ]; then
  echo "fixinflatedavg: ### no CSURF_DIR env var (use export)  ...quitting"
  exit
fi
if [ `pwd` != "$SUBJECTS_DIR" ]; then
  echo "fixinflatedavg: ### run from inside current SUBJECTS_DIR  ...quitting"
  echo "fixinflatedavg: ### first do:  cd $SUBJECTS_DIR"
  exit
fi
if [ ! -d "$subj" ]; then
  echo "fixinflatedavg: ### subject directory: $subj not found"
  exit
fi
for hemi in lh rh; do
  if [ ! -f "$SUBJECTS_DIR/$subj/$surfdir/$hemi.${inflatedext2}" ]; then
    echo \
      "fixinflatedavg: ### no $SUBJECTS_DIR/$subj/$surfdir/$hemi.$inflatedext2"
    exit
  fi
done
fixsubj="$CSURF_DIR/subjects/fsaverage-ADDITIONS"   # in new dist/inst
if [ ! -d "$fixsubj" ]; then
  echo "fixinflatedavg: ### csurf subject dir: $fixsubj not found"
  exit
fi
for hemi in lh rh; do
  if [ ! -f "$fixsubj/$labeldir/$hemi-FIX-POLES.label" ]; then
    echo "fixinflatedavg: ### no $fixsubj/$labeldir/$hemi-FIX-POLES.label"
    exit
  fi
done

### check outputs don't exist
found=0
for hemi in lh rh; do
  for ext in $extlist; do
    if [ -f "$SUBJECTS_DIR/$subj/$surfdir/$hemi.${ext}ORIG" ]; then found=1; fi
  done
done
if [ $found -eq 1 ]; then
  echo "fixinflatedavg: ### already done? (some ORIG outfiles already there)"
  echo "fixinflatedavg: ### to redo:"
  echo ""
  echo "  pushd $SUBJECTS_DIR/$subj/$surfdir"
  for hemi in lh rh; do
    for ext in $extlist; do
      echo "  mv $hemi.${ext}ORIG $hemi.$ext"
    done
  done
  echo "  popd"
  echo ""
  exit
fi

### wait confirm start
echo ""
echo "OK to repair N/S poles of inflated_avg surfaces for this subject?"
echo ""
echo "  $SUBJECTS_DIR/$subj"
echo ""
echo "  1) original files saved w/ORIG suffix (e.g., rh.areaORIG)"
echo "  2) original files replaced with fixed (e.g., rh.area)"
echo "  3) new files also copied w/NEW suffix (e.g., rh.areaNEW)"
echo ""
echo "  files affected:"
echo ""
for hemi in lh rh; do
  for ext in $extlist; do
    echo "    $hemi.$ext"
  done
done
echo ""
echo " => ctrl-D to go"
echo " => ctrl-C to quit"
read resp
echo ""
echo "starting fixinflatedavg"

### fix bad triangles at icosahedral poles in surf/area/curv/sulc
script=/tmp/TmpTclScript.$$
for hemi in lh rh; do
  echo ""
  echo "#################################"
  echo "fixinflatedavg: working on $hemi"
  echo "#################################"

  # copy inputs to ORIG (vs. mv since surf fix need existing to start)
  for ext in $extlist; do
    cp $SUBJECTS_DIR/$subj/$surfdir/$hemi.${ext} \
       $SUBJECTS_DIR/$subj/$surfdir/$hemi.${ext}ORIG
  done

  # make tmp tksurfer tcl script
  echo "### $script: tksurfer tcl script to fix inflated_avg poles" > $script
  echo "setfile label $fixsubj/$labeldir/$hemi-FIX-POLES.label"    >> $script
  echo "read_label_to_annot_using_col 120 120 255"                 >> $script
  echo "set locklabelflag 1"                                       >> $script
  echo "shrink 40"                                                 >> $script
  echo "setfile area ~/$surfdir/$hemi.$areaext"                    >> $script
  echo "write_binary_areas"                                        >> $script
  echo "setfile outsurf ~/$surfdir/$hemi.$inflatedext2"            >> $script
  echo "write_binary_surface"                                      >> $script
  echo "setfile curv ~/$surfdir/$hemi.$curvext"                    >> $script
  echo "read_binary_curv"                                          >> $script
  echo "smooth_curv 1"                                             >> $script
  echo "write_binary_curv"                                         >> $script
  echo "setfile curv ~/$surfdir/$hemi.$sulcext"                    >> $script
  echo "read_binary_curv"                                          >> $script
  echo "smooth_curv 1"                                             >> $script
  echo "write_binary_curv"                                         >> $script
  echo "exit"                                                      >> $script
  echo ""
  cat $script
  echo ""

  # run tksurfer
  command="tksurfer $subj $hemi $inflatedext2 -tcl $script"
  echo $command
  echo ""
  $command

  # copy fixed to NEW
  for ext in $extlist; do
    cp $SUBJECTS_DIR/$subj/$surfdir/$hemi.${ext} \
       $SUBJECTS_DIR/$subj/$surfdir/$hemi.${ext}NEW
  done

done

### cleanup
rm -f $script

