#!/bin/csh -f
#
#	rotate a log file through a number of levels
#
#	thus foo   --> foo.0
#	     foo.0 --> foo.1
#	     foo.1 --> foo.2
#
#	etc, until foo.level which is removed.  
#
#	written by tom christiansen, 20 apr 87


@ level = 7
set own = root
set grp = daemon
set mode = 644
set special = ""

switch ( $#argv )
	case 5:
		set special = "$5"
	case 4:
		set mode = $4
	case 3:
		set level = $3
	case 2:
		set owner = $2
		set own = ${owner:r}
		set grp = ${owner:e}
	case 1:
		set file  = $1
		breaksw
	default:
		echo "usage: $0 file [owner [level [mode [special]]]]" 
		exit 1
endsw
		

cd $file:h
set name = $file:t

echo Rotating $file through $level levels

/bin/rm -f $name.$level $name.$level.gz

while ( $level > 0 )
	@ less = $level - 1
	if ( -f $name.$less.gz ) then
		/bin/mv $name.$less.gz  $name.$level.gz
	else
		/bin/mv $name.$less  $name.$level
		/usr/psup/bin/gzip $name.$level
        endif
	@ level = $less 
end

/bin/mv $name    $name.0
/usr/psup/bin/gzip $name.0
/bin/cp /dev/null $name
chown $own $name
chgrp $grp $name
/bin/chmod $mode  $name

if ( "$special" != "" ) then
	eval "$special"
endif

exit $status
