*! Dan Blanchette 1.1 dan.blanchette@duke.edu 08Feb2005 
*! Center of Entrepreneurship and Innovation Duke University's Fuqua School of Business
** research computing, unc-ch
* Stata fixed Linux problem of not being able to edit a file in a directory starting with "~"
* Added "Caution" note when requesting to edit a Stata ado file.
** Dan Blanchette 1.0.3 06 Nov 2003 made it work in Linux
*! NJC 1.0.2 31 Dec 2002 
** CFB 1.0.1 26Dec2002 cloned from adotype
* NJC 1.2.1 1 December 1999
** NJC 1.2.2 6 December 1999
* NJC 1.2.3 14 December 1999 
** AJM 1.3 5 August 2009   (Eliminated file size check for versions 11 and greater.)	

program def adoedit, rclass
        version 8.0


	if "`1'" == "" | "`2'" != "" { 
		di as err "incorrect syntax"
		exit 198 
	}
	args cmd 

	 /* ends with .ado */
	if substr(`"`cmd'"',length(`"`cmd'"')-3,length(`"`cmd'"'))==".ado"  {   
		local filen `"`cmd'"'
	} 
	else {
           	local filen `"`cmd'.ado"'
	} 

	findfile `filen'

	// here will exit 601 if `cmd' not found 
	
	local file `"`r(fn)'"' 

        local tfile : subinstr local file "\" "/" , all

        if index(`"`tfile'"',"/ado/base")  |  index(`"`tfile'"',"/ado/updates")  {
          di " "
          di as err "Caution, you are requesting to edit an ado file provided by Stata."
          di as err "If this is really what you want, consider first copying the file to {input}`: sysdir PERSONAL' {error}."
          di " "
          more
          di " "
        }

	capture hexdump `"`file'"', analyze
	local size = r(filesize)
	
	* Eliminates file size check for versions 11 and greater.		
	if c(stata_version) < 11 {

		if `size' < 32000 & `size' > 0 {
			doedit `"`file'"'
			discard
		return local adofile `"`file'"'
		exit 0 
			}
		else {
				di as txt _n "Sorry, files larger than 32,000 bytes cannot be do-edited."
					di as txt "You must use another text editor for this file." 
					error 603
		} 
	}
			
	else if c(stata_version) >= 11 {
			doedit `"`file'"'
			discard
		return local adofile `"`file'"'
		exit 0 
		}

end