/*********************************************************************
 * Author: B. Alex Bridges                                           *
 * File: lab08.c                                                     * 
 * Class: CPSC-202 Lab Section 3                                     *
 * Project: Lab 8 - Scrambled Mail                                   *
 * Purpose: This program will e-mail a scrambled message to a        *
 *          specified user.                                          *
 *********************************************************************/

/* INCLUDES */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

/* FUNCTION PROTOTYPE(S) */
int f_Offspring(void);
void f_StandBy(int pid_Value);


/********************************************************************* 
 * MAIN FUNCTION                                                     *
 *********************************************************************/
/*************************************************************************
 * Purpose: To e-mail a scrambled message to specified user(s).          *
 * Input: --COMMAND-LINE ARGUMENTS--                                     *
 *        1. 'I_FILE' = The input file of the unscrambled message        *
 *        2. # = The number of passes to make                            *
 *        3+. 'E-MAIL_ADDRESS(ES)' = The e-mail address(es) of the       *
 *                                   recipient(s).                       *
 * Output: --RETURN VALUES--                                             *
 *         => COMPLETED SUCCESSFULLY = 0                                 *
 *         => ERROR = -1                                                 *
 * Called Routines: f_Offspring, f_StandBy                               *
 * Assumptions: N/A                                                      *
 *************************************************************************/
int main(int argc, char *argv[]) /* SETS UP COMMAND-LINE ARGUMENTS */
{
	/* --LOCAL VARIABLES-- */
	char charA_O_File[21+1];/* THE FILENAME OF THE TEMP. OUTPUT FILE */
	char *p_O_File;			/* CHAR. POINTER TO THE TEMP. OUTPUT FILE */
	pid_t pid_Value;		/* THE PROCESS ID VALUE */
	int int_i;				/* LOOP COUNTER/ARRAY INDEX FOR E-MAIL ADDRESSES */
	
	/* --FUNCTION-- */
	/* HEADER */
	printf("B. Alex Bridges\n"
	       "CPSC-202, Winter 1998\n"
	       "The Scrambled Mail Program\n"
	       "\n"); /* BLANK LINE */ 
	
	/* CHECKING FOR REQUIRED ARGUMENTS */
	if( argc < 4 )
	{	
		printf("\aERROR: The following command-line arguments must be included:\n"
		       " => 1. 'I_FILE' = The input file of the unscrambled message.\n"
		       " => 2. # = The number of passes to make.\n"
		       " => 3+. 'E-MAIL_ADDRESS(ES)' = The e-mail address(es) of the recipient(s).\n");
		exit(-1); /* ERROR */
	} /* if */
	
	/* CREATE TEMP. OUTPUT FILE */
	printf("Creating scrambled file . . . ");
	strcpy(charA_O_File, "/tmp/Scrambled_XXXXXX");
		if( ( p_O_File = mktemp(charA_O_File) ) == 0 )
	{
		perror("ERROR: mktemp Scrambled File");
		exit(-1); /* ERROR */
	} /* if */;
	printf("COMPLETED! \n");
		
	/* SCRAMBLE MESSAGE */
	printf("\n"); /* BLANK LINE */
	if( ( pid_Value = f_Offspring() ) == 0 )
	{
		/* Scrambler S # 'I_FILE' 'O_FILE' */
		execl("./Scrambler", "Scrambler", "S", argv[2], argv[1], p_O_File, NULL);
		
		/* IF execl FAILS */
		perror("ERROR: execl Scrambler");
		exit(-1); /* ERROR */
	} /* if */
	f_StandBy(pid_Value);
	printf("\n"); /* BLANK LINE */
		
	/* E-MAIL SCRAMBLED MESSAGE */
	for( int_i = 3; int_i <= ( argc - 1 ); int_i++ )
	{
		printf("E-mailing '%s' scrambled file  . . . ", argv[int_i]);
		if( ( pid_Value = f_Offspring() ) == 0 )
		{
			/* mpack -s Scrambled File 'O_FILE' 'E-MAIL_ADDRESS' */
			execl("/usr/local/bin/mpack", "mpack", "-s A Scrambled File", p_O_File, argv[int_i], NULL);

			/* IF execl FAILS */
			perror("ERROR: mpack Scrambled Message");
			exit(-1); /* ERROR */
		} /* if */
		f_StandBy(pid_Value);
		printf("COMPLETED! \n");
	} /* for */
	
	/* DELETE TEMP. OUTPUT FILE */
	printf("Removing scrambled file . . . ");
	if ( ( remove(p_O_File) ) == -1)
	{
		perror("ERROR: remove Scrambled File");
		exit(-1); /* ERROR */
	} /* if */
	printf("COMPLETED! \n");
	
	return(0); /* COMPLETED SUCCESSFULLY */
} /* main */

/********************************************************************* 
 * ADDITIONAL FUNCTIONS                                              *
 *********************************************************************/
/*************************************************************************
 * Purpose: To create a child process.                                   *
 * Input: N/A                                                            *
 * Output: --RETURN VALUES--                                             *
 *         => COMPLETED SUCCESSFULLY  = Process ID value of the child.   *
 *         => ERROR = -1                                                 *
 * Called Routines: N/A                                                  *
 * Assumptions: N/A                                                      *
 *************************************************************************/
int f_Offspring(void)
{
	/* --LOCAL VARIABLES-- */
	pid_t pid_Value;	/* THE PROCESS ID VALUE */
	
	if( ( pid_Value = fork() ) < 0 )
	{
		perror("fork");
		exit(-1); /* ERROR */
	} /* if */
	
	return(pid_Value);
} /* f_Offspring */

/*************************************************************************
 * Purpose: Waits for child process to complete.                         *
 * Input: pid_Value = The process ID value of the child.                 *
 * Output: N/A                                                           *
 * Called Routines: N/A                                                  *
 * Assumptions: All of the child processes terminated normally.          *
 *************************************************************************/
void f_StandBy(int pid_Value)
{
	while( wait( (int *) 0 ) != pid_Value )
		continue;
} /* f_StandBy */
