*! SCT 0.0.1 28 December 2006
version 9

/* Syntax:

-cdp-
When the cmd has been issued alone, with no arguments, it will try to find the current default working dir.

-cdp dirname-
Will change the default working directory to dirname, and create a profile.do if necessary.

-cdp, clear-
Will remove any default working directory from profile.do
*/

* ToDo:
* 1) Help file
* 2) Better error handler

pr de cdp_beta
	syntax [anything(name=dir id="Path")], [CLEAR]
	cap local dir = `dir' // This removes the quotes

* If dir and clean are not empty, syntax error
	if ("`dir'"!="" & "`clear'"!="") error 100

* If -dir- is not empty, test whether the path is valid.
	cap confirm new file "`dir'/7n4aw3ly" // Some random string
	if _rc!=0 & "`dir'"!="" {
		di as error `"The directory does not exist!"'
		di as error `"Wrong path: {it:`dir'}"'
		error 1337
	}

* Profile Not Found
	local filename = "`c(sysdir_stata)'profile.do"
	cap confirm file "`filename'"
	if _rc!=0 { // 404 error: Profile Not Found!
		if ("`dir'"=="") {
			di as text "(Profile.do does not exist)"
		}
		else {
			file open newprofile using "`filename'", write text
			file write newprofile `"cd "`dir'""' _n 
			file write newprofile `"exit"' _n 
			noi di as text `"(`filename' created)"'
			noi di as text `"{it: cd "`dir'"}  ==>  Profile.do"'
			file close newprofile
		}
		exit
	}

* We found the profile, now let's search it
	if ("`dir'`clear'"=="") { // We will not write anything, only get the default working dir
		file open oldprofile using "`filename'", read text
	}
	else { // For these cases we will use tempfiles
		tempfile temp1
		tempfile temp2
		copy "`filename'" "`temp1'"
		file open oldprofile using "`temp1'", read text 
		file open newprofile using "`temp2'", write text
	}
	file read oldprofile line
	local theend 0
	while r(eof)==0 { 
		local cmd : word 1 of `line'
		if ("`cmd'"=="exit" | `theend') {
			if ("`dir'`clear'"=="") continue, break
			if ("`dir'"!="" & !`theend') {
				file write newprofile `"cd "`dir'""' _n 
				noi di as text `"{it: cd "`dir'"}  ==>  Profile.do"'
			}
			file write newprofile `"`line'"' _n
			local theend 1
		}
		else if ("`cmd'"=="cd" & "`dir'`clear'"=="") {
			local cdline = `"`line'"'
		}
		else if ("`cmd'"!="cd" & "`dir'`clear'"!="") {
			file write newprofile `"`line'"' _n
		}
		file read oldprofile line
	}

	if ("`dir'`clear'"=="") {
		if `"`cdline'"'=="" {
			di as text "(No default working folder specified on profile.do)"
		}
		else {
			di as text `"Default working folder: "`: word 2 of `cdline''" "'
		}
	}

	if ("`dir'"=="" & "`clear'"!="") di as text "(All -cd- commands cleared on profile.do)"

	file close oldprofile
	if ("`dir'`clear'"!="") {
		file close newprofile
		nobreak copy "`temp2'" "`filename'", replace
	}

/*	cap noi di as error "TEMP 1"
	cap noi type "`temp1'"
	cap noi di as error "TEMP 2"
	cap noi type "`temp2'"
	cap noi di as error "FINAL"
	cap noi type "`filename'" */
end
