#!/bin/sh

action=$1
DIR_BUILD_PATCH=build_patch

if [[ $action == "clean" ]]; then
	rm -rf linux-2.6.16*
	rm -rf $DIR_BUILD_PATCH
elif [[ $action == "build" ]]; then


	echo "Creation patch logos"

	files_before="files_before/Kconfig files_before/logo.c files_before/Makefile"
	files_after="files_after/Kconfig files_after/logo.c files_after/Makefile"
	logos="logos/logo_oz240_clut224.ppm logos/logo_oz480_clut224.ppm logos/logo_oz640_clut224.ppm"

	dest_before="linux-2.6.16.orig/drivers/video/logo"
	mkdir -p $dest_before
	for f in `echo $files_before`
	do
		echo $f
		if [[ ! -e $f ]]; then
			echo "Le fichier '$f' n'existe pas !!!"
			exit 1
		fi
		cp $f $dest_before
	done

	dest_after="linux-2.6.16/drivers/video/logo"
	mkdir -p $dest_after
	for f in `echo $files_after`
	do
		echo $f
		if [[ ! -e $f ]]; then
			echo "Le fichier '$f' n'existe pas !!!"
			exit 1
		fi
		cp $f $dest_after
	done


	# existence d'images jpg
	for j in `ls logos/*.jpg`
	do
		echo "Transformation de $j en ppm"
		basename=`basename $j`
		name=`echo $basename | sed s/.jpg//`
		jpegtopnm $j > /tmp/$name.pnm || exit 1
		pnmquant 224 /tmp/$name.pnm > /tmp/$name.224.pnm || exit 1
		pnmnoraw /tmp/$name.224.pnm > logos/$name.ppm || exit 1
		rm /tmp/$name.pnm  /tmp/$name.224.pnm
	done

	# Copie des logos
	for l in `ls logos`
	do
		cp logos/$l $dest_after
	done

	mkdir $DIR_BUILD_PATCH
	diff -Naur linux-2.6.16.orig linux-2.6.16 > $DIR_BUILD_PATCH/logo_oz-r2.patch
	bzip2 $DIR_BUILD_PATCH/logo_oz-r2.patch
	md5sum $DIR_BUILD_PATCH/logo_oz-r2.patch.bz2 > $DIR_BUILD_PATCH/logo_oz-r2.patch.bz2.md5
	echo "Copier les patchs situés dans $DIR_BUILD_PATCH vers le répertoire des sources (/stuff/sources)"
else
	echo "$0 build|clean"
	echo "Nota: S'il existe une image jpg (logos/logo_oz*) elle sera utilisée comme logo :)"
	exit 1
fi
