#!/bin/sh
#
# fblogo_patch
#
# Generate a kernel patch using a kernel source tree
# and a custom logo header
#
# Gordon Fraser <gordon@freakzone.net> 2002/2003
#
# This software comes with ABSOLUTELY NO WARRANTY
# This software is free software, and you are welcome to redistribute it
# under certain conditions
# See the COPYING file for details.
#

usage() {
  echo "Usage: $0 <logo-header> <path-to-linux> [<width> <height>]" 1>&2
  exit 1
}

error() {
  echo "fbcon_patch error: $1" 1>&2
  exit 1
}

if [ $# -ne 2 ] && [ $# -ne 4 ]
then
  usage
fi

LOGOFILE=$1
SRCDIR=$2
TMPFILE=/tmp/fbcon.c_tmp

if [ $# -eq 4 ]
then
  WIDTH=$3
  HEIGHT=$4
else
  WIDTH=0
  HEIGHT=0
fi



if [ ! -e $LOGOFILE ]
then
  error "$LOGOFILE is not a valid header file"
fi

if [ ! -d $SRCDIR ] || [ ! -d $SRCDIR/drivers/video ] || [ ! -d $SRCDIR/include/linux ]
then
  error "$SRCDIR is not a valid directory"
fi

if [ ! -e $SRCDIR/drivers/video/fbcon.c ]
then
  error "$SRCDIR/drivers/video/fbcon.c not found - this doesn't seem to be a valid kernel source directory"
fi

if [ "$WIDTH" -le "0" ]
then
  WIDTH=$(grep "LOGO_W 80\" to" $LOGOFILE | sed -e 's/.*"#define\ LOGO_W\ 80"\ to\ "#define\ LOGO_W\ \([0-9]\+\)"/\1/')
  if [ -z "$WIDTH" ] || [ "$WIDTH" -le "0" ]
  then
    error "width could not be detected, try to specify it on the commandline"
  fi
fi

if [ $HEIGHT -le 0 ]
then
  HEIGHT=$(grep "LOGO_H 80\" to" $LOGOFILE | sed -e 's/.*"#define\ LOGO_H\ 80"\ to\ "#define\ LOGO_H\ \([0-9]\+\)"/\1/')
  if [ -z "$HEIGHT" ] || [ $HEIGHT -le 0 ]
  then
    error "height could not be detected, try to specify it on the commandline"
  fi
fi

cat $SRCDIR/drivers/video/fbcon.c | \
sed -e "s/#define\ LOGO_H\([ 	]*\).*/#define\ LOGO_H\1$HEIGHT/" | \
sed -e "s/#define\ LOGO_W\([ 	]*\).*/#define\ LOGO_W\1$WIDTH/" \
>$TMPFILE

LOGONAME=$(echo $LOGOFILE | sed -e 's/\([\/\.]\)/\\\1/g')
TMPNAME=$(echo $TMPFILE | sed -e 's/\([\/\.]\)/\\\1/g')
SRCDIRNAME=$(echo $SRCDIR | sed -e 's/\([\/\.]\)/\\\1/g')

diff -u $SRCDIR/drivers/video/fbcon.c $TMPFILE |\
sed -e "s/$SRCDIRNAME/linux-old/" |\
sed -e "s/$TMPNAME/linux\/drivers\/video\/fbcon\.c/"

diff -u $SRCDIR/include/linux/linux_logo.h $LOGOFILE | \
sed -e "s/$SRCDIRNAME/linux-old/" |\
sed -e "s/$LOGONAME/linux\/include\/linux\/linux_logo\.h/"

rm -rf $TMPFILE
