#!/bin/bash

# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj. All rights reserved.

# ==================================================
#
# Collect all IRIS/RDA log files to one package file.
#
# ==================================================

SKIPANTLIB=false
# Process command line options
while [ ! "$1" = "" ] ; do
  OPT="$1" ; shift

  if [  "${OPT}" =  "help"  -o \
          "${OPT}" = "-help"  -o \
          "${OPT}" = "-h"  ] ; then
    echo "Options for collect_irisrda_logs:"
    echo " -noantlib : do not include antlib log files."
    echo
    exit 0
  fi
  
  if [ "${OPT}" = "-noantlib"  ] ; then
    SKIPANTLIB=true
  fi
  
done
hostname=$(hostname -s)
date=$(date -I)

logpath_parent=$(mktemp --directory --tmpdir collect_irisrda_logs-XXXXXX)
trap "rm -r -- "$logpath_parent"" EXIT

logname=${hostname}-${date}
logpath="$logpath_parent/$logname"
mkdir --parents $logpath

error_log_name="collect_irisrda_logs_error.log"
error_log=$(mktemp --tmpdir "$error_log_name-XXX")

# Copy the log files from IRIS/RDA log path
copy_ok=false
if [ ${SKIPANTLIB} = true ] ; then
  echo "Not copying antlib.log* files."
  if rsync --exclude=*antlib* ${IRIS_LOG}*.log* ${logpath}
  then
    copy_ok=true
  fi
else
  if cp ${IRIS_LOG}*.log* ${logpath} 2>>"$error_log"
  then
    copy_ok=true
  fi
fi
 
if [ ${copy_ok} = true ] ; then
  total_file_size=0
  for filename in ${logpath}/*.log*
  do
    file_size_kb=`du -k "$filename" | cut -f1`
    let "total_file_size+=file_size_kb"
  done
  echo "IRIS/RDA log files copied, total ${total_file_size} kB."
else
  echo "No IRIS/RDA log files found!"
fi

if cp ${IRIS_LOG}*.LOG* ${logpath} 2>>"$error_log"
then
  total_file_size=0
  for filename in ${logpath}/*.LOG*
  do
    file_size_kb=`du -k "$filename" | cut -f1`
    let "total_file_size+=file_size_kb"
  done
  echo "IRIS/RDA LOG files copied, total ${total_file_size} kB."
else
  echo "No IRIS/RDA LOG files found!"
fi

# Export the full journalctl log to file
if journalctl -a > ${logpath}/system.log 2>>"$error_log"
then
  filename=${logpath}/system.log
  file_size_kb=`du -k "$filename" | cut -f1`
  echo "System log saved, file size ${file_size_kb} kB."
else
  echo "Saving the journalctl to system.log file failed!"
fi

# Export only RCP8 log from journalctl to file
if journalctl -au rcp8 > ${logpath}/rcp8.log 2>>"$error_log"
then
  filename=${logpath}/rcp8.log
  file_size_kb=`du -k "$filename" | cut -f1`
  echo "RCP8 log saved, file size ${file_size_kb} kB."
else
  echo "Saving the RCP8 log from journalctl failed."
fi

# Get the log from ifdr10
if ifdr_get_logs > ${logpath}/ifdr10.log 2>>"$error_log"
then
  IFDR10_GET_LOGS_SUCCESS=true
  filename=${logpath}/ifdr10.log
  file_size_kb=`du -k "$filename" | cut -f1`
  echo "IFDR10 log saved, file size ${file_size_kb} kB."
else
  echo "Saving the IFDR10 log failed."
fi

# Copy the MCU boot log files if exists
if cp ${IRIS_LOG}mcu* ${logpath} 2>>"$error_log"
then
  total_file_size=0
  for filename in ${logpath}/mcu*
  do
    file_size_kb=`du -k "$filename" | cut -f1`
    let "total_file_size+=file_size_kb"
  done
  echo "MCU boot log files copied, total ${total_file_size} kB."
# If mcu log files are not found, check also the older location for the files.
elif cp /tmp/mcu* ${logpath} 2>>"$error_log"
then
  total_file_size=0
  for filename in ${logpath}/mcu*
  do
    file_size_kb=`du -k "$filename" | cut -f1`
    let "total_file_size+=file_size_kb"
  done
  echo "MCU boot log files copied, total ${total_file_size} kB."
else
  echo "No MCU boot log files found!"
fi


# Copy the Ethernet switch log files if exists
if cp -r /var/log/remote-hosts ${logpath} 2>>"$error_log"
then
  total_file_size=0
  for filename in $(find ${logpath}/remote-hosts/ -type f)
  do
    file_size_kb=`du -k "$filename" | cut -f1`
    let "total_file_size+=file_size_kb"
  done
  echo "Ethernet switchs log files copied, total ${total_file_size} kB."
else
  echo "No Ethernet switch log files found!"
fi

# Copy IRIS/RDA configuration files
mkdir --parents ${logpath}/configuration
cp -r /etc/vaisala/irisrda ${logpath}/configuration/ 2>>"$error_log"
total_file_size=0
for filename in $(find ${logpath}/configuration/irisrda -type f)
do
  file_size_kb=`du -k "$filename" | cut -f1`
  let "total_file_size+=file_size_kb"
done
echo "IRIS/RDA configuration files copied, total ${total_file_size} kB."

# Copy IFDR10 configuration if IFDR10 is reachable
if [[ -n "$IFDR10_GET_LOGS_SUCCESS" ]]; then
  source /etc/profile.d/ifdr.sh
  ifdr_get_device_info > ${logpath}/configuration/ifdr_device_info.json 2>>"$error_log"
  if [ -s ${logpath}/configuration/ifdr_device_info.json ]; then
    ifdr_get_settings > ${logpath}/configuration/ifdr_settings.json 2>>"$error_log"
    ifdr_get_config_antenna > ${logpath}/configuration/ifdr_config_antenna.json 2>>"$error_log"
    ifdr_get_config_network > ${logpath}/configuration/ifdr_config_network.json 2>>"$error_log"
    ifdr_get_config_timesync > ${logpath}/configuration/ifdr_config_timesync.json 2>>"$error_log"

    total_file_size=0
    for filename in ${logpath}/configuration/ifdr_*.json
    do
      file_size_kb=`du -k "$filename" | cut -f1`
      let "total_file_size+=file_size_kb"
    done
    echo "IFDR10 configuration copied, total ${total_file_size} kB."
  else
    echo "Could not get IFDR10 device info, not copying IFDR10 configuration."
  fi
else
  echo "Not copying IFDR10 configuration, since failed to get IFDR10 logs."
fi

cp "$error_log" "$logpath/$error_log_name"

# Export a list of installed RPM packages from dnf
filename=${logpath}/installed_rpm_packages.txt
if dnf list --installed > "$filename" 2>>"$error_log"
then
  file_size_kb=`du -k "$filename" | cut -f1`
  echo "List of installed packages saved, file size ${file_size_kb} kB."
else
  echo "Saving the list of installed packages failed."
fi

# Pack everything to a tgz package
tar --create --gzip --file "$logname.tgz" --directory "$logpath_parent" "$logname"
tar_result="$?"
file_size_kb=`du -k "${logname}.tgz" | cut -f1`
echo "Log files packed to file $(pwd)/${logname}.tgz. File size ${file_size_kb} kB."

# Remove error log temp file, if tar succeeded
if [ $tar_result -eq 0 ]; then
  rm -f -- "$error_log"
fi
