// Michel Galle, Felipe Gross Marques
// Número: 0873817, 0873834
// Turma: 33
// Sistemas Operacionais I
// Farlei J. Heinen
// Data : 16/09/03

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <string.h>
#include <sys/utsname.h>
#include <signal.h>
#include "shell.h"
#include <errno.h>

int main(int argc, char *argv[]) {
	char *parametros [10];
	char entrada[150];
	char path[50];
	char *comando;
	int x, sonpid, pid, sinal, teste, bg;
	struct sysinfo info;
	struct utsname version;
	TypeCommand comando_id ;

	bg = 0;
	signal(SIGCHLD, matafilho);

	do {
		getcwd(path,50);
		printf ("\n[LINSH@ %s ]$ ", path);
		fgets (entrada,150, stdin );

		// remove o \n do final da string
		entrada [ strlen ( entrada ) - 1 ] = '\0' ;
		if (strlen(entrada)==0)
			continue;
		comando = strtok (entrada, " ");

		parametros[0] = comando ;
		parametros[1] = NULL;
		x=1;
		parametros[x] = strtok(NULL, " ");
		fprintf(stdout, "Antes do while\n");
		if (parametros[x] != NULL) {
			if (strncmp(parametros[x],"&",1)==0) {
				fprintf(stdout, "'&' encontrado\n");
				bg = 1;
				parametros[x] = NULL;
			}
		}
		while (parametros[x] != NULL){
			x++;
			parametros[x] = strtok (NULL, " ");
		fprintf(stdout, "pegou um parametro %s\n",parametros[x]);
			if (parametros[x]==NULL)
				break;
			if (strncmp(parametros[x],"&",1)==0) {
				fprintf(stdout, "'&' encontrado");
				bg = 1;
				parametros[x] = NULL;
			}
		//	if ( *parametros[x] == '&' ) {
		//		bg = 1;
		//		parametros[x] = NULL;
		//	}
		if (bg == 0) {
			fprintf(stdout, "Passou pelo if");
		}
		}

		comando_id = parse_comando ( comando ) ;

		switch ( comando_id ) {
			case CD:
				x = chdir (parametros[0]);
				if (x!=0) {
					printf ("Arquivo ou diretório inválido\n");
				}
				break ;

			case PWD:
				printf("%s\n", path);
				break ;

			case HELP:
				/* ajuda do sistema */
				printf ( "Comandos:\n" ) ;
				printf ( "cd: altera o diretorio atual\n" ) ;
				printf ( "pwd: mostra o diretorio atual\n" ) ;
				printf ( "help: exibe isso\n" ) ;
				printf ( "exit: sai do shell\n" ) ;
				break ;

			case EXIT:
				/* trata o EXIT com a saida do switch */
				break ;
			default:
				sonpid = fork();
				if (sonpid==0){
					execvp(comando, parametros);
					printf ( "%s :Arquivo ou diretório não encontrado", comando) ;
					exit(0);
				}

				if ( bg == 0) {
					fprintf(stdout, "Esperando pelo filho...\n");
					waitpid ( sonpid,  NULL, WUNTRACED );
					fprintf(stdout, "Filho morto...\n");
				}
				bg = 0;
		}
	} while ( comando_id != EXIT ) ;

	return 0;
}

/* funcao para retornar o ID da funcao */
TypeCommand parse_comando ( char *comando )
{
	if ( !strcmp ( comando, "cd" ) )
		return CD ;
	else if ( !strcmp ( comando, "pwd" ) )
		return PWD ;
	else if ( !strcmp ( comando, "exit" ) )
		return EXIT ;
	else if ( !strcmp ( comando, "help" ) )
		return HELP ;
	else return UNKNOWN;
}
void matafilho(int sinal) {
	signal(SIGCHLD, matafilho);
	waitpid(-1, NULL, WNOHANG | WUNTRACED);
}

