#!/bin/bash
#
# save_y2logs - save YaST2 logs to a compressed tar file
#		to attach it to a Bugzilla bug report
#
# Author: Stefan Hundhammer <sh@suse.de>

PATH=/usr/sbin:/usr/bin:/sbin:/bin

usage()
{
    echo "Usage: ${0##*/} [ARCHIVE_NAME]" >&2
    echo "" >&2
    echo "Copies the YaST2 logs to a (compressed) tar archive." >&2
    echo "If file name is missing it is generated by the script." >&2
    exit 1
}

if test -z "$1"; then
  # choose some good default
  if [ "$(type -p xz)" ] ; then
    SUFFIX=.tar.xz
    COMPRESSION=xz
  elif [ "$(type -p gzip)" ] ; then
    SUFFIX=.tar.gz
    COMPRESSION=gzip
  elif [ "$(type -p bzip2)" ] ; then
    SUFFIX=.tar.bz2
    COMPRESSION=bzip2
  else
    SUFFIX=.tar
    COMPRESSION=
  fi
  TARGET=`mktemp --suffix $SUFFIX /tmp/y2log-XXXXXX`
else
  case "$1" in
    -*)
	usage
	;;

    *.tar)
	COMPRESSION=
	;;

    *.tgz|*.tar.gz)
	COMPRESSION=gzip
	;;

    *.txz|*.tar.xz)
	COMPRESSION=xz
	;;

    *.tar.bz2)
	COMPRESSION=bzip2
	;;

    *)
	echo "FATAL: argument is not a (compressed) tar file." >&2
	echo "Use one of these suffixes: .tar .tgz .tar.gz .txz .tar.xz .tar.bz2" >&2
	exit 4
  esac
  # use full path
  TARGET=$(readlink -m "$1")
fi

# check for compression program
if [ "$COMPRESSION" -a -z "$(type -p $COMPRESSION)" ]; then
  echo "FATAL: $COMPRESSION not available" >&2
  exit 3
fi

TMPDIR=`mktemp -d` || ( echo "FATAL: Failed to create a temporary directory" >&2; exit 5 );


# 1. gather some information in $TMPDIR

# last kernel messages, if any
if [ "$(type -p dmesg)" ]; then
  dmesg > $TMPDIR/dmesg
  LIST_TMP="$LIST_TMP dmesg"
fi

if [ "$(type -p journalctl)" ]; then
  journalctl --dmesg > $TMPDIR/journalctl-dmesg
  LIST_TMP="$LIST_TMP journalctl-dmesg"
fi

# strip sensitive information
if [ -f /etc/install.inf ]; then
  sed 's/\(^WlanESSID: \|^WlanKey: \|^RootPassword: \|^VNCPassword: \|^Password: \).*$/\1XXXXXX/' </etc/install.inf >$TMPDIR/install.inf
  LIST_TMP="$LIST_TMP install.inf"
fi

# if storing logs at the end of installation after bootloader fail, try to store pbl log from target system
if [ -f /mnt/var/log/pbl.log ]; then
  cp /mnt/var/log/pbl.log $TMPDIR/pbl-target.log
  LIST_TMP="$LIST_TMP pbl-target.log"
fi

# installed packages
if [ -f /var/lib/rpm/Packages -a "$(type -p rpm)" ]; then
  rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}\t\t\t(%{VENDOR})\t%{DISTRIBUTION}\n' 2>/dev/null | sort >/$TMPDIR/rpm-qa
  LIST_TMP="$LIST_TMP rpm-qa"
fi

# rename, so people can see it
if [ -f /.packages.root ]; then
  cp /.packages.root $TMPDIR/_packages.root
  LIST_TMP="$LIST_TMP _packages.root"
fi


# 2. things from /var/log relevant for us

VAR_LOG_FILES='\
  YaST2 \
  Xorg.0.log \
  zypper.log zypp/history* pk_backend_zypp \
  pbl.log linuxrc.log wickedd.log \
  evms-engine.* \
  boot.msg messages udev.log \
'
cd /var/log
for i in $VAR_LOG_FILES ; do
  [ -e "$i" ] && LIST_VAR="$LIST_VAR $i"
done
cd /


# 3. any other files

FILES='\
  etc/X11/xorg.conf etc/X11/xorg.conf.install \
  etc/X11/xorg.conf.d linuxrc.config etc/os-release \
'
for i in $FILES ; do
  [ -e "$i" ] && LIST="$LIST $i"
done


echo "Saving YaST logs to $TARGET" >&2

[ -n "$COMPRESSION" ] && COMPRESSION="--$COMPRESSION"

tar cf "$TARGET" $COMPRESSION --dereference -C / $LIST -C /var/log $LIST_VAR -C $TMPDIR $LIST_TMP
err=$?

rm -rf $TMPDIR

[ $err != 0 ] && echo "FATAL: Error creating archive $TARGET" >&2

exit $err
