objref frec
strdef cmd, dircmd, direx, comper, version, vercomment, vercomment2, mypath, userstr, machname, machnick, outfile, edate, comver
strdef verleft, verright
objref  strobj
strobj = new StringFunctions()

proc typeout() {local i, myline, rn, rank, gid, srcid localobj tgt, f, cell, f2c // Write out the cell types & gid ranges
	if (pc.id == 0) { 				// Write header to file 1 time only
		rn = -1
		sprint(dircmd, "ls ../results/%s", RunName)
		{system(dircmd, direx)}
		{sprint(comper, "ls: cannot access ../results/%s: No such file or directory", RunName)}
		while (strcmp(direx,comper)!=0 && strcmp(direx,"")!=0 ) {
			rn = rn + 1
			sprint(dircmd, "ls ../results/%s_%02.0f", RunName, rn)
			{system(dircmd, direx)}
			{sprint(comper, "ls: cannot access ../results/%s_%02.0f: No such file or directory", RunName, rn)}
		}
		if (rn>-1) {
			sprint(RunName, "%s_%02.0f", RunName, rn)
		}
		printf(" so I will create it now\n")
		sprint(dircmd, "mkdir ../results/%s", RunName)
		{system(dircmd, direx)}
		//sprint(dircmd, "cp ../setupfiles/lowindex.hoc ../results/%s", RunName)
		//{system(dircmd, direx)}

		//sprint(dircmd, "cp ../stimulation/sintrainparams.hoc ../results/%s", RunName)
		//{system(dircmd, direx)}

		//sprint(dircmd, "cp ../cells/allsyns.dat ../results/%s", RunName)
		//{system(dircmd, direx)}

		// check for hg
			sprint(dircmd, "hg parent --template '{rev}: {desc}\n'")
			{system(dircmd, direx)}
		
		// check string for "no hg in"
		index = strobj.len(direx)

		// if the check matches, then use backup files
		if (index<1) {
			sprint(dircmd, "cp ../hg_status.out  ../results/%s/hg_status.out", RunName)
			{system(dircmd, direx)}
			
			sprint(dircmd, "cp ../hg_diff.out  ../results/%s/hg_diff.out", RunName)
			{system(dircmd, direx)}
				
			f2c = new File()		
			f2c.ropen("../vercomment.txt")		// Open the celltype
			myline=f2c.gets(vercomment)		
			strobj.left(vercomment, myline-1)			
			f2c.close
			
			f2c.ropen("../version.txt")		// Open the celltype
			f2c.scanstr(version)			
			f2c.close			

		} else { // else use built in hg
			sprint(dircmd, "hg status")
			{system(dircmd, direx)}
			if (strcmp(direx,"")>0) {
				comver = "Yes"
				sprint(dircmd, "hg status > ../results/%s/hg_status.out", RunName)
				{system(dircmd, direx)}
				
				sprint(dircmd, "hg diff > ../results/%s/hg_diff.out", RunName)
				{system(dircmd, direx)}
			} else {
				comver = "No"
			}
			sprint(dircmd, "hg parent --template '{rev}: {desc}\n'")
			{system(dircmd, direx)}
			
			if (strobj.len(direx)<2) {
				vercomment="unknown"
			} else {
				strobj.left(direx, strobj.len(direx)-1) // hg parent rev/desc
				vercomment=direx
			}
			
			i = strobj.substr(vercomment, "'")
			while (i>0) {
				verleft = vercomment
				strobj.left(verleft, i)

				verright = vercomment
				strobj.right(verright, i+1)
				
				sprint(vercomment,"%s''%s", verleft, verright)
				
				i = strobj.substr(verright, "'")
				if (i>0) {
					i = i + strobj.len(verleft)+2
				}
			}

			sprint(dircmd, "hg parent --template '{node}\n'")
			{system(dircmd, direx)}
			if (strobj.len(direx)<2) {
				version="unknown"
			} else {
				strobj.left(direx, strobj.len(direx)-1) // hg parent node
				version=direx
			}
		}

		system("cd ..;pwd",mypath)
		if (strobj.len(mypath)<2) {
			mypath="unknown"
		} else {
			strobj.left(mypath, strobj.len(mypath)-1) // path
		}
		system("whoami", userstr)
		if (strobj.len(userstr)<2) {
			userstr="unknown"
		} else {
			strobj.left(userstr, strobj.len(userstr)-1) // whoami
		}
		{system("hostname", machname)}
		if (strobj.len(machname)<2) {
			machname="unknown"
		} else {
			strobj.left(machname, strobj.len(machname)-1) // hostname
		}
		machnick =  machname// nickname for host machine, usually second term in address		
		i = strobj.substr(machnick, ".")
		if (i>0) {
			strobj.right(machnick, i+1)
			i = strobj.substr(machnick, ".")
			if (i>0) {
				strobj.left(machnick, i)
			}
		}

		{system("date \"+%d-%b-%Y %H:%M:%S\"", edate)}
		if (strobj.len(edate)<2) {
		edate="unknown"
		} else {
			strobj.left(edate, strobj.len(edate)-1) // date
		}

		sprint(outfile, "../results/%s/runreceipt.txt", RunName)
		frec = new File(outfile)
		frec.wopen()
		frec.printf("NumProcessors = %g;\n", pc.nhost)
		frec.printf("ExecutionDate = '%s';\n", edate)
		frec.printf("ExecutedBy = '%s';\n", userstr)
		if (strcmp(machnick,"local")!=0) {
			frec.printf("Machine = '%s';\n", machnick)
		}
		frec.printf("MachineFull = '%s';\n", machname)
		frec.printf("ModelVerComment='%s';\n", vercomment)
		frec.printf("ModelVersion='%s';\n", version)
		frec.printf("ModelDirectory ='%s';\n", mypath)
		frec.printf("NEURONVersion ='%s';\n", nrnversion(2)) // version number and mercurial changeset
		frec.printf("WorkDirChg ='%s';\n", comver) // whether this job was run using a model working directory that
														// had been changed since the last version update													
														// If no, the run can therefore be easily reproduced(No)
														// If yes, we won't have any record of the changes and that
														// will limit our ability to reproduce the run unless
														// we record the changes, which we do in that case (Yes)
		printmyvars()
		frec.close()
	}

	pc.barrier()					// Wait for all ranks to get to this point
	sprint(cmd,"../results/%s/celltype.dat", RunName)
	f = new File(cmd)
	if (pc.id == 0) { 				// Write header to file 1 time only
		f.wopen()
		f.printf("celltype\ttypeIndex\trangeStart\trangeEnd\n")
		for i=0, numCellTypes-1 {
			f.printf("%s\t%s\t%d\t%d\t%d\n", cellType[i].cellType_string, cellType[i].technicalType, i, cellType[i].cellStartGid, cellType[i].cellEndGid)
		}
		f.close()
	}
}
/**/strdef thisline, getval, result, paramstr, testline, valstr
/**/proc printmyvars() {local strlen localobj pfobj

	pfobj = new File()
	pfobj.ropen("../setupfiles/parameters.hoc")
	while (pfobj.gets(thisline)>-1) {
		testline=thisline
		strobj.left(testline, 1) // testline
		if (strcmp(testline,"d")==0) {
			result=thisline
			strobj.right(result, 13)
			strlen = strobj.head(result, "\",", paramstr)
			{sprint(getval, "printnow(%s, paramstr, %g)", paramstr, strobj.len(paramstr))}
			{execute1(getval)}
		}
	}
	pfobj.close
}
/**/proc printnow() {
		if (argtype(1)==0) {
			if ($3>0) {
				//sprint(getval, "paramval = %s", $s2) // Note that paramval cannot be a local variable
				//{execute1(getval)}
				{frec.printf("%s = %g;\n", paramstr, $1)}
			}
		} else {
			if (argtype(1)==2) {
				if ($3>0) {
					//sprint(getval, "paramval = %s", $s2) // Note that paramval cannot be a local variable
					//{execute1(getval)}
					{frec.printf("%s = '%s';\n", paramstr, $s1)}
				}
			} 
		}
	}
typeout()
{pc.broadcast(RunName, 0)}