*! version 0.0.2 2016-06-20 | scott long | rrus
* quick and dirty; did not test url
* looks local first, then posted.
* TODO allow web first

capture program drop rrex
program define rrex

	* online data location
	local data_url "http://www.indiana.edu/~jslsoc/stata/rrus_data/"

	/*
	SPECIFICATIONS OF DIFFERENT MODELS
	details of specific models/examples should be handled here
	[cmd]_data -> 	name of dataset to be used
	[cmd]_model -> 	model to be estimated
	[cmd]_pre# -> 	any commands to be executed between loading data
					and estimating model
	[cmd]_post# -> 	any commands to be executed between loading data
					and estimating model

	post-estimation commands can be included -- see below
		for post-estimation commands, options will be executed with first cmd
		after logit

	*/

	* EXECUTION: no code below should be specific to a particular model

	* parameters that can be tweaked

	local look_first "local"
	local look_first "posted"

local posteddir "d:\Dropbox\Active\RRUS\PostedData\"

	* parse input

    syntax anything [, Web local user copy *]
    tokenize "`anything'"
	local arg "`1'"

	local done "no"

	* options about locations
	if "`web'" != "" {
		local look_first "web"
	}
	if "`user'" != "" | "`local'" != "" {
		local look_first "local"
	}

	* handle case where argument is a model

	if "``arg'_data'" != "" {

		* get data
		_rrexUse ``arg'_data', ///
		    posteddir(`posteddir') url(`data_url') lookfirst(`look_first') `copy'

		* execute any pre-commands
		local precommands_done "no"
		local pre_count = 1
		while "`precommands_done'" == "no" {
			if "``arg'_pre`pre_count''" == "" {
				local precommands_done "yes"
			}
			else {
				local cmdtext "``arg'_pre`pre_count''"
				di as txt ". `cmdtext'"
				`cmdtext'
                window push `cmdtext'
				local pre_count = `pre_count' + 1
			}
		}

		* estimate model
		local cmdtext "``arg'_model'"
		if "`options'" != "" & "``arg'_post'" != "yes" { // if user has specified options
			if strpos("`cmdtxt'", ",") == 0 { // no options in default model
				local cmdtext "`cmdtext', `options'"
			}
			else { // are options in default model, so no comma
				local cmdtext "`cmdtext' `options'"
			}
		}
		di as txt ". `cmdtext'"
		`cmdtext'
		di
        window push `cmdtext'
		local done "yes"

		* execute any post-commands
		local postcommands_done "no"
		local post_count = 1
		while "`postcommands_done'" == "no" {
			if "``arg'_post`post_count''" == "" {
				local postcommands_done "yes"
			}
			else {
				local cmdtext "``arg'_post`post_count''"
				if "`options'" != "" & "``arg'_post'" == "yes" & `post_count' == 1 {
					if strpos("`cmdtxt'", ",") == 0 { // no options in default model
						local cmdtext "`cmdtext', `options'"
					}
					else { // are options in default model, so no comma
						local cmdtext "`cmdtext' `options'"
					}
				}

				di as txt ". `cmdtext'"
				`cmdtext'
				di
                window push `cmdtext'
				local post_count = `post_count' + 1
			}
		}

	}

	* if not handled as command, try loading as dataset

	if "`done'" == "no" {
*		di as txt "(`arg' is not supported command; attempting to load as dataset)"
		_rrexUse `arg', ///
		    posteddir("`posteddir'") url("`data_url'") lookfirst(`look_first') `copy'
		local done "yes"
	}

end

capture program drop _rrexUse
program define _rrexUse

*	syntax namelist, url(string) lookfirst(string) [ copy ]
*    tokenize "`namelist'"

    syntax anything, posteddir(string) url(string) lookfirst(string) [ copy ]
    tokenize "`anything'"

	local data "`1'"
	local look_first "`lookfirst'"

// look at posted first


	if "`look_first'" == "posted" {

local posteddata = "`posteddir'" + "`data'"

capture sysuse `data'.dta, clear

		if _rc != 0 {
			capture use "`posteddata'", clear
			if _rc != 0 {
				di as err `"`posteddata' not found"'
				error 999
			}
			else {
				di _n as txt `". use "`posteddata'", clear"'
				di
                window push use "`posteddata'", clear
			}
		}
		else {
			di _n as txt `". sysuse "`data'.dta", clear"'
			di
            window push sysuse `where'`data'.dta, clear
		}

    }




	if "`look_first'" == "local" {

		capture sysuse `data'.dta, clear
		if _rc != 0 {
			capture use "`url'`data'", clear
			if _rc != 0 {
				di as err `"program cannot find file `data' locally or online"'
				error 999
			}
			else {
				di _n as txt `". use "`where'`data'.dta", clear"'
				di
                window push use `where'`data'.dta, clear
			}
		}
		else {
			di _n as txt `". sysuse "`data'.dta", clear"'
			di
            window push sysuse `where'`data'.dta, clear
		}

	}

//  look first online

	if "`look_first'" == "web" {

		capture use "`url'`data'", clear
		if _rc != 0 {
			 capture sysuse `data'.dta, clear
			 if _rc != 0 {
				  di as err `"program cannot find file `data' locally or online"'
				  error 999
			 }
			 else {
				 di _n as txt `". sysuse "`data'.dta", clear"'
                 window push sysuse `data'.dta, clear
			 }
		}
		else {
			di _n as txt `". use "`url'`data'.dta", clear"'
            window push use `url'`data'.dta, clear
		}

	}

//  copy to working directory

    if "`copy'"=="copy" {
        capture copy "`url'`data'.dta" `data'.dta
        di "note: file `data'.dta copied to working directory"
    }

end
exit
* version 4.0.8 2014-08-07 | long freese | copy fix
* version 4.0.7 2014-07-28 | long freese | no regjob3
* version 4.0.6 2014-07-01 | long freese | copy
* version 4.0.5 2014-06-28 | long freese | mgen
* version 4.0.4 2014-06-09 | long freese | window push
* version 4.0.3 2014-06-08 | long freese | additional examples
* version 4.0.1 2014-05-13 | long freese | full rewrite for spost13 release