#include <iostream.h>
#include "myincludes.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "krypto.h"

//_------------------------------------------_
void printHelp ()
	{
	 fprintf (stderr, "krypto von SiSch\n(Compiled at: ");
	 fprintf (stderr, "%s, %s)", __DATE__, __TIME__);
	 fprintf (stderr, "\n\nuse: krypto [-d || -e] -k <key> -s -o <outputfile>  <filename>\nkrypto will encrypt the file <filename>, and then ");
	 fprintf (stderr, "write it to <outputfile>.\n	-d decrypt the file\n	-e encrypt the file(default)\n");
	 fprintf (stderr, "	-k key (password) for encryption or decryption - min. 6 charakters\n");
	 fprintf (stderr, "	-s slim encryption (not very good - but small files:)\n");
	 fprintf (stderr, "	-o outputfile (only)for encryption (Krypto will use <filename> + \".kr\"\n	   if you don't use this option)");
	 fprintf (stderr, "\n\neMail: general_Ossig@web.de\n");
	 exit(true);
	}
//_-------------------------------------------_

int main (int argc,char *argv[])
 {
  char *bef1;
//  try {
	enum AKTION {ENCRYPT, DECRYPT};
	bool aktion = ENCRYPT;
	char *theArg, *theKey="";
        krypto myKrypto;
//Argumente prfen
	if (argc==1)
		printHelp();
	bool noHelp;
	for (int i=1;i<argc;i++)
	     {
	     theArg=argv[i];
		if (theArg[0]=='-')
		{
		 noHelp=false;
		 for (int ia=1;ia<len(theArg);ia++)
		 {//for ia anf
		 if (tolower(theArg[ia]) == 'w')
		 	{fprintf (stderr, "ich bin krypto von SiSch\n");/*->>*/return 109;}
		 if (tolower(theArg[ia]) == 'e')
		     noHelp==true;
		 if (tolower(theArg[ia]) == 'd')
		    {aktion=DECRYPT;noHelp=true;}
		 if (tolower(theArg[ia]) == 's')
		     {myKrypto.slim=true;noHelp=true;}
		 if (tolower(theArg[ia])=='k')
		     {
		      theKey=argv[i+1];
		      noHelp=true;
		      if (argc<i+2)
		          {fprintf (stderr, "TOO FEW ARGUMENTS...\n\n"); printHelp ();}
		      if (len(theKey)<6)
		          {fprintf (stderr, "THE KEY MUST BE 6 CHARAKTERS LONG, NOT ONLY %d...\n\n", len(theKey)); printHelp ();}
		      }
		 if (tolower(theArg[ia])=='o')
		     {myKrypto.OutPutFile=argv[i+1];noHelp=true;}
		if (!noHelp && tolower(theArg[ia])!='e')
		     {fprintf(stderr, "UNGLTIGES ARGUMENT: %s\n\n", argv[i]);printHelp();}
		}//for ia end
		}
	     }
        if (theKey=="")
	    printHelp();

	fprintf (stderr, "krypto sais Guten Tag\n");

	myKrypto.FileName=argv[argc-1];
	if (myKrypto.OutPutFile[0]==0)
	    {sprintf(bef1, "%s.kr", myKrypto.FileName);myKrypto.OutPutFile=bef1;}
	if (aktion == ENCRYPT)
	    {myKrypto.encrypt(theKey);
	     //find();
	     //sprintf(bef1, "rm %s", myKrypto.FileName);
	     //system (bef1);
            }
	else

	    myKrypto.decrypt(theKey);
	return true;
//  }
//  catch (...){fprintf(stderr, "\nUnerwarteter Fehler... Programm wird beendet\n");abort();}


 }


