*! v0.8.0 2017-09-11 Scott Long  - workingdir.pkg

// make/create cd* commands

capture program drop makecd
program define makecd
    version 13.0
    syntax namelist [ , ///
        data(string) /// location of data
        data2(string) /// 2nd data location
        Note(string) /// description
        AUTOrun(string) /// run this command
        Details /// display S_cd globals
        url(string) /// url
        user(string) /// anything you want
        NOSlash /// no ending slash on locations
        replace ///
    ]

    //  trap entering project as cdproject

    local name12 = substr("`namelist'",1,2)
    if ("`name12'"!="cd") local namelist "cd`namelist'"

    //  trap reserved names

    if "`namelist'"=="cddrop" | "`namelist'"=="cdlist" {
            di as error "`namelist' name is reserved; chose another name"
            exit
    }

    //  current working directory

    local path `"`c(pwd)'"'
    local path : subinstr local path "\" "/", all
    local lastchar = substr("`path'",strlen("`path'"),1)
    if ("`lastchar'"=="/" | "`noslash'"=="noslash") local wdcurrent "`path'"
    else local wdcurrent "`path'/"

    //  check PERSONAL

    local PERSONAL "`c(sysdir_personal)'"
    if "`PERSONAL'"=="" {
        di as error "no PERSONAL directory is set"
        exit
    }
    else { // does it exist?
        capture cd "`PERSONAL'"
        if _rc!=0 {
            display as error "PERSONAL directory `PERSONAL' does not exit"
            exit
        }
        qui cd "`wdcurrent'" // restore working directory
    }

    //  name of ado file

    local adoname `"`namelist'"'
    local adowithpath `"`PERSONAL'`adoname'.ado"'

    //  data()

    if "`data'"!="" {
        capture cd "`data'"
        if _rc==0 { // if path exist, get cleaner name
            local data `"`c(pwd)'"' // get clean name
            qui cd "`wdcurrent'" // restore working directory
        }
        local path `"`data'"'
        local path : subinstr local path "\" "/", all
        local lastchar = substr("`path'",strlen("`path'"),1)
        if ("`lastchar'"=="/" | "`noslash'"=="noslash") local data "`path'"
        else local data "`path'/"
    }

    //  data2()

    if "`data2'"!="" {
        capture cd "`data2'"
        if _rc==0 {
            local data2 `"`c(pwd)'"' // get clean name
            qui cd "`wdcurrent'" // restore working directory
        }
        local path `"`data2'"'
        local path : subinstr local path "\" "/", all
        local lastchar = substr("`path'",strlen("`path'"),1)
        if ("`lastchar'"=="/" | "`noslash'"=="noslash") local data "`path'"
        else local data2 "`path'/"
    }

    //  url

    if "`url'"!="" {
        qui webuse query
        local urlcurrent `"`r(prefix)'"' // current url to restore later

        qui capture webuse set "`url'"
        if _rc!=0 {
            display as error "invalid URL `url'"
            exit
        }
        else local urlpath `"`r(prefix)'"' // returned by webuse

        qui webuse set `urlcurrent' // restore webuse url

        local path `"`urlpath'"'
        local path : subinstr local path "\" "/", all
        local lastchar = substr("`path'",strlen("`path'"),1)
        if ("`lastchar'"=="/" | "`noslash'"=="noslash") local urlpath "`path'"
        else local urlpath "`path'/"
    }

    //  create ado file

    tempname adohandle
    file open  `adohandle' using `"`adowithpath'"', text write `replace'
    local date : di %tcCCYY-NN-DD clock(c(current_date),"DMY")
    * header
    file write `adohandle' _col(1) `"*! `adoname'.ado `date' $S_NAME"' _new
    file write `adohandle' _col(1) `"program define `adoname'"' _new
    file write `adohandle' _col(4) `"version 13"' _new
    file write `adohandle' _col(4) `"syntax , [Details]"' _new
    * create S_cd global
    file write `adohandle' _col(4) `"* create S_cd globals"' _new
    file write `adohandle' _col(4) `"macro drop S_cdauto S_cdcmd S_cddata S_cddata2 S_cdnote S_cdurl S_cduser S_cdwd"' _new
    file write `adohandle' _col(4) `"global S_cdcmd   "`adoname'""' _new
    file write `adohandle' _col(4) `"global S_cdauto  "`autorun'""' _new
    file write `adohandle' _col(4) `"global S_cddata  "`data'""' _new
    file write `adohandle' _col(4) `"global S_cddata2 "`data2'""' _new
    file write `adohandle' _col(4) `"global S_cdnote  "`note'""' _new
    file write `adohandle' _col(4) `"global S_cdurl   "`urlpath'""' _new
    file write `adohandle' _col(4) `"global S_cduser  "`user'""' _new
    file write `adohandle' _col(4) `"global S_cdwd    "`wdcurrent'""' _new
    * change wd
    file write `adohandle' _col(4) `"* change to new working directory"' _new
    file write `adohandle' _col(4) `"cd "`wdcurrent'""' _new
    * optionally list macros
    file write `adohandle' _col(4) `"* list S_cd macros"' _new
    file write `adohandle' _col(4) `"if ""' "`" "details'"`""!="" {"' _new
    file write `adohandle' _col(6) `"cap _list_s_cd_globals"' _new
    file write `adohandle' _col(6) `"if _rc==0 _list_s_cd_globals"' _new
    file write `adohandle' _col(6) `"else di "workflow command _list_s_cd_globals is not found""' _new
    file write `adohandle' _col(4) `"}"' _new
    * autorun
    file write `adohandle' _col(4) `"* autorun set, run ado file"' _new
    file write `adohandle' _col(4) `"if "$"'`"S_cdauto"!="" {"' _new
    file write `adohandle' _col(8) `"cap noi di _new ". $"'`"S_cdauto""' _new
    file write `adohandle' _col(8) `"cap noi $"'`"S_cdauto"' _new
    file write `adohandle' _col(4) `"}"' _new
    * close up
    file write `adohandle' _col(1) `"end"' _new
    file write `adohandle' _col(1) `"exit"' _new
    file close `adohandle'

//  summary

    di as text "command " as result "`adoname'" ///
       as text " saved in your PERSONAL directory"
    if ("`details'"!="") _list_s_cd_globals
end
exit
* makecd.ado v0.4.0 Scott Long 2017-07-31
* makecd.ado v0.4.1 Scott Long 2017-08-03 check cdcd
* makecd.ado v0.5.0 Scott Long 2017-08-03 user and url
* makecd.ado v0.5.1 Scott Long 2017-08-09 test c(sysdir_personal)
* makecd.ado v0.5.2 Scott Long 2017-08-09 cleanup
* makecd.ado v0.5.3 Scott Long 2017-08-12 noslash
* makecd.ado v0.6.1 Scott Long 2017-08-18 add details option
* makecd.ado v0.6.2 Scott Long 2017-08-18 display autorun
* makecd.ado v0.7.0 Scott Long 2017-09-07
* makecd.ado v0.7.1 Scott Long 2017-09-08