#! /bin/csh -f 
# I've skipped the users .cshrc, so I must set the search path

# You may need to modify the following
# system_types are auto, apollo, linux, hp-ux, titan

set system_type = auto
set path = ( /bin /usr/{l3/bin,local/bin,ucb,new,bin} /tmp )
set tmp = /tmp

if $system_type == auto then
  if $?ISP then
    if ( $ISP == m68k || $ISP == a88k ) set system_type = apollo
  else if $?HOSTTYPE then
    if ( $HOSTTYPE != "" ) then
      set system = $HOSTTYPE
    else if ( $HOSTTYPE =~ hp* ) then
      set system = hp-ux
    endif
  else
    set system = linux
  endif
endif 

if $system_type != linux then
  set cp   = ( `which cp` )
  set tail = ( `which tail` )
  set zcat = ( `which gzip` -dc )
  set rm   = ( `which rm` )
endif
set do_nothing = ( if 0 echo ) 

if $system_type == linux then
  set cp   = ( /usr/bin/cp -a )
  set tail = ( /usr/bin/tail )
  set zcat = ( /usr/bin/gzip -dc )
  set rm   = ( /bin/rm )
else if $system_type == apollo then
  set cp   = ( $cp -o )
  set obty = ( /etc/obty )
  set ver  = ( ';' ver bsd4.3 )
else if $system_type == hp-ux then
  set cp    = ( $cp -pf )
else if $system_type == titan then
  set chmod = ( `which chmod` ugo+x )
endif

if ! $?obty  set obty  = ( $do_nothing )
if ! $?ver   set ver   = ( )
if ! $?chmod set chmod = ( $do_nothing )

# gzip is neccissary to uncompress the compressed executables, so
# I'm changeing the partection on gzip to make accidently corrupting
# gzip more difficult.

chmod ugo-w $cp $tail $zcat $rm >& /dev/null

# Now we'll loop over each filename and make it a compressed executable

while ( $#argv > 0 ) 
  set t = ( "$argv[1]:t" )
  if ( ! -x "$1" ) then
    echo -n "FILE NOT FOUND OR NOT EXECUTABLE :"
    goto ERROR
  endif

  if ( 0 == `head -1 "$1" | grep -c '^#! '` ) then

# Check if we have write access (NOTE: csh526 treats "-w" as "-e" disabling this option)

    if ( " csh rm tail gunzip gzip zcat " =~ *" $t "* | ! -w "$1" ) then
      echo -n "WRITE PERMISSION DENIED :"
      goto ERROR
    endif

# Check if apollo type is coff

    if ( $system_type == apollo ) then
      set type = ( `$obty "$1"` )
      if ( coff != $type[$#type] ) then
        echo -n "FILE IS NOT FILETYPE COFF :"
        goto ERROR
      endif
    endif

# First we use gzip to compress the file.
    
    set z = ( "$tmp/$t.z" )
    gzip -c "$1" > "$z"

# Check of the compression worked before continueing

    if ! -e "$z" goto  ERROR 

# Now check if compressing will save any blocks of disk space

    @ block_size = 1024
    @ script_size = 360 

    set wc_old = ( `wc -c "$1"` )
    @ wc_old =  $wc_old[1] + $block_size - 1
    @ wc_old /= $block_size

    set wc_new = ( `wc -c "$z"` )
    @ wc_new = $wc_new[1] + $block_size + $script_size - 1
    @ wc_new /= $block_size

    if ( $wc_old <= $wc_new ) then
      echo -n NO BLOCKS SAVED BY COMPRESSING : 
      $rm "$z"
      goto ERROR
    endif 

# We cat a header script into the old filename for uncompressing.
# note1: If you add more lines to this script, you must update the tail
#        statement.
# note2: If you add a call to another script, you will increase the 
#        amount of time spent loading your executable!
# note3: If the file is already uncompressed, this won't re-uncompress it.
#        I trust the OS to lock the file from being removed when multiple
#        processes are using it.

(echo '#! /bin/csh -f' ;\
 echo 'onintr done_gexe_script' ;\
 echo 'set t = ( "$0" );set t = ( '$tmp'/"$t:t" )'$ver ;\
 echo 'if ! -x "$t" then' ;\
 echo -n $cp '"$0" "$t";'$obty' "$t" coff;' ;\
 echo '('$tail' +14 "$0"|'$zcat')>"$t";'$chmod' "$t"' ;\
 echo 'endif' ;\
 echo 'if ("x$1" == "x-uncompress" & $#argv == 1) then' ;\
 echo    $tail '+1 "$t">"$0";'$obty' "$0" coff' ;\
 echo 'else' ;\
 echo '  "$t" $argv:q' ;\
 echo 'endif' ;\
 echo 'done_gexe_script:' ;\
 echo $rm '-f "$t";exit' ;\
 cat -u "$z" ) > "$1" ; rm "$z"

    $obty "$1" unstruct
  else

    echo -n FILE IS ALREADY COMPRESSED : 
    goto ERROR

  endif

  goto ENDLOOP

ERROR:
  echo "ERROR COMPRESSING $1"
ENDLOOP:
  shift
end
exit













