# make_1_gef - by R.L.Glenn, CGIS Staff, NHQ
#
# make_1_gef takes the three sub-files of SCSGEF data
# and makes one file, this is required for SCSGEF to GRASS.
#
# Four [three optional] parameters are required
# the first is the name of the header data file
# the second is the name of the area lines data file
# the optional second is the name of the features line/point data file
# the third is the name of the text data file
# the optional third is the name of the resultant data file
# the fourth is the name of the resultant combined data file

if test $# -lt 3  -o $# -gt 4
   then echo "  USAGE:\n   for area line data (e.g. soils)"
        echo "             $0 header_file lines_file text_file resultant_file"
        echo "  USAGE:\n   for feature line/point data (e.g. special map symbols)"
        echo "             $0 header_file feature_file resultant_file";exit
fi

if [ -r $1 ]                   # if header file exists & is readable
   then continue
   else echo "\t$0 cannot access $1 ";exit
fi

if [ -r $2 ]                   # if lines  or features file exists & is readable
   then continue
   else echo "\t$0 cannot access $2 ";exit
fi

if test $# -eq 4 
then if [ -r $3 ]                   # if text file exists & is readable
     then continue
     else echo "\t$0 cannot access $3 ";exit
     fi
fi

if test $# -gt 3 
   then if [ -f $4 ]           # if resultant file exists
        then if [ ! -w $4 ]   # and resultant file is NOT writable
             then echo "\t$0 Can NOT write to existing file <$4>"
		  exit
	     else continue
             fi
        else echo "\t$0 Creating new file <$4>"
	     continue
	fi
   else if [ -f $3 ]           # resultant file exists
	   then if [ ! -w $3 ]
	           then echo "\t$0 Can NOT write to existing file <$3>"; exit
	           else continue
	           fi
	   else echo "\t$0 Creating new file <$3>"
	        continue
	fi
fi

if test $# -eq 4 
   then cat $1 > $4 		       # output header to resultant
        echo "-head" >> $4             # end of header data marker
        cat $2 >> $4		       # append lines to resultant
        echo "-line" >> $4             # end of lines data marker
        cat $3 >> $4 		       # append text to resultant
        echo "-end" >> $4              # end of data marker
   else cat $1 > $3 		       # output header to resultant
        echo "-head" >> $3             # end of header data marker
        cat $2 >> $3 		       # append lines to resultant
        echo "-end" >> $3              # end of data marker
fi

echo "Done"
