#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cstdlib>
 
#define MAX_MILLISECONDS_BETWEEN_PRINTS 250
#define MAX_CHARS_TO_READ 10
 
void simulatePause(){
	int randomDuration = rand()%MAX_MILLISECONDS_BETWEEN_PRINTS+1;
	usleep(1000*randomDuration);
}
 
void printSomeCharsFromFile(std::ifstream& fileStream){
 
	std::cout.setf(std::ios::unitbuf);
 
	std::string chars;
	std::string::size_type lengthOfLine = chars.size();
	int pos = 0;
 
	if(fileStream){				
		getline(fileStream, chars);
		lengthOfLine = chars.size();
		pos = 0;
 
		while(pos < lengthOfLine){
			simulatePause();
			int randomLength = rand()%lengthOfLine+1;
			randomLength = (randomLength > MAX_CHARS_TO_READ) ?  MAX_CHARS_TO_READ : randomLength;
			std::cout << chars.substr(pos,randomLength);
			pos += randomLength;
		}
 
		std::cout << "\n";
	}
}
 
void autoPrint(std::ifstream& fileStream){
	while(fileStream){
		printSomeCharsFromFile(fileStream);
	}
}
 
int main(int argc, char** argv){
 
	//Instructions are for idiots.
	if(argc < 1){
		std::cout << "Usage: hacker [filename]" << "\n";
		std::cout << "There's no man page, don't bother." << "\n";
		return 1;
	}
 
	std::string file_path(argv[1] ? argv[1] : "");
	std::string path = file_path;
	std::ifstream inputStream;
 
	if(file_path == ""){		
		std::cout << "-----------------------------------" << "\n";
		std::cout << "-   Welcome to MovieHacker 1.0    -" << "\n";
		std::cout << "-----------------------------------" << "\n\n";
 
		std::cout << "Please enter a file to hack with." << "\n\n";
		std::cin >> file_path;
	}
 
	inputStream.open(file_path.c_str());
 
	if(!inputStream){
		std::cerr << "There's been a fatal error. That file doesn't exist.\n";
		std::cerr << "First you will be baked. Then there will be cake." << "\n";
		return 3;
	}
 
	for(int i=0;i<1000;i++){
		std::cout << "\n";
	}
 
	autoPrint(inputStream);
	inputStream.close();
	std::cout << "\n\n------ ACCESS GRANTED --------" << "\n";
 
	return 0;
}