#!/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.

# Changelog:
# Version 1.1 - Add tool "patch" for the ATI-Installation (thanks to Matthias)
# Version 1.0 - inital development

# Webpage:
# http://www.sebastian-siebert.de/2009/12/27/opensuse-11-2-proprietaeren-grafik-treiber-ati-catalyst-9-12-als-rpm-installieren/

ATIDIR="ati-9.12"
ATIINSTALLER="ati-driver-installer-9-12-x86.x86_64.run"
OPENSUSERELEASE="/etc/SuSE-release"

# Check the size of the console
set -- $(stty size 2> /dev/null || echo 0 0)
LINES=$1
COLUMNS=$2
if [ ${LINES} -eq 0 ]; then
    LINES=24
fi
if [ ${COLUMNS} -eq 0 ]; then
    COLUMNS=80
fi

print_okay() {
    echo -e "\033[${COLUMNS}C\033[15D[\e[1;32m OK \e[0;37m]"
}
print_failure() {
    echo -e "\033[${COLUMNS}C\033[15D[\e[1;31m FAILURE \e[0;37m]"
}
print_available() {
    echo -e "[\e[1;32m AVAILABLE \e[0;37m]"
}
print_missing() {
    echo -e "[\e[1;31m MISSING \e[0;37m]"
}

check_install() {
    PROGRAM=$1
#    ONLYRETURN=$2
    echo -n -e "   check ${PROGRAM}... "
    if [ "`LANG="en_EN.UTF-8" zypper -q info ${PROGRAM} | grep "Installed:" | cut -c 12-`" = "Yes" ]; then
#        if [ ${ONLYRETURN} = "yes" ]; then
#            return 0
#        fi
        print_available
    else
#        if [ ${ONLYRETURN} = "yes" ]; then
#            return 1
#        fi
        print_missing
        echo -n -e "\n   missing \"${PROGRAM}\"-package: try to run \"zypper install ${PROGRAM}\"\n"
        zypper -q in ${PROGRAM}
    fi
}

echo -e "*******************************************************************"
echo -e "*                                                                 *"
echo -e "*   Script:       makerpm-ati-9.12.sh                             *"
echo -e "*   Version:      1.0                                             *"
echo -e "*   Written by:   Sebastian Siebert (mail@sebastian-siebert.de)   *"
echo -e "*                                                                 *"
echo -e "*   Description:  This script helps you to create a rpm package   *"
echo -e "*                 from the propritary ATI installer               *"
echo -e "*                                                                 *"
echo -e "*   License:      This script is under the                        *"
echo -e "*                 modified BSD License (2-clause license)         *"
echo -e "*                                                                 *"
echo -e "*******************************************************************\n"

echo -n "Check for running this script as root..."
if [ $(whoami) != "root" ]; then
    echo -n -e "\n   Error: This script needs to run as root!"
    print_failure
    exit 1
else
    print_okay
fi

echo -n "Check for existing ATI-Installer \"${ATIINSTALLER}\" in current directory..."
if [ -f ${ATIINSTALLER} ]; then
    print_okay
else
    echo -n -e "\n   Error: Please download the ATI Installer from http://support.amd.com/us/gpudownload/Pages/index.aspx"
    print_failure
    exit 1
fi

echo -n "Set the correct permissions of ATI-Installer \"${ATIINSTALLER}\"..."
chown root:root ${ATIINSTALLER} && chmod 744 ${ATIINSTALLER}
if [ $? -eq 0 ]; then
    print_okay
else
    echo -n -e "\n   Error: permissions could not be set"
    print_failure
    exit 1
fi

echo -n "Grab openSUSE Version..."
if [ -f ${OPENSUSERELEASE} ]; then
    OPENSUSEVERSION=`cat ${OPENSUSERELEASE} | grep VERSION | cut -c 11-`
    ATIVERSION=`echo "${OPENSUSEVERSION}" | sed -e 's/\.//g'`
    echo -n -e "\n   openSUSE Version = ${OPENSUSEVERSION} (${ATIVERSION})"
    print_okay
else
    echo -n -e "\n   Error: This script runs only under openSUSE!"
    print_failure
    exit 1
fi

echo "Check the installed development packages (Please wait a minute)..."
check_install "gcc"
check_install "make"
check_install "patch"
check_install "kernel-source"
check_install "linux-kernel-headers"
check_install "kernel-$(uname -r | cut -f 3 -d "-")-devel"
print_okay

echo -n "Get the architecture of this machine..."
ARCH=$(uname -m)
case "${ARCH}" in
i?86)
    ATIARCH="IA32"
    ;;
x86_64)
    ATIARCH="AMD64"
    ;;
esac
echo -n -e "\n   Architecture: ${ARCH} (${ATIARCH})"
print_okay

PACKAGE="SuSE/SUSE${ATIVERSION}-${ATIARCH}"
echo -n -e "\n\nBuild the RPM-Packages \"${PACKAGE}\"...\n"
echo -n -e "----- START: ATI INSTALLER ---\n"
./${ATIINSTALLER} --buildpkg ${PACKAGE} | tee atiinstaller.log
echo -n -e "----- END: ATI INSTALLER ---\n\n"
RPMFILEWP=`cat atiinstaller.log | grep "generated" | cut -f 2 -d " "`
if [ -n "${RPMFILEWP}" ]; then
    echo -n -e "RPM-Package was builded successfully!"
    print_okay
    RPMFILE=`basename ${RPMFILEWP}`
    echo -n -e "\nInstall the RPM-Package with: rpm -ihv ${RPMFILE}\n"
    echo -n -e "   OR\n"
    echo -n -e "Update the RPM-Package with: rpm -Uhv ${RPMFILE}\n"
else
    echo -n -e "Error: RPM-Package was NOT builded!"
    print_failure
fi

# clean up
rm -f atiinstaller.log
