#!/bin/csh -f
#
#         C-shell script : CRZ2RNX
#             (frontend of CRX2RNX)
#                 1996.12.19  created by Y. Hatanaka.
#                 2007.06.10  updated by Y. Hatanaka.
#                      - replace "compress" with "uncompress"
#                      - file name extension may be lower and upper case
#                 2009.07.07  modified extensively by Y. Hatanaka.
#                      - Ambiguities/bugs in following items are resolved:
#                          * setting of output directory
#                          * case of deletion of input files
#                          * case of overwriting an output file
#                        Options to control them are added.
#                      - Handling of more RINEX file types are added.
#                      - 'gzipped' files can be processed.
#
#--------------------------------------------------------------------
set help = 0
foreach var ($argv[*])
if ( "$var" == '-h' ) set help = 1
end

if($#argv < 1 || $help) then
more << EOF

CRZ2RNX : C-shell script to decompress multiple RINEX files.

Usage : CRZ2RNX [-c] [-d] [-f] [-v] file ...

-c : output to the current directory
-d : delete input files if decompressed successfully
-f : force overwriting output files without inquiring
-v : verbose mode
-h : show this message and stop
file ... : input compressed RINEX (or CRINEX) files.
Wildcards can be used.

compressed RINEX/CRINEX -->     CRINEX     -->      RINEX
????????.??d  -->   ????????.??o
????????.??d.Z(gz) --> (????????.??d) -->   ????????.??o
????????.??o.Z(gz)                    -->   ????????.??o
????????.??n.Z(gz)                    -->   ????????.??n
????????.??g.Z(gz)                    -->   ????????.??g
????????.??l.Z(gz)                    -->   ????????.??l
????????.??p.Z(gz)                    -->   ????????.??p
????????.??h.Z(gz)                    -->   ????????.??h
????????.??b.Z(gz)                    -->   ????????.??b
????????.??m.Z(gz)                    -->   ????????.??m
????????.??c.Z(gz)                    -->   ????????.??c

Remarks:
- Installation of CRX2RNX is necessary to use this tool.
- The extensions of the input files must conform to the RINEX convention.
- An decompressed file is saved in the same directory as the input file
unless the option '-c' is specified.
- An input file is deleted only when the option "-d" is specified and
the compression is successful.

[20090707]

EOF

exit
endif
#--------------------------------------------------------------------

# set default mode
set out_current = 0
set del_input = 0
unset verbose
set ovrewrite = 0

set PROGRAM = CRX2RNX

unset noclobber

# check options
foreach var ($argv[*])
switch ($var)
case '-c':
set out_current = 1
shift; breaksw
case '-d':
set del_input = 1
shift; breaksw
case '-f':
set ovrewrite = 1
shift; breaksw
case '-v':
set verbose = 1
shift; breaksw
default:
break
endsw
end


# process files
foreach file ($argv[*])

# make command to be issued and name of output file
set file2 = $file
set ext   = $file:e
if ( $out_current ) set file2 = $file2:t
if( $ext == Z || $ext == gz ) set file2 = $file2:r
if( $file2 =~ *.??[dD] ) then
set file2 = `echo $file2 | sed -e 's/d$/o/' -e 's/D$/O/' `
else if( $file2 !~ *.??[oOnNgGlLpPhHbBmMcC] || ! ($ext == Z || $ext == gz) ) then
# This is not a compressed RINEX file ... skip it
continue
endif
set file_save = $file2

# check if the output file is preexisting
if ( -e "$file_save" && ! $ovrewrite ) then
echo "The file $file_save already exists. Overwrite?(y/n,default:n)"
if ( $< !~ [yY] ) continue
endif

# issue the command
if( $file =~ *.??[dD] ) then
cat $file  | CRX2RNX - > $file_save
else if( $file =~ *.??[dD].Z || $file =~ *.??[dD].gz ) then
  file $file | grep -q "Zip"
  if ( "$status" == "0" ) then
     unzip -p $file | CRX2RNX - > $file_save
  else
     file $file | grep -q "ASCII"
     if ( "$status" == "0" ) then
        cat $file | CRX2RNX -> $file_save
     else
        zcat $file | CRX2RNX - > $file_save
     endif
  endif
else
zcat $file > $file_save
endif

# remove the input file
if ( $status == 0 && $del_input ) rm $file

end
