*! cdmake.ado v0.1.0 Scott Long 2017-07-23
program define cdmake
    version 13.0

    syntax name [ , DATApath(string) replace ]

    local project "`namelist'"

//  ado file information

    local PERSONAL "`c(sysdir_personal)'"
    local adoname `"cd`project'"'
    local adofile `"`PERSONAL'`adoname'.ado"'
    local currentpwd `"`c(pwd)'"'
    local date : di %tcCCYY-NN-DD clock(c(current_date),"DMY")

//  data path

    if "`datapath'"!="" {

        capture cd "`datapath'"
        if _rc!=0 {
            display as error "invalid data path `datapath'"
            exit
        }
        else {
            local datapath `"`c(pwd)'"' // get clean name
            qui cd "`currentpwd'" // restore working directory
        }
    }

    else {
        local datapath `"`currentpwd'"' // default path is working directory
    }

//  create ado file

    tempname adohandle
    file open  `adohandle' using `"`adofile'"', text write `replace'
    file write `adohandle' _col(1) `"*! `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) `"cd "`currentpwd'""' _new
    file write `adohandle' _col(4) `"global S_data "`datapath'\""' _new
    file write `adohandle' _col(1) `"end"' _new
    file write `adohandle' _col(1) `"exit"' _new
    file close `adohandle'

    display `"`adoname'.ado saved in `PERSONAL'"'
    di _col(5) `"working directory:  `c(pwd)'"'
    di _col(5) `"data path:          `datapath'"'

end
exit