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

echo "$(basename $0) - Version 1.0"
echo "This script removes the water mark in the fglrx driver."
echo "Copyright (c) 2011, Sebastian Siebert (mail@sebastian-siebert.de)"
echo -e "All rights reserved.\n"

if [ "`whoami`" != "root" ]; then
    echo "Error: This script needs to run as root!"
    exit 1
fi

ARCH="`uname -m`"
case "${ARCH}" in
i?86)
    DRIVER="/usr/lib/xorg/modules/drivers/fglrx_drv.so"
    ;;
x86_64)
    DRIVER="/usr/lib64/xorg/modules/drivers/fglrx_drv.so"
    ;;
esac

if [ -w "${DRIVER}" ]; then
    echo -n "Please wait while removing the water mark "
    for REPLACEMENT in $(objdump -d ${DRIVER} | awk '/call/&&/EnableLogo/{print "\\x"$2"\\x"$3"\\x"$4"\\x"$5"\\x"$6}')
    do
        sed -i "s/${REPLACEMENT}/\x90\x90\x90\x90\x90/g" ${DRIVER}
        echo -n "."
    done

    echo -e "\n\nWater mark was removed! Please restart your computer."
else
    echo "Error: The file ${DRIVER} is not writable!"
fi

