*! cddrop.ado v0.6.0 Scott Long 2017-08-13

// workflow: cddrop - drop or restore cd* commands

capture program drop cddrop
program define cddrop
    version 13.0
    syntax namelist [ , delete REStore]

    if "`restore'"=="restore" local suf "dropped"
    else local suf "ado"

    //  list of names to process

    * if all, use any that start with cd in PERSONAL
    if "`namelist'"=="_all" {
        local cdfiles : dir "`c(sysdir_personal)'" files "cd*.`suf'"
    }
    * if single file
    else {
        local n12 = substr("`namelist'",1,2) // add cd if missing
        if "`n12'"!="cd" local namelist "cd`namelist'"
        local cdfiles "`namelist'.`suf'"
    }
    * remove suffix
    local cdfiles : subinstr local cdfiles ".`suf'" "", all

    //  delete or rename to dropped

    if "`restore'"!="restore" {
        foreach file in `cdfiles' {
            * if not delete, make a copy then later delete
            if "`delete'"!="delete" {
                * copied file later deleed
                copy `"`c(sysdir_personal)'`file'.ado"' ///
                     `"`c(sysdir_personal)'`file'.dropped"' , replace
                di as result "`file'.ado" as text " renamed " ///
                   as result "`file'.dropped"
            }
            else di as result "`file'.ado" as text " deleted"
            * delete file
            rm `"`c(sysdir_personal)'`file'.ado"'
            * remove command from memory
            capture program drop `file'
        }
    }

    //  restore *.dropped files to *.ado files

    else {
        foreach file in `cdfiles' {
            copy `"`c(sysdir_personal)'`file'.dropped"' ///
                 `"`c(sysdir_personal)'`file'.ado"'
            if _rc==0 | _rc==111 { // copy successful
                rm `"`c(sysdir_personal)'`file'.dropped"'
                di as result "`file'.dropped" as text " renamed " ///
                   as result "`file'.ado"
            }
            else {
                di as result "`file'.dropped" as text " not found"
            }
        } // file loop
    }
end
exit
* cddrop.ado v0.5.0 Scott Long 2017-08-04
* cddrop.ado v0.4.1 Scott Long 2017-08-02
* cddrop.ado v0.4.0 Scott Long 2017-08-02
* TODO: if first letters are not cd*.ado, try adding cd

cddrop aut
cddrop cdaut
cddrop _all
cddrop aut, delete
cddrop cdaut, delete
cddrop _all, delete
cddrop aut, restore
cddrop cdaut, restore
cddrop _all, restore