#!/bin/tcsh -ef

set SUBJECTS=($SUBJECTS)
set average_subject=average
if (-e $SUBJECTS_DIR/${average_subject} == 0) then
		mksubjdirs $SUBJECTS_DIR/${average_subject}
else
		mkdir -p $SUBJECTS_DIR/${average_subject}/surf
endif
foreach hemi (lh rh)
	foreach s (orig white pial inflated)
		set echo=1
		mris_make_average_surface -i 7 -o $s $hemi ${s}_avg sphere.reg ${average_subject} $SUBJECTS
		unset echo
	end
	cd $SUBJECTS_DIR/${average_subject}/surf	
	pwd	
	mris_smooth -n 5 ./${hemi}.white_avg ./$hemi.smoothwm
	mris_inflate  ./${hemi}.smoothwm ./${hemi}.inflated
	mris_curvature -a 10 -w ./${hemi}.white_avg
	cp ${hemi}.white_avg.H  ${hemi}.curv
	cp $CSURF_DIR/average/$hemi.sphere.reg .
	ln -s -f $hemi.sphere.reg $hemi.sphere
		set echo=1
	mris_average_curvature -o $average_subject  curv $hemi sphere.reg $SUBJECTS ./${hemi}.curv
	mris_average_curvature -o $average_subject  sulc $hemi sphere.reg $SUBJECTS ./${hemi}.sulc
	mris_average_curvature -o $average_subject  thickness $hemi sphere.reg $SUBJECTS ./${hemi}.thickness
end
cp $SUBJECTS_DIR/$SUBJECTS[1]/mri/T1/COR-.info $SUBJECTS_DIR/${average_subject}/mri/T1

