*! cdsave.ado v0.6.2 Scott Long 2017-08-18 display autorun
* cdsave.ado v0.6.1 Scott Long 2017-08-18 add details option

// workflow: cdsave - create cd* commands

capture program drop cdsave
program define cdsave
    version 13.0
    syntax namelist [ , ///
        DATApath(string) /// location of data
        Note(string) /// description
        AUTOrun(string) /// run this ado-file from working directory
        DETails /// display globals
        url(string) /// url
        user(string) /// anyting a user wants to add
        NOSlash /// no ending slash on locations
        replace ]

    //  trap accidentally entering project as cdproject

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

    //  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"'

    //  datapath

    if "`datapath'"!="" {
        capture cd "`datapath'"
        * error ok since path does not need to exist when cdsave is run
        if _rc==0 {
            local datapath `"`c(pwd)'"' // get clean name
            qui cd "`wdcurrent'" // restore working directory
        }
    }
    * if datapath not specified, use working directory
    else local datapath `"`wdcurrent'"'

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

    //  url

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

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

        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) `"*! `adowithpath' `date' $S_NAME"' _new
    file write `adohandle' _col(1) `"*! `wdcurrent'"' _new
    file write `adohandle' _col(1) `"program define `adoname'"' _new
    file write `adohandle' _col(4) `"version 13"' _new
    file write `adohandle' _col(4) `"args 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_cdnote 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 "`datapath'""' _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 we
    file write `adohandle' _col(4) `"* change to new working directory"' _new
    file write `adohandle' _col(4) `"cd "`wdcurrent'""' _new
    * optioanlly list macros
    file write `adohandle' _col(4) `"* list S_cd macros"' _new
    file write `adohandle' _col(4) `"if ""' "`" "details'"`""!="" cdinfo"' _new
    * autorun
    file write `adohandle' _col(4) `"* autorun set, run ado file"' _new
    *file write `adohandle' _col(4) `"if "$"'`"S_cdauto"!="" cap noi $"'`"S_cdauto"' _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 di _new ". $"'`"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'"!="") cdinfo
end
exit
*! cdsave.ado v0.5.3 Scott Long 2017-08-12 noslash
*! cdsave.ado v0.5.2 Scott Long 2017-08-09 cleanup
*! cdsave.ado v0.5.1 Scott Long 2017-08-09 test c(sysdir_personal)
*! cdsave.ado v0.5.0 Scott Long 2017-08-03 user and url
*! cdsave.ado v0.4.1 Scott Long 2017-08-03 check cdcd
*! cdsave.ado v0.4.0 Scott Long 2017-07-31
*! TODO: What to do it datapath does not exist
// changewd: create cd* ado file and save in PERSONAL directory
//
// Globals set in by cd* ado files:
//   S_cdcmd  : name of command
//   S_cdauto : ado file to automatically run
//   S_cddata : data path
//   S_cdnote : note about project