#! /bin/sh

##---------------------------------------------------------------------------
## Optional Package for fli4l 2.0.3                 (see http://www.fli4l.de)
##
## Package    :  opt_logshift
##
## Creation   :  22.07.02    Winfried Mueller
## Last Update:  01.08.02    Winfried Mueller
## Release    :  0.1.0
## State      :  exp
##
## Copyright (c) 2002 Winfried Mueller <wm@reintechnisch.de>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##---------------------------------------------------------------------------

use="use: logshift <File> <ShiftNumber>"

# maximale Anzahl Shiftfiles
max_n=60

file=$1
n=$2

# Require
if [ ! -f "$file" ] || [ "$file" = "" ]
then
  echo "File not available: [$file]" >&2
  echo $use >&2
  exit 1
fi

if [ "$n" -ge $max_n ] || [ "$n" -le 0 ]
then
  echo "ShiftNumber not in Range: [$n]"
  echo $use >&2
  exit 1
fi

# shift all files [0..n]
x=$n
y=`expr $x - 1`
while [ "$x" -ge 1 ]
do
  if [ -f "$file.$y" ]
  then
    mv "$file.$y" "$file.$x"
  fi
  x=`expr $x - 1`
  y=`expr $x - 1`
done

# cp the original file to "$file.0" and clear original file
# Warn: Logs, that come in the time-intervall between this
# commands, are loss
# --
cp $file "$file.0"
echo -n > $file
# --

exit 0
