*!version 0.2.1 :: 03/30/12 11:31 AM

//  Program     : graphsto
//  Author      : Joseph D. Wolfe  & J. Scott Long
//  Description : captures graph name in global graphout can use
//  Requirements: >=Stata 10 (currently untested on previous versions)

capture clear  programs
program define graphsto

//  #2
//  set macros and defaults

local caller : di _caller()
local everything `"`0'"'

if `"`1'"' == "clear"   local sendtoclear = 1
if `"`1'"' == "drop"    local sendtoclear = 1
if `"`1'"' == "dir"     local sendtodir   = 1
if `"`1'"' == ",clear"  local sendtoclear = 1
if `"`1'"' == ",drop"   local sendtoclear = 1
if `"`1'"' == ",dir"    local sendtodir   = 1
if `"`1'"' == ","  & (`"`2'"'=="clear" | `"`2'"'=="drop") {
	     local sendtoclear = 1
}
if `"`1'"' == ","  & (`"`2'"'=="dir") {
	     local sendtodir = 1
}

if "`sendtodir'"==""   local sendtodir = 0
if "`sendtoclear'"=="" local sendtoclear = 0


if  regexm(`"`1'"',"^:[a-zA-Z]+") |    ///
    regexm(`"`1'"',":")           local sendtoparse = 1
	                         else local sendtoparse = 0

//  #3
//  errors

if `sendtoparse'==0 & regexm(`"`3'"',":") {
	di as error ///
	"invalid syntax: see graphsto help file for more information"
	exit 198
}

if (`sendtoclear' == 0 & `sendtodir' == 0 & `sendtoparse'==0) {
	di as error ///
	"Invalid syntax: see graphsto help file for more information"
	exit 198
}

//  #4
//  parse, export, and get graph names

if `sendtoparse' {

	gettoken trash cmdline : everything, parse(":")

	local cmdword1 : word 1 of `cmdline'
	if "`cmdword1'"!="gr_export" local cmdword2 : word 2 of `cmdline'
	loc grphexport "`cmdword1' `cmdword2'"

	local grph_and_opts : subinstr local cmdline "`cmdword1'" ""
	if "`cmdword2'"!="" local grph_and_opts : subinstr local grph_and_opts "`cmdword2'" ""
	gettoken grph export_options: grph_and_opts, parse(",")

	* check if graph export command is correct
	if regexm(`"`grphexport'"', "graph export") |       ///
       regexm(`"`grphexport'"', "gr_export") local stop = 0
	                                    else local stop = 1
	if `stop' {
		di as error ///
		"graphsto only words with graph export"
		exit 198
	}

	* export graph
	version `caller':`grphexport' `"`macval(grph)'"' `macval(export_options)'

	* parse file name from path
	_cllct_getFileNameAndPath, fileinfo(`"`grph'"')
	local grph      `"`s(file_name)'"'
	di "Graph Name: `grph'"

	* collect graph names
	local  graphstored_graphs      `"$graphsto_stored_graphs"'
	global graphsto_stored_graphs  `"`graphstored_graphs' `grph'"'
}

//  #5
//  options

if `sendtoclear' {
	capture macro drop graphsto_stored_graphs
	di as text "(note: graphs removed)"
}

if `sendtodir' {
	foreach grph of global graphsto_stored_graphs {
		di as text `"`grph'"'
	}
	if "$graphsto_stored_graphs" == "" {
		di as text "(note: no graphs in directory)"
	}
}

end // graphsto
exit

* Version History
* version 0.0.1  11/05/10
	* basic functions & errors
* version 0.0.2  11/05/10
	* changed from prefix to wrapper for graph export
* version 0.0.3  11/05/10
	* went back to prefix format
	* renamed to graphsto
* version 0.1.0  02/05/11 9:32 AM
	* allow grahsto name: graph export
	* errors
* version 0.1.1  02/06/11 3:20 AM
	*last version was getting too complicated came back to 0.0.3
	*added errors and updated parsing
* version 0.1.2  03/01/11 12:38 PM
	*adjust so that users can specify path to file
* version 0.2.0  03/05/11 11:34 AM
	*new version for web
*version 0.2.0 :: 03/05/11 11:34 AM