#!/bin/bash

# Copyright (c) 2010, Sebastian Siebert (mail@sebastian-siebert.de)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY SEBASTIAN SIEBERT ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL SEBASTIAN SIEBERT BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Version 1.1 - add the upload functionality
# Version 1.0 - inital development

# Webpage:
# http://www.sebastian-siebert.de/

VERSION="1.1"

echo -e "ati-report.sh - Version ${VERSION}\n"
echo "Copyright (c) 2010, Sebastian Siebert (mail@sebastian-siebert.de)"
echo "All rights reserved."
echo -e "This script is under the modified BSD License (2-clause license)\n"

UPLOAD="false"
REPORTFILE="ati-report.txt"

while [ "$#" -gt "0" ]; do
    case $1 in
        -u|--upload)
            UPLOAD="true"
            shift 1
        ;;
    esac
done

echo "Generating a report file \"${REPORTFILE}\" in the directory `pwd` ..."

if [ -f ${REPORTFILE} ]; then
    rm ${REPORTFILE}
fi

touch ${REPORTFILE}

report_header() {
    echo "------------------------------------------------" >>${REPORTFILE}
    echo "   System and diagnostic report" >>${REPORTFILE}
    echo "------------------------------------------------" >>${REPORTFILE}
    echo "   Generated by $(basename $0) (Version ${VERSION})" >>${REPORTFILE}
    echo "      on `date "+%Y-%m-%d %T"`" >>${REPORTFILE}
    echo "" >>${REPORTFILE}
    echo "   The script was written by Sebastian Siebert" >>${REPORTFILE}
    echo "   and is under the modified BSD License" >>${REPORTFILE}
    echo "   (2-clause license)" >>${REPORTFILE}
    echo "" >>${REPORTFILE}
    echo "   Website: http://www.sebastian-siebert.de" >>${REPORTFILE}
    echo "------------------------------------------------" >>${REPORTFILE}
    echo -n -e "\n\n\n" >>${REPORTFILE}
}

report_echo() {
    echo "   OUTPUT (echo): $1"
    echo "# OUTPUT (echo): $1" >>${REPORTFILE}
    shift 1
    echo "------------------------------------------------" >>${REPORTFILE}
    for ECHO_SINGLELINE in "$@"; do
        echo "${ECHO_SINGLELINE}" >>${REPORTFILE}
    done
    #echo "$@" >>${REPORTFILE}
    echo "================================================" >>${REPORTFILE}
    echo -n -e "\n\n\n" >>${REPORTFILE}
}

report_output() {
    echo "   OUTPUT: $@"
    echo "# OUTPUT: $@" >>${REPORTFILE}
    echo "------------------------------------------------" >>${REPORTFILE}
    echo "`$@ 2>&1`" >>${REPORTFILE}
    echo "================================================" >>${REPORTFILE}
    echo -n -e "\n\n\n" >>${REPORTFILE}
}

report_file() {
    echo "   FILE: $1"
    echo "# FILE:      $1" >>${REPORTFILE}
    echo "# ATTRIBUTE: `ls -l $1 2>/dev/null`" >>${REPORTFILE}
    echo "------------------------------------------------" >>${REPORTFILE}
    echo "`cat $1 2>&1`" >>${REPORTFILE}
    echo "================================================" >>${REPORTFILE}
    echo -n -e "\n\n\n" >>${REPORTFILE}
}

report_files() {
    REPORT_DIR="`ls $@`"
    for REPORT_DIR_FILE in ${REPORT_DIR}; do
        report_file ${REPORT_DIR_FILE}
    done
}

report_header
report_output `which uname` -a
report_file /etc/SuSE-release
report_file /proc/version
report_output `which gcc` --version
#report_output `which fglrxinfo`
report_output `which ldd` `which fglrxinfo`
report_output `which env`
report_file /proc/cpuinfo
report_file /proc/meminfo
report_output `which lspci` -nn
report_file /proc/pci
report_files /proc/ati/*/*
report_files /etc/X11/xorg.conf.d/*
report_file /etc/X11/xorg.conf
report_files /var/log/Xorg.*.log
report_output `which dmesg`
report_echo "lsmod | grep fglrx" `lsmod | grep fglrx`
report_output `which lsmod`
report_file /etc/ati/amdpcsdb
report_file /etc/ati/amdpcsdb.default
report_output `which hwinfo` --monitor
report_output `which hwinfo` --gfxcard
report_echo "rpm -qa | grep fglrx | sort" `rpm -qa | grep fglrx | sort`
report_echo "rpm -qa | grep kernel | sort" `rpm -qa | grep kernel | sort`
report_file /boot/grub/menu.lst
report_file /etc/sysconfig/kernel
report_files /home/*/.kde4/share/config/kwinrc

if [ "${UPLOAD}" = "true" ]; then
    REPORTURL="`cat ati-report.txt | curl -F 'sprunge=<-' http://sprunge.us -s 2>&1`"
    echo -n -e "\n\nThe report was uploaded to sprunge.us.\n   The Link is ${REPORTURL}\n"
fi

echo "Finished!"
