#!/bin/csh -fb
set noglob = ( )
# This program is copyright 1993 by Bill C. Riemers, under the same terms of
# gzip.  If this is to restrictive, let me know and I will consider relaxing
# it.  (bcr@physics.purdue.edu, bcr@cernapo.cern.ch)

# Put explicit paths here.  I'm not sure if this will work with other programs.
set gzip_data   = ( gzip  )
set ungzip_data = ( gunzip )
set compress_exec   = ( tcx )
set uncompress_exec = ( untcx -u )

# What is the smallest size file you wish to compress in kilobytes
@ minsize = 5 

# Filenames to always exclude (things that you are constantly using...)
# There is no need to list suid programs.  Wildcards are permitted.
set exclude = ( \
	vmlinuz zSystem.map zImage* emacs lemacs *wm \
	xinit getty selection uugetty syslogd *clock \
	*sh X386 XF86_* \
	*.Z *.gz *.tgz *.taz *.tpz *.gif \
	)

# We can also exclude directories and paths ...
set path_exclude = ( \
	*/tmp/* /mnt/* /sbin/* /bin/* /etc/* /usr/spool/* \
	)

# Default extention
set suffix = .GZ


# The rest of this is the script.

if ! $?ZLIB_SUFFIX setenv ZLIB_SUFFIX $suffix
set goto = $0
set options = ( --suffix "$ZLIB_SUFFIX" )
set files = ( )
set maxdepth = ( -maxdepth 0 )

process_arguments:
foreach option ( $argv:q )
  if ! ( $?add_next_option ) then
    if( "X$option:q" =~ "X-"* & ! $?expression ) then
      if ( "X$option:q" =~ X--* ) then
	switch ( "X$option:q" )
          case X--suffix:
	    set add_option = ( )
	    set add_next_option = ( )
            breaksw
          case X--ascii:
          case X--force:
          case X--list:
          case X--no-name:
          case X--name:
          case X--quiet:
          case X--verbose:
          case X--fast:
          case X--best:
	    set add_option = ( )
	    breaksw
          case X--decompress:
          case X--uncompress:
	    set goto = Gunzip
	    breaksw
	  case X--stdout:
	  case X--to-stdout:
	  case X--license:
	  case X--test:
	    if ( $goto == GZip ) exec $gzip_data --suffix "$ZLIB_SUFFIX" $argv:q
	    exec $ungzip_data --suffix "$ZLIB_SUFFIX" $argv:q
	    breaksw
	  case X--recursive:
	    set maxdepth = ( )
	    breaksw 
	endsw
      else
	if ( "X$option:q" == X-*d* ) then
	  set goto = Gunzip
	endif
	if ( "X$option:q" =~ X-S* ) then
	  set add_option = ( )
          goto break_option
	endif
	if ( "X$option:q" =~ X-*[aflnNq,0-9]* ) then
	  set add_option = ( )
	endif
	if ( "X$option:q" =~ X-*[cLt]* ) then
	  if ( $goto == GZip ) exec gzip --suffix "$ZLIB_SUFFIX" $argv:q
	  exec gunzip --suffix "$ZLIB_SUFFIX" $argv:q
        endif
	if ( "X$option:q" =~ X-*r* ) set maxdepth = ( )
      endif
    else
      set files = ( $files:q $option:q )
      unset expression 
    endif
  else
    set add_option = ( )
    unset add_next_option
  endif

break_option:

  if $?add_option then
    set options == ( $options:q $option:q )
    unset add_option
  endif

end

minimum_size:
set sizes = ( -not -empty -not -size 1k )
while ( $minsize > 1 )
  set sizes = ( $sizes -not -size "$minsize"k )
  @ minsize--
end

excluded_files:
set exclude_files = ( )
foreach  file ( g{,un}zip zcat $compress_exec[1]:q $uncompress_exec[1]:q $exclude:q )
  set exclude_files = ( $exclude_files -not -name $file )
end
                      
exclude_dirs:
set exclude_dirs = ( )
foreach dir ( /proc/* /tmp/* $path_exclude )
  set exclude_dirs = ( $exclude_dirs -not -path $dir )
end

goto $goto:t
exit 1

GZip:
find $files $maxdepth -type f -not -perm +7111 \
  $sizes $exclude_files:q $exclude_dirs:q -links 1 -not -fstype proc \
  -exec $gzip_data:q $options:q '{}' ';' 
if ( $?compress_exec ) then
  find $files $maxdepth -type f -perm +111 -not -perm +7000 \
    $sizes $exclude_files:q $exclude_dirs:q -links 1 -not -fstype proc \
    -not -name lib*.so -not -name lib*.so.* -not -name ld.so \
    -exec $compress_exec:q '{}' ';'
endif 
exit 0

Gunzip:
find $files $maxdepth -type f -not -perm +7111 \
  -not -empty $exclude_files:q $exclude_dirs:q -links 1 -not -fstype proc \
  -exec $ungzip_data:q $options:q '{}' ';' 
if ( $?uncompress_exec ) then
  find $files $maxdepth -type f -perm +111 -not -perm +7000 \
    -not -empty $exclude_files:q $exclude_dirs:q -links 1 -not -fstype proc \
    -not -name lib*.so -not -name lib*.so.* -not -name ld.so \
    -exec $uncompress_exec:q '{}' ';' 
endif
exit 0

