xpanel("")
xbutton("nothing", "print 1")
xpanel(5000, 5000)

show_winio(0)

{load_file("stdlib.hoc", "String")}

objref rwl_, rwf_, sf_, tobj, box
objref ldfile
strdef tstr, rwdname, path

sprint(path, "%s", getcwd())
ldfile = new File()

sf_ = new StringFunctions()
if (unix_mac_pc() == 1) {
	rwdname = "$(HOME)/.NRNWorkingDirs"
}else{
	rwdname = "$(NEURONHOME)/RecentWorkingDirs"
}

proc filemenu() {
	box = new VBox()
	box.intercept(1)
	xpanel("")
	xmenu("Recent directories", "recent_working_dirs()")
	xmenu()
	xpanel()
	box.intercept(0)
}

proc doit() {local b
	filemenu()
	while (1) {
		index = -1
		b = box.dialog("Choose directory (containing .mod files) for creating nrnmech.dll", "Choose directory", "Quit")
		if (b == 0) { break }
		if (index != -1) {
			chdir(rwl_.object(index).s)
		}
		change_working_dir()
	}
	quit()
}

proc change_working_dir() {
	ldfile.chooser("d", "Directory", "", "Make nrnmech.dll", "Cancel", getcwd())
	if (ldfile.chooser()) {
		read_recent_working_dirs()
		if (change_working_dir1(ldfile.dir)) {
			exec()
		}
	}
}

func change_working_dir1() {
	if(chdir($s1) != 0) {
		sprint(tstr, "No such directory: %s", $s1)
		continue_dialog(tstr)
		return 0
	}
	sprint(path, "%s", getcwd())
	write_recent_working_dirs()
	return 1
}

proc read_recent_working_dirs() {local i
	rwf_ = new File()
	rwl_ = new List()
	if (rwf_.ropen(rwdname)) {
		while (!rwf_.eof()) {
			rwf_.gets(tstr)
			sf_.left(tstr, sf_.len(tstr) - 1)
			tobj = new String(tstr)
			rwl_.append(tobj)
		}
		rwf_.close()
	}
}

proc write_recent_working_dirs() {local i
	rwf_ = new File()
	if (rwf_.wopen(rwdname)) {
		rwf_.printf("%s\n", path)
		for i=0, rwl_.count-1 {
			if (strcmp(path, rwl_.object(i).s) != 0) {
				rwf_.printf("%s\n", rwl_.object(i).s)
				if (i > 10) break
			}
		}
		rwf_.close()
	}
}

proc recent_working_dirs() {local i
	read_recent_working_dirs()
	for i=0, rwl_.count-1 {
		//sprint(tstr, "change_working_dir2(%d)", i)
		sprint(tstr, "index=%d box.unmap(1)", i)
		xbutton(rwl_.object(i).s, tstr)
	}
}

proc exec() {localobj s1, s2
	chdir(path)
    if (sf_.substr(nrnversion(8), "mingw") > 0) {
	s1 = new String()
	s2 = new String()
	sscanf(neuronhome(), "%[^:]:%s", s1.s, s2.s)
	sprint(s1.s, "/cygdrive/%s%s", s1.s, s2.s)
	sprint(tstr, "%s/mingw/usr/bin/sh %s/lib/mknrndll.sh %s", neuronhome(), neuronhome(), s1.s)
	print tstr
	system(tstr)
	quit()
    }else{
	sprint(tstr, "%s\\bin\\mintty -c %s/lib/minttyrc %s/bin/bash --rcfile %s/lib/nrnstart.bsh %s/lib/mknrndll.sh %s", neuronhome(1), neuronhome(), neuronhome(), neuronhome(), neuronhome(), neuronhome())
	print tstr
	WinExec(tstr)
	quit()
    }
}

doit()

