#!/bin/sh
#
#  Copyright (c) Members of the EGEE Collaboration. 2004. 
#  See http://www.eu-egee.org/partners/ for details on the copyright
#  holders.  
#
#  Licensed under the Apache License, Version 2.0 (the "License"); 
#  you may not use this file except in compliance with the License. 
#  You may obtain a copy of the License at 
#
#      http://www.apache.org/licenses/LICENSE-2.0 
#
#  Unless required by applicable law or agreed to in writing, software 
#  distributed under the License is distributed on an "AS IS" BASIS, 
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
#  See the License for the specific language governing permissions and 
#  limitations under the License.
#
# Author Peter Kunszt <peter.kunszt@cern.ch>
# $Id: glite-url-copy-cleanlog,v 1.6 2009/02/12 19:22:02 szamsu Exp $

if [ $# -ne 5 ] ;then
  echo
  echo "usage: $0 user channel target_dir|target_file completed/failed age min_amount"
  echo
  echo "   user              Name of the user whose logs are to be backed up."
  echo
  echo "   channel           Channel name to be  backed  up    \(\"\"  for  all"
  echo "                     channels\)."
  echo
  echo "   target_dir|file   Full path of target directory  or  file to archive"
  echo "                     data to.  If the  file  exists  and  is  a  zipped"
  echo "                     tarfile, data will be appended."
  echo
  echo "                     Note: in order to do  this,  the  existing  zipped"
  echo "                     tarfile is unzipped.   If  the file does not exist"
  echo "                     yet, you have to  specify a .tar file,  it will be"
  echo "                     compressed into .tar.gz automatically."       
  echo
  echo "   completed/failed  0 or 1 indicating which logs to archive."
  echo "                       0: archive completed"
  echo "                       1: archive failed"
  echo
  echo "   age               number indicating the minimal age (in fractions of"
  echo "                     a day).   This  is   counted  since  the   last"
  echo "                     modification of the file."
  echo
  echo "examples: "
  echo "   $0 jack \"\" /tmp 0 1"
  echo
  echo "     This  will  archive  all  completed  logs  older than  1 day  to"
  echo "     /tmp/glite-url-copy-completed-DATE-24.tar.gz  where  DATE  is  the"
  echo "     current date-time in format YYMMDDhhmm"
  echo
  echo "   $0 joe ch1 /data/archive.tgz 0 0.5"
  echo
  echo "     This will archive all completed logs of channel  ch1 older than 12"
  echo "     hours,  appending   the    data   to    the   existing   archive"
  echo "     /data/archive.tgz if it exists,  or  creating  the  archive  if it"
  echo "     does not exist."
  echo
  exit 1
fi

u=$1
c=$2
tdir=$3
completed=$4
age=$5
sdir="/var/tmp/glite-url-copy-$u"
date=`date  +%y%m%d%H%M`
tarfile=""

dirs="failed"
if [ $completed -eq 0 ] ;then
    dirs="completed"
fi

if [ ! -d $sdir ] ;then
  echo Source directory $sdir does not exist
  exit 0
fi

if [ -d $tdir ] ;then

   tarfile="$tdir/glite-url-copy-$dirs-$date-$age.tar"

   touch $tarfile
   if [ $? -ne 0 ] ; then
     echo Error: specified target file cannot be written to: $tdir
     rm -rf $tarfile
     exit -1
   fi
else

  if [ -w $tdir ] ;then

    tar ztf $tdir > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
       gunzip $tdir
       tarfile=$tdir
    else
       echo Error: specified target file is not a gzipped tarfile: $tdir
       exit -1
    fi

  else

    if [ -a $tdir ] ;then
       echo Error: specified target file exists but is non-writable: $tdir
       exit -1
    else
      touch $tdir
      if [ $? -eq 0 ] ; then
        tarfile=$tdir
      else
        echo Error: specified target file cannot be written to: $tdir
        exit -1
      fi
    fi
  fi
fi

echo Writing to: $tarfile

pushd $sdir > /dev/null 2>&1

if [ "X$c" == "X" ] ;then
  dirs=`ls -d *$dirs`
else
  dirs=$c$dirs
fi

for d in $dirs
do
  cd $d
  mkdir -p $d
  perl -e "opendir(DIR,\"$sdir/$d\"); while(\$_=readdir DIR){ rename \"\$_\", \"$d/\$_\" unless /^\./  || -M \"$sdir/$d/\$_\" < ($age/24.0); } closedir(DIR);"

  tar rf $tarfile $d
  rm -rf $d
  cd ..
done
gzip $tarfile

popd > /dev/null 2>&1
