// C++ program to run  "mpeg_encode" version 1.5 (University of California, Berkeley)
// It is designed to take still pictures and turn them into a slide show movie using a linux shell
// Author Ari Palczewski  St. John's University Collegeville MN, 2004

#include <stdlib.h>
#include<fstream>
#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/dir.h>



main()
{
// all the variables in the program are placed first
char savename[80];
char directory[200],temp[200];
float frames_per_second;
int jpegorpng;
ofstream Variables;
char file_name[80];
char *type;
float i=1;
int j=1;
char k[80],l[80];
int how_to_enter_file;	
char beginning_name[80],beginning_name2[80]; 
int first_number;
int last_number;
int number_step;
char ending[80],ending2[80];
int sw=1,sw2=1,sw3=1,sw4=1;
//char directorys[201];

struct stat info;
char temperary_file[21]="/tmp/mpeg_encode_run";	
	cout<<endl<<endl<<"Mpeg Creator"<<endl<<endl;	//reads out title of program
	cout<<"This program is designed to take different types of graphic files and turn them into mpeg movies.  Before you start you must know a couple of things about the program"<<endl<<"1. All input and output file cannot contain spaces"<<endl<< "2. All mpegs are encoded at 30 frames per second"<<endl<<"3. You can put in what ever frame rate you would like i.e. for single pictures if you put in 1 fps the program actually encodes that file 30 times giving you an image that changes once every second, if you put in .5 it will encode the file 60 times giving you 1 effective frame for every 2 seconds and so on."<<endl<<endl;  //introduction to program whenever it starts to help clarify how to use program
	cout<<"What is the name of the file you want to have the mpeg saved as?"<<endl; // the name of the file, the file will be written to directory were this program is placed 
	cin>>savename;
	cout<<"In what directory are the pictures located?"<<endl;
	while (sw3==1)	//checks the directory before you can move on to make sure that the directory actually exists
	{
		cin>>directory;
		if (stat(directory,&info)==0)
		{
			sw3==0;
			break;
		}
		else 
		{
			cout<<"Your directory is invalid, please enter a different directory"<<endl;
		}
	}	
	cout<<"What is the effective frame per second?"<< endl;
	cin>>frames_per_second;
	cout<<"What is the original format of the pictures you are putting in 1)jpeg 2)png 3)gif 4)pnm 5) eps"<< endl; 
        //program can write in either jpeg or pnm that's why below "BASE_FILE_FORMAT" is set to one or other. "mpeg_encode" has other formats that can be later added but for now I have only input the file types I need
	
	Variables.open(temperary_file); //open a file for "mpeg_encode" to read
	while (sw==1)
	{
	  cin>>jpegorpng;
		switch (jpegorpng)

		{
		case 1: type="jpegtopnm *"; sw=0; 
			break;
		case 2: type="pngtopnm *"; sw=0; 
			break;
		case 3: type="giftopnm *"; sw=0; 
			break;
		case 4: type="*"; sw=0; 
			break;
                case 5: type="pstopnm *"; sw=0;
                        break;
		default: cout<<"You did not enter a valid number, re-enter selection "<<endl;
			break;
		}
	}
	 //cout<<type<<endl;
	cout<<"You can enter pictures into this program in two different ways"<<endl<<"Choose from the following choices? Enter 1 or 2"<<endl<<endl;
	cout<<"1. Enter the file individually i.e. cat.jpg,dog.jpg,bird.jpg"<<endl;
		cout<<"2. Enter the file name in a string i.e. cat*.jpg [1-50] were the * indicates a spot to change a number i.e. cat1.jpg,cat2.jpg,cat3.jpg...,cat50.jpg"<<endl<<endl; 	
	while (sw2==1)
	{
		cin>>how_to_enter_file;
		switch (how_to_enter_file)
		{
		case 1: sw2=0;
			break;
		case 2: sw2=0;
			break;
		default: cout<<"You did not enter a valid number, re-enter selection "<<endl;
			break;
		}	
	}

	
	//everything from "Variables" will be written to a file were the program "mpeg_encode" then creates the movie 
	//Variables.open("temporary_data");
	Variables<<"OUTPUT		"<<savename<<endl<<endl;
	Variables<<"INPUT_DIR	"<<directory;
	Variables<<endl<<endl<<"PATTERN		IBBPBBPBBPBBPBB"<<endl<<endl;
	Variables<<"PIXEL		FULL"<<endl<<endl;
	Variables<<"RANGE		10"<<endl<<endl;
	Variables<<"PSEARCH_ALG	LOGARITHMIC"<<endl<<endl;
	Variables<<"BSEARCH_ALG	CROSS2"<<endl<<endl;
	Variables<<"IQSCALE		8"<<endl<<endl;
	Variables<<"PQSCALE		10"<<endl<<endl;
	Variables<<"BQSCALE		25"<<endl<<endl;
	Variables<<"REFERENCE_FRAME	DECODED"<<endl<<endl;
	Variables<<"GOP_SIZE	30"<<endl<<endl;
	Variables<<"SLICES_PER_FRAME	1"<<endl<<endl;
	Variables<<"BASE_FILE_FORMAT	PNM"<<endl<<endl;;
	Variables<<"INPUT_CONVERT	"<<type;
	Variables<<endl<<endl;
	Variables<<"INPUT"<<endl;
	strcat(directory,"/");
	if (how_to_enter_file==1)
	{
	 cout<<"Enter the file names in order of how you want then to appear in the movie."<<endl<<"Enter "<<"*"<<" when you are finished entering the file.  Hit return between each file"<<endl;  
		while(strcmp(file_name,"*")!=0)	
		{
			//strcpy(temp,directory);
			cin>>file_name;	
			for(i=1; i<=30; i=i+frames_per_second)// this is were the frame rate is figured out,since "mpeg_encode" had a default of 30 frames per second, a user can select what ever frame rate they would like and it will create multiples of the same frame to give the user the desired frame rate
			{
				if(strcmp(file_name, "*")!=0) // keeps loop going unitl the user is done entering the files  
				{
					strcpy(temp,directory);  //resets if/else statment so that "temp" doesn't grow bigger with each set 
					if(stat(strcat(temp,file_name),&info)==0)	//adds the file name to the end of the directory for the computer to read
					{
						Variables<<file_name<<endl; 
					}
					else
					{
						cout<<"You did not enter a valid file name, re-enter a different file now"<<endl;
						break;
					}
				}
			}
		}
	}
	if (how_to_enter_file==2)  
	{
	  cout<<"Enter the name before the number i.e 'cat', if there is nothing"<<endl; 
	  cout<<"before or after the numbers just enter"<<"*" <<"where it says:"<<endl; 
	  cout<<"'Beginning part of files' and 'Ending part of files ' , then"<<endl;
	  cout<<"put in the first number in the list, then the last number in the list,"<<endl; 
	  cout<<"the step for the numbers i.e 1 for each, 2 for every-other and so on, "<<endl;
	  cout<<"and then finally the ending i.e. '.jpg'.  If there are zeros before the number"<<endl;
	  cout<< "i.e. cat01.jpg, enter the the name like cat0."<<endl;
		while (sw4==1)
		{	
			cout<<"Beginning part of files ";
			cin>>beginning_name;
			cout<<"First number in list ";
			cin>>first_number;
			cout<<"Last number ";
			cin>>last_number;
			cout<<"Step number ";
			cin>>number_step;
			cout<<"Ending part of files ";
			cin>>ending; 
			for (j=first_number; j<=last_number; j=j+number_step) //sets the code to caculate a strand of files so the user doesn't have to put in all of it by hand, only works if the file has numbers in them
			{
				for(i=1; i<=30; i=i+frames_per_second) //set the frame rate
	  			{
					if (strcmp(beginning_name, "*")==0)  
					{
						if(strcmp(ending, "*")==0) //this section is if the the file only contains a list of numbers with no begining or end 
						{
							sprintf(k,"%d",j); //turns a int "j" into a char "k" 
							strcpy(temp,directory); //resets temp for each cycle
							if(stat(strcat(temp,k),&info)==0)
							{
								Variables<<j<<endl;sw4=0;
							}
							else
							{
								cout<<temp;
								cout<<" Is not a valid file, re-enter a different file now"<<endl;
								break;
							}
						}
						else	//this section is for files who have no beginning but an ending after the numbers
						{	
							strcpy(temp,directory);
							sprintf(k,"%d",j);
							if(stat(strcat(strcat(temp,k),ending),&info)==0)
							{
								Variables<<j<<ending<<endl;sw4=0;
							}
							else
							{	
								cout<<temp;
								cout<<" Is not a valid file, re-enter a different file now"<<endl;
								break;
							}	
						}
					}
					else if (strcmp(ending, "*")==0) //this section is if the is a begining but not ending 
					{	
						
						sprintf(k,"%d",j);
						strcpy(temp,directory);
						strcpy(l,k);	//resets l to k for each cycle 
						strcpy(beginning_name2,beginning_name); //resets beginning_name2 for each cycle
						if(stat(strcat(temp,strcat(beginning_name2,l)),&info)==0)
						{
							Variables<<beginning_name<<j<<endl;sw4=0;
						}
						else
						{
							cout<<temp;
							cout<<" Is not a valid file, re-enter a different file now"<<endl;
							break;
						}
					}
					else	//this section if for a begining and a ending for each file in the list
					{
						
						sprintf(k,"%d",j);	//temp, l, begining_name2, and ending2 were added varibables so that "temp" would change for each option but not pile up with the old info from each loop.  i.e.temp1, temp12, temp123,temp1234, it will stay temp1, temp2, temp3 temp4.  
						strcpy(temp,directory);
						strcpy(l,k);
						strcpy(beginning_name2,beginning_name);
						strcpy(ending2,ending);
						if(stat(strcat(temp,strcat(beginning_name2,strcat(l,ending2))),&info)==0)
						{
							Variables<<beginning_name<<j<<ending<<endl;sw4=0;
						}
						else
						{	cout<<temp;
							cout<<" Is not a valid file, re-enter a different file now"<<endl;
							break;
						}
					}
				}
			}	
		}	
	} 
	Variables<<"END_INPUT"<<endl<<endl;
	Variables.close();
	system("/usr/people/faculty/physics/jcrumley/www-docs/370/magnetopause/mpeg_encode -no_frame_summary /tmp/mpeg_encode_run"); 
        // since this program creates a data file that mpeg_encode then runs, i
        // this is were the program does this. 
       
        // Now remove the tmp file
        system("/bin/rm /tmp/mpeg_encode_run");
return 0;
}	

