// For testing only

char PROGNAME[80]="readid\0";
char LASTCHANGED[80]="19 April 2015\0";
#define VERSION		"1.1"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <crypt.h>
#include "xlogin.h"
#include "key.h"

char usernamebuf[MAXUSERNAMESIZE*2];
char passwordbuf[MAXPASSWORDSIZE*2];
char filenamebuf[FILEPATHLENGTH];

void remove_newline(char *s) {
	if (!s) return;
	int len = strlen(s);
	if (!len) return;
	if (s[len-1]=='\n') s[len-1]=0;
}

int main (int argc, char *argv[])
{
	char *filename;
	int i;
	// process args
	for (i=1; i<argc; i++) {
		// help
		if (strcmp(argv[i],"-h")==0 ||
		    strcmp(argv[i],"-help")==0 ||
		    strcmp(argv[i],"--help")==0) 
		{
			fprintf(stderr,"%s: <option list>\n",PROGNAME);
			fprintf(stderr,"-file\t\t Identity file to read, default is "
			               DEFAULT_CONFIG_PATH "/identity/username.id\n");
			exit(0);
		}
		// filename
		else if (strcmp(argv[i],"-f")==0 || 
		         strcmp(argv[i],"-file")==0 || 
		         strcmp(argv[i],"-filename")==0)
		{
			i++; filename=argv[i];
		}		
	}

	// if filename not given, use default
	if (!filename) {
		filename = DEFAULT_CONFIG_PATH "/identity/username.id";
	}

	FILE *idfile;
	idfile=fopen(filename,"r");
	if (idfile==NULL)
	{
		perror("Opening  file %s FAILED\n");
		exit(1);
	}

	int x;
	char buffer[FILESIZE];
	fread(&buffer,1,FILESIZE,idfile);
	fclose(idfile);

	weakdecrypt(usernamebuf,passwordbuf,filename);
	fprintf(stderr, "username=%s  password=%s  filename=%s  key=%s\n",
	        usernamebuf,passwordbuf,filename,XLOGINKEY);
}

