/*

    Copyright 2010 Plácido Fernández

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    @author - Plácido Fernández for http://www.enalpha.com
    @author - Javier Monge for http://www.enalpha.com

 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
char* cadena = "magic_lamp_max_waves";
char* cadena1 = "Den Magiska Lampans maximala antal v";
int fd;
int fd1;
int cp;

int modificarSO(){

	char letra;
	int leido = 0;
	leido = read(fd, &letra, 1);
	int encontrado = 0;
	int semiencontrado = 0;
	int cont1 = 0;
	int longitud = strlen(cadena);

	while (leido == 1 && encontrado == 0){

		if (letra == cadena[0]){
			semiencontrado = 1;
		}
		else {
			semiencontrado = 0;
		}
		leido = read(fd, &letra, 1);
		cont1 = 1;

		while (leido == 1 && semiencontrado == 1 && encontrado == 0 && cont1 < longitud){
			
			if (cadena[cont1] == letra){}
			else {
				semiencontrado = 0;
			}
			leido = read(fd, &letra, 1);
			cont1++;
			//printf("%c", letra);
		}
		if (semiencontrado == 1){
			encontrado = 1;
		}
	}

	int i = 0;
	for (i = 0; i < 5; i++){
		read(fd, &letra, 1);
	}
	letra = '0';
	leido = write(fd, &letra, 1);
	if (leido > 0){
		printf ("/usr/lib/compiz/libanimation.so changed\n");
	}
	/*
	if (encontrado == 1){
		printf ("Cadena encontrada\n");
	}
	else {
		printf ("Cadena no encontrada\n");
	}*/
}

int modificarXML(){

	char letra;
		int leido = 0;
		leido = read(fd, &letra, 1);
		int encontrado = 0;
		int semiencontrado = 0;
		int cont1 = 0;
		int longitud = strlen(cadena1);

		while (leido == 1 && encontrado == 0){

			if (letra == cadena1[0]){
				semiencontrado = 1;
			}
			else {
				semiencontrado = 0;
			}
			leido = read(fd, &letra, 1);
			cont1 = 1;

			while (leido == 1 && semiencontrado == 1 && encontrado == 0 && cont1 < longitud){

				if (cadena1[cont1] == letra){}
				else {
					semiencontrado = 0;
				}
				leido = read(fd, &letra, 1);
				cont1++;
				//printf("%c", letra);
			}
			if (semiencontrado == 1){
				encontrado = 1;
			}
		}

		int i = 0;
		for (i = 0; i < 120; i++){
			read(fd, &letra, 1);
		}
		letra = '0';
		leido = write(fd, &letra, 1);
		if (leido > 0){
			printf ("/usr/share/compiz/animation.xml changed\n");
		}
		/*
		if (encontrado == 1){
			printf ("encontrado\n");
		}
		else {
			printf ("no encontrado\n");
		}*/
}

int main(){

	system("cp /usr/lib/compiz/libanimation.so /usr/lib/compiz/libanimation.so.copy");
	printf("Original file copied to /usr/lib/compiz/libanimation.so.copy\n");
	system("cp /usr/share/compiz/animation.xml /usr/share/compiz/animation.xml.copy");
	printf("Original file copied to /usr/share/compiz/animation.xml.copy\n");

	fd = open("/usr/lib/compiz/libanimation.so", O_RDWR);
	if (fd < 0) {
		printf ("Error opening the file\nTry with sudo\n");
		exit(-1);
	}
	modificarSO();
	close(fd);
	fd1 = open("/usr/share/compiz/animation.xml", O_RDWR);

	if (fd1 < 0) {
		printf ("Error opening the file\nTry with sudo\n");
		exit(-1);
	}
	modificarXML();
	close(fd1);

	printf("Ready!\n");
return 0;
}


