#!/bin/sh
#-------------------------------------------------------------------------------
# charon_logarchive
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2022 STROMASYS.
# All rights reserved.
#
VERSION=1.10

. `dirname $0`/charon_common

LOGFLIST="${CHARONDIR}/utils/charon_logchk.list"
BOOTLIST="${CHARONDIR}/utils/charon_gstart.boot"

test -e ${HOME}/.charonconslogdir && CONSLOGDIR=`cat ${HOME}/.charonconslogdir`
test -z "${CONSLOGDIR}" && CONSLOGDIR="${CHARONDIR}/log"

LOGARC=${CHARONDIR}/log/`basename $0`.log
date >${LOGARC}
echo "--------------------------------------------------------------" >>${LOGARC}
echo "[I] CONSLOGDIR=${CONSLOGDIR}" >>${LOGARC}

KEEPD=60
KEEPZ=182
DOZIP=0

function ArchiveLog
{
  #-- $1 = folder
  #-- $2 = name
  #-- $3 = "" or "LINK" (for guests log files)
  #-- $4 = if $3 = "LINK", then $4 is log file name in cfg file
  PROTECT=""
  test "$3" = "LINK" && PROTECT="$1/`readlink $4`"
  find $1 -name "$2" -mtime +${KEEPD} | while read ARCFIL
  do
    if test "${PROTECT}" = "${ARCFIL}"
    then
      echo "[I] Keep/last ${ARCFIL}" >>${LOGARC}
    else
      if test ${DOZIP} = 1
      then
        echo "[I] Zipping ${ARCFIL}" >>${LOGARC}
        if test -e ${ARCFIL}.gz
        then
          D=`date +%Y%m%d-%H%M%S`
          echo "[I] Moving ${ARCFIL}.gz to ${ARCFIL}.${D}.gz" >>${LOGARC}
          mv ${ARCFIL}.gz ${ARCFIL}.${D}.gz
        fi
        gzip ${ARCFIL}
      else
        echo "[I] Removing ${ARCFIL}" >>${LOGARC}
        rm -f ${ARCFIL} 
      fi 
      RET=$?
      if test ${RET} = 0
      then
        echo "[I] Done." >>${LOGARC}
      else
        echo "[E] Return code = ${RET}" >>${LOGARC}
      fi
    fi
  done

  find $1 -name "$2.gz" -mtime +${KEEPZ} | while read ZIPFIL
  do
    echo "[I] Removing ${ZIPFIL}" >>${LOGARC}
    rm -f ${ZIPFIL} 
    RET=$?
    if test ${RET} = 0
    then
      echo "[I] Done." >>${LOGARC}
    else
      echo "[E] Return code = ${RET}" >>${LOGARC}
    fi
  done
}


while test $# -ne 0
do
  case "$1"
  in
    -keep)
      KEEPD=60
      ;;
    -keep=*)
      KEEPD=`echo $1 | cut -f2 -d'=' | tr -dc '[0-9]'`
      echo "[I] Parameter $1" >>${LOGARC}
      test -z "${KEEPD}" && KEEPD=60
      #-- Cannot be less than 14 days
      test ${KEEPD} -lt 14 && KEEPD=14
      ;;
    -zip)
      DOZIP=1
      ;;
    -zip=*)
      KEEPZ=`echo $1 | cut -f2 -d'=' | tr -dc '[0-9]'`
      echo "[I] Parameter $1" >>${LOGARC}
      test -z "${KEEPZ}" && KEEPZ=182
      test ${KEEPZ} -lt 35 && KEEPZ=35
      DOZIP=1
      ;;
  esac
  shift
done

echo "[I] DOZIP=${DOZIP}" >>${LOGARC}
echo "[I] Resulting KEEPD=${KEEPD}" >>${LOGARC}
echo "[I] Resulting KEEPZ=${KEEPZ}" >>${LOGARC}
if test ${KEEPZ} -le ${KEEPD}
then
  echo "[E] ERROR: Zip retention must be greater than log file retention" >>${LOGARC}
  exit 1
fi

TMPFIL="/tmp/`basename $0`.tmp"
#-------------------------------------------------------------
# Important note: guests log files cannot be renamed as the
# emulator is still writing in the renamed file, not
# in the newly created one
#-------------------------------------------------------------
echo "${CHARONDIR}/log/charon_alerts.log"           >${TMPFIL}
echo "${CHARONDIR}/log/charon_expchk.log"           >>${TMPFIL}
echo "${CHARONDIR}/log/charon_gstart.log"           >>${TMPFIL}
echo "${CHARONDIR}/log/charon_logchk.log"           >>${TMPFIL}
echo "${CHARONDIR}/log/charon_menu_hostinfo.log"    >>${TMPFIL}
echo "${CHARONDIR}/log/charon_menu_v2c.log"         >>${TMPFIL}

cat <<EOF >>${LOGARC}

--------------------------------------------------------------
Renaming and archiving log files
--------------------------------------------------------------
EOF

#--- Get LOGFRENEXT value
get_logfile -renext

#--- Read CHARON log files list, rename and archive if necessary
sort -u ${TMPFIL} | while read FIL
do
  if test -e ${FIL}
  then
    echo "[I] Renaming ${FIL} to ${FIL}.${LOGFRENEXT}" >>${LOGARC}
    mv -f ${FIL} ${FIL}.${LOGFRENEXT}
    RET=$?
    if test ${RET} = 0
    then
      echo "[I] Done." >>${LOGARC}
      touch ${FIL}
    else
      echo "[E] Return code = ${RET}" >>${LOGARC}
    fi
  else
    echo "[I] ${FIL} not found." >>${LOGARC}
  fi
  ArchiveLog `dirname ${FIL}` "`basename ${FIL}`.upto*[0-9]"
done


#--- Archive yum and dnf install log files
ArchiveLog ${CHARONDIR}/log "???.install.????????_??????"


#--- Archive CHARON guests log files
cat ${BOOTLIST} 2>/dev/null | while read GCFG
do
  GCFG=`echo ${GCFG} | cut -f2 -d';'`
  GCNF=`basename ${GCFG} | sed "s=\.cfg$==g"`

  get_logfile ${GCFG}
  if test -L ${LOGF}
  then
    LOGTMP=`basename ${LOGF} | sed "s=\.log==g"`
    ArchiveLog `dirname ${LOGF}` "${LOGTMP}.??????-??????-????.log" "LINK" ${LOGF}
  else
    ArchiveLog `dirname ${LOGF}` "`basename ${LOGF}`.upto????-??-??-??????"
  fi

  get_logfile ${GCFG} OPA0
  if test -n "${LOGCONS}"
  then
    if test -L ${LOGCONS}
    then
      LOGTMP=`basename ${LOGCONS} | sed "s=\.log==g"`
      ArchiveLog `dirname ${LOGCONS}` "${LOGTMP}.??????-??????-????.log" "LINK" ${LOGCONS}
    else
      ArchiveLog `dirname ${LOGCONS}` "`basename ${LOGCONS}`.upto????-??-??-??????"
    fi
  fi

  for LOGCONS in `ls ${CHARONDIR}/log/console.${GCNF}.*.log ${CHARONDIR}/log/console.stop.${GCNF}.*.log 2>/dev/null`
  do
    ArchiveLog `dirname ${LOGCONS}` "`basename ${LOGCONS}`"
  done

done


echo "[I] End." >>${LOGARC}

exit
