#!/bin/ksh
#/* Part of the Maestro sequencer software package.
# * Copyright (C) 2011-2015  Operations division of the Canadian Meteorological Centre
# *                          Environment Canada
# *
# * Maestro is free software; you can redistribute it and/or
# * modify it under the terms of the GNU Lesser General Public
# * License as published by the Free Software Foundation,
# * version 2.1 of the License.
# *
# * Maestro is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# * Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public
# * License along with this library; if not, write to the
# * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# * Boston, MA 02111-1307, USA.
# */


arguments=$*

eval `cclargs $0 \
  -e        ""                    ""                           "[Target experiment  ]" \
  -t        ""                    ""                           "[Real-Time deletion value   ]" \
  -d        ""                    ""                           "[Datestamp deletion value   ]" \
  -v        ""                    "1"                          "[Verbose            ]" \
  -l        ""                    "1"                          "[Delete logs and stats too? ]" \
  -m        ""                    "$TRUE_HOST"                 "[Machine to delete listings  ]" \
  -h        ""                    ""                           "[Display help       ]" \
  ++ $arguments`                                                                                                                                                                                                                   

if [[ $v != "" ]] ; then  
  set -x
fi 

if [[ -z $e ]]; then 
   if [[ -z $SEQ_EXP_HOME ]]; then 
      echo Error: -e argument or SEQ_EXP_HOME must be defined.
      exit 1
   else  
      e=$SEQ_EXP_HOME
   fi
fi

#datestamp deletion
if [[ -n $d ]]; then  
    if [[ $d = +([0-9]) ]] ; then 
      #padding to 14 characters 
      while [[ $d -lt 9999999999999 ]] ; do 
           d=$(($d * 10))
      done 
    else 
       echo Error: -d argument must only be numeric.
       exit 1
    fi 
    #clean sequencing: 
    echo "Cleaning sequencing for date $d" 
    find $e/sequencing/ -name "*$d*" -print -exec rm -rf {} \; 
  
    if [[ "$l" == "1" ]]; then 
        echo "Cleaning logs and stats: $e/logs/${d}_nodelog and ${d}_toplog, $e/stats/${d} and ${d}_avg "
        rm -f $e/logs/${d}_nodelog
        rm -f $e/logs/${d}_toplog
        rm -f $e/stats/${d}
        rm -f $e/stats/${d}_avg
    fi
 
    echo "Cleaning latest listings for date $d" 
    find $e/listings/latest -name "*$d*" -print -exec rm -f {} \; 
    
    if [[ -n $m ]] ; then
       if [[ $m == "all" ]] ; then
          echo "Clean listings on all machines" 
          for machine_path in $e/listings/*
          do
             machine=${machine_path##*/}
             echo "Current machine: $machine"
             if [[ $machine != "latest" ]]; then
                ssh $machine find $e/listings/$machine/ -name "*$d*" -type f -print -exec 'rm -f {} \;'
             fi
          done
       else
          echo "Clean listings on machine $m" 
          ssh $m find $e/listings/$m/ -name "*$d*" -type f -print -exec 'rm -f {} \;'
       fi 
    fi 
fi

#real time deletion
if [[ -n $t ]]; then  
    if [[ "`uname`" != "Linux" ]]; then 
       echo "Error: -t argument can only be invoked from a Linux platform. Target machine however may be non-Linux." 
       exit 1
    fi
    #clean sequencing: 
    echo "Cleaning sequencing older than $t day(s)" 
    find $e/sequencing/status/ -mindepth 1 -maxdepth 1 -type d -mtime +$t -print  -exec rm -rf {} \; 
    find $e/sequencing/status/depends -mindepth 1 -maxdepth 1 -type d -mtime +$t -print  -exec rm -rf {} \; 
    find $e/sequencing/status/remote_depends -mindepth 1 -maxdepth 1 -type d -mtime +$t -print -exec rm -rf {} \;  
    find $e/sequencing/ -mindepth 2 -not -name ".gitignore" \( -type f -or -type l \) -mtime +$t -print -exec rm -f {} \; 
  
    if [[ "$l" == "1" ]]; then 
        echo "Cleaning log $e/logs/"
        find $e/logs/ -maxdepth 1 -not -name ".gitignore" -type f -mtime +$t -print -exec rm -f {} \; 
        echo "Cleaning stats $e/stats/"
        find $e/stats/ -maxdepth 1 -not -name ".gitignore" -type f -mtime +$t -print -exec rm -f {} \; 
    fi
 
    echo "Cleaning latest listings older than $t day(s)" 
    find $e/listings/latest -type l -mtime +$t -print -exec rm -f {} \; 
    
    if [[ -n $m ]] ; then
       if [[ $m == "all" ]] ; then
          echo "Clean listings on all machines older than $t day(s)"
          for machine_path in $e/listings/*
          do
             machine=${machine_path##*/}
             echo "Current machine: $machine"
             if [[ $machine != "latest" ]]; then
                ssh $machine find $e/listings/$machine/ -type f -mtime +$t -print -exec 'rm -f {} \;' 
             fi
          done
       else
          echo "Clean listings on machine $m older than $t day(s)" 
          ssh $m find $e/listings/$m/ -type f -mtime +$t -print -exec 'rm -f {} \;' 
       fi
    fi 
fi


