#!/bin/sh
#
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2014-2014 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# This script will collect all info of a bareos install like:
# - logfiles
# - bactraces
# - tracebacks
# - core files
#
set -a

#
# Source the Bareos config functions.
#
. /usr/pkg/lib/bareos/scripts/bareos-config-lib.sh

working_dir=`get_working_dir`

get_compression_method()
{
   #
   # See what compression is available on the platform
   #
   bzip2=`which bzip2 2> /dev/null`
   gzip=`which gzip 2> /dev/null`

   if [ ! -z "${bzip2}" ]; then
      compressor="${bzip2} -z -c"
      comp_suffix=".bz2"
   else
      if [ ! -z "${gzip}" ]; then
         compressor="${gzip} -c"
         comp_suffix=".gz"
      else
         compressor="cat"
         comp_suffix=""
      fi
   fi
}

collect_debug_info()
{
   #
   # If there is a logfile capture it and compress them when possible.
   #
   if [ -f log ]; then
      ${compressor} log > collect.$$/log${comp_suffix}
   fi

   #
   # Capture all core files and compress them when possible.
   #
   for file in *.core.*
   do
      ${compressor} ${file} > collect.$$/${file}${comp_suffix}
      if [ ${REMOVE:-NO} = YES ]; then
         rm -f ${file}
      fi
   done

   #
   # Capture all bactrace files
   #
   for file in *.bactrace
   do
      cp -p ${file} collect.$$
      if [ ${REMOVE:-NO} = YES ]; then
         rm -f ${file}
      fi
   done

   #
   # Capture all traceback files
   #
   for file in *.traceback
   do
      cp -p ${file} collect.$$
      if [ ${REMOVE:-NO} = YES ]; then
         rm -f ${file}
      fi
   done
}

collect_pkg_info()
{
   case `uname -s` in
      Linux)
         LSB_DISTRIBUTOR=`lsb_release -i -s`
         case ${LSB_DISTRIBUTOR} in
            "SUSE LINUX")
               PKG_TYPE="rpm"
               if [ -f /etc/os-release ]; then
                  cat /etc/os-release
               else
                  if [ -f /etc/SuSE-release ]; then
                     cat /etc/SuSE-release
                  fi
               fi
               ;;
            "openSUSE project")
               PKG_TYPE="rpm"
               if [ -f /etc/os-release ]; then
                  cat /etc/os-release
               else
                  if [ -f /etc/SuSE-release ]; then
                     cat /etc/SuSE-release
                  fi
               fi
               ;;
            CentOS)
               PKG_TYPE="rpm"
               if [ -f /etc/redhat-release ]; then
                  cat /etc/redhat-release
               fi
               ;;
            Fedora)
               PKG_TYPE="rpm"
               if [ -f /etc/fedora-release ]; then
                  cat /etc/fedora-release
               fi
               ;;
            RedHatEnterprise*)
               PKG_TYPE="rpm"
               if [ -f /etc/redhat-release ]; then
                  cat /etc/redhat-release
               fi
               ;;
            Oracle*)
               PKG_TYPE="rpm"
               ;;
            MandrivaLinux)
               PKG_TYPE="rpm"
               ;;
            Arch|archlinux)
               ;;
            LinuxMint)
               ;;
            Debian)
               PKG_TYPE="deb"
               if [ -f /etc/debian_version ]; then
                  cat /etc/debian_version
               fi
               ;;
            Ubuntu)
               PKG_TYPE="deb"
               if [ -f /etc/debian_version ]; then
                  cat /etc/debian_version
               fi
               ;;
            Univention)
               PKG_TYPE="deb"
               if [ -f /etc/debian_version ]; then
                  cat /etc/debian_version
               fi
               ;;
            *)
               ;;
         esac

         case ${PKG_TYPE} in
            deb)
               dpkg-query -l '*bareos*'
               ;;
            rpm)
               rpm -qa | grep bareos
               ;;
            *)
               ;;
         esac
         ;;
      SunOS)
         for pkgname in `pkginfo | grep bareos | cut -d' ' -f2`
         do
            pkginfo -l ${pkgname}
         done
         ;;
      *)
         ;;
   esac > collect.$$/pkginfo
}

collect()
{
   output="bareos.capture.`date +%Y%m%d%H%M`.tar"
   cd ${working_dir} || exit 1

   #
   # Create a collection dir
   #
   mkdir collect.$$

   #
   # See what compression we can use to make things somewhat smaller.
   #
   get_compression_method

   #
   # Collect the debug information like postmortem core files, logs
   # and bactrace and traceback files.
   #
   collect_debug_info

   #
   # Collect the installed packages.
   #
   collect_pkg_info

   #
   # tar up the whole capture folder
   #
   tar cf ${output} collect.$$
   rm -rf collect.$$

   echo "Please upload the following file for support: ${workdir}/${output}"
}

usage()
{
   echo "Usage: $0 [-hr]"
}

main()
{
   #
   # Parse options.
   #
   while getopts "hr" arg
   do
      case ${arg} in
         r)
            REMOVE="YES"
            ;;
         h|\?)
            usage
            exit 0
            ;;
         *)
            usage
            exit 1
            ;;
      esac
   done
   nr_shift=`expr $OPTIND - 1`
   shift ${nr_shift}

   collect $*
}

main $*
