#! /bin/tcsh -f

#set echo=1

if ( ("x$1" == "x") || ( "x$1" == "x-help" ) || ( "x$1" == "x--help" ) ) then
    echo "USAGE: vno_match_check <subjid> [option]"
    echo ""
    echo "Checks that all surfaces and surface data files for subject <subjid>"
    echo "have the same number of vertices. Exits with status code 0 if ok."
    echo "Exits with status code 1 and error message if any mismatches found."
    echo "Options: (only one is accepted)"
    echo "  debug     progress text is printed"
    echo "  rh        right hemi only" 
    echo "  lh        left hemi only" 
    exit 1
endif

if ( ! $?SUBJECTS_DIR ) then
    echo "SUBJECTS_DIR is not declared"
    exit 1
endif

set subj=$1
if ( ! -e $SUBJECTS_DIR/$subj/surf ) then
    echo "$SUBJECTS_DIR/$subj/surf does not exist"
    exit 1
endif

cd $SUBJECTS_DIR/$subj/surf

set hemis=( rh lh )
if ("x$2" == "xrh") set hemis=( rh )
if ("x$2" == "xlh") set hemis=( lh )

foreach hemi ( $hemis )
   
    # get number of vertices of the orig surface
    if ("x$2" != "x") echo "Checking $subj/surf/$hemi.orig..."
    if ( ! -e $SUBJECTS_DIR/$subj/surf/$hemi.orig ) then
        echo "ERROR: $SUBJECTS_DIR/$subj/surf/$hemi.orig does not exist!"
        exit 1
    endif
    set vno_orig=`mris_info $hemi.orig \
        |& grep "num vertices:" \
        |& awk '{print $3}'`
    if ("x$vno_orig" == "x") then
        echo "Error running mris_info"
        exit 1
    endif

    # and check that all the other surfaces have this same number
    foreach surf ( white pial inflated smoothwm sphere )

        if ("x$2" != "x") echo "Checking $subj/surf/$hemi.$surf..."
        if ( ! -e $SUBJECTS_DIR/$subj/surf/$hemi.$surf ) then
            echo "ERROR: $SUBJECTS_DIR/$subj/surf/$hemi.$surf does not exist!"
            exit 1
        endif
        set vno=`mris_info $hemi.$surf \
            |& grep "num vertices:" \
            |& awk '{print $3}'`
        if ("x$vno" == "x") then
            echo "Error running mris_info"
            exit 1
        endif

        if ( "$vno_orig" != "$vno" ) then
            echo "\nERROR: $subj/surf/$hemi.orig has $vno_orig vertices, $subj/surf/$hemi.$surf has $vno vertices\n"
            exit 1
        endif

    end

    # now check that the curvature files have the correct number of vertices
    foreach curv ( curv sulc area thickness )

        if ("x$2" != "x") echo "Checking $subj/surf/$hemi.$curv..."
        if ( ! -e $SUBJECTS_DIR/$subj/surf/$hemi.$curv ) then
            echo "ERROR: $SUBJECTS_DIR/$subj/surf/$hemi.$curv does not exist!"
            exit 1
        endif
        mris_info $hemi.orig --c $hemi.$curv >& /tmp/mris_info.log
        if ($status) then
            grep ERROR /tmp/mris_info.log
            exit 1
        endif

    end

    # now check that the annotation files have the correct number of vertices
    set annot_list = ( aparc.annot aparc.a2005s.annot )
    if ("x$2" == "xaparc_edited") set annot_list = ( $annot_list aparc_edited.annot )
    foreach annot ( $annot_list )

        if ("x$2" != "x") echo "Checking $subj/label/$hemi.$annot..."
        if ( ! -e $SUBJECTS_DIR/$subj/label/$hemi.$annot ) then
            echo "ERROR: $SUBJECTS_DIR/$subj/label/$hemi.$annot does not exist!"
            exit 1
        endif
        mris_info $hemi.orig --a ../label/$hemi.$annot >& /tmp/mris_info.log
        if ($status) then
            grep ERROR /tmp/mris_info.log
            exit 1
        endif

    end
end

if ("x$2" != "x") echo "Pass: all surfaces and surface data for subject $subj have the same number of vertices."
