#!/bin/bash
#
# Copyright (c) 2011, 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.

# Changelog:
#
# 1.2 - add additional "openSUSE:Tumbleweed" as exclusion (Thanks to Mike)
# 1.1 - speed optimization
# 1.0 - initial version

box_full() {
    printf '*%.0s' $(seq 1 67)
    echo ""
}

box_content() {
    echo -n "*"
    if [ $# -eq 0 ]; then
        printf ' %.0s' $(seq 1 65)
    elif [ $# -eq 1 ]; then
        RIGHT="$1"
        printf ' %.0s' $(seq 1 17)
        echo -n "${RIGHT}"
        printf ' %.0s' $(seq 1 $[48-${#RIGHT}])
    elif [ $# -eq 2 ]; then
        LEFT="$1"
        RIGHT="$2"
        printf ' %.0s' $(seq 1 3)
        echo -n "${LEFT}"
        printf ' %.0s' $(seq 1 $[14-${#LEFT}])
        echo -n "${RIGHT}"
        printf ' %.0s' $(seq 1 $[48-${#RIGHT}])
    fi
    echo "*"
}

SCRIPTVERSION="1.2"

# Get current openSUSE-Version
OPENSUSE_MAJOR_VERSION="`grep 'VERSION' /etc/SuSE-release | sed -e 's/VERSION\s=\s//g' | cut -d"." -f1`"
OPENSUSE_MINOR_VERSION="`grep 'VERSION' /etc/SuSE-release | sed -e 's/VERSION\s=\s//g' | cut -d"." -f2`"

IFS=$'\012'

box_full
box_content
box_content "Script:" "$(basename $0)"
box_content "Version:" "${SCRIPTVERSION}"
box_content "Written by:" "Sebastian Siebert <mail@sebastian-siebert.de>"
box_content
box_content "Description:" "List all installed old packages"
box_content "on this openSUSE ${OPENSUSE_MAJOR_VERSION}.${OPENSUSE_MINOR_VERSION}"
box_content
box_content "License:" "This script is under the"
box_content "modified BSD License (2-clause license)"
box_content
box_full

if [ "$(whoami)" != "root" ]; then
    echo -e "\nERROR: This script should run as root!\n"
    exit 1
fi

echo -e "\nPlease wait, this may take a while ...\n"

for PACKAGES in `rpm -qa --qf '%{name}#%{version}#%{release}#%{distribution}\n' | sort`
do
    PKGDISTRO="`echo "${PACKAGES}" | cut -d'#' -f4`"
    if [ -n "`echo "${PKGDISTRO}" | grep -v -E "${OPENSUSE_MAJOR_VERSION}\.${OPENSUSE_MINOR_VERSION}|(none)|openSUSE\:Tumbleweed"`" ]; then
        PKGNAME="`echo "${PACKAGES}" | cut -d'#' -f1`"
        PKGVERSION="`echo "${PACKAGES}" | cut -d'#' -f2`"
        PKGRELEASE="`echo "${PACKAGES}" | cut -d'#' -f3`"
        echo "${PKGNAME}-${PKGVERSION}-${PKGRELEASE} -> ${PKGDISTRO}"
    fi
done

exit 0
