*! editprofile.ado - Edit your user-specific profile.do file
*! version 1.0.0  17June2026  Wu Lianghai (agd2010@yeah.net)
*! School of Business, Anhui University of Technology (AHUT)
*! Ma'anshan, China

program define editprofile
    version 16.0

    syntax [, CREATE OPEN SHOWpath PATH(string) ]

    /* ----------------------------------------------------------------
       Default action: if no options specified, OPEN the existing
       profile.do file.  The search order follows Stata's own
       convention: current directory → Stata system directory →
       PERSONAL ado directory.
       ---------------------------------------------------------------- */

    if "`open'" == "" & "`create'" == "" & "`showpath'" == "" {
        local open = "open"
    }

    /* ----------------------------------------------------------------
       Resolve the path to profile.do
       ---------------------------------------------------------------- */
    if "`path'" != "" {
        /* User supplied a path — normalise slashes to forward style */
        local profile_file : subinstr local path "\" "/", all

        /* If the supplied path is a directory, append profile.do */
        if (strpos("`profile_file'", "profile.do") == 0) {
            if (substr("`profile_file'", -1, 1) == "/") {
                local profile_file = "`profile_file'profile.do"
            }
            else {
                local profile_file = "`profile_file'/profile.do"
            }
        }
    }
    else {
        /* No path given — search in the conventional order */
        local found = 0

        /* 1. Current working directory */
        capture confirm file "profile.do"
        if _rc == 0 {
            local profile_file = c(pwd) + "/profile.do"
            local found = 1
        }

        /* 2. Stata executable directory */
        if `found' == 0 {
            local try = c(sysdir_stata) + "profile.do"
            capture confirm file "`try'"
            if _rc == 0 {
                local profile_file = "`try'"
                local found = 1
            }
        }

        /* 3. PERSONAL ado directory (recommended location) */
        if `found' == 0 {
            local profile_file = c(sysdir_personal) + "profile.do"
        }
    }

    /* ----------------------------------------------------------------
       SHOWpath — just display the path and exit
       ---------------------------------------------------------------- */
    if "`showpath'" != "" {
        capture confirm file "`profile_file'"
        if _rc == 0 {
            di as txt "{p}profile.do found at:{p_end}"
            di as res "  {stata `profile_file':`profile_file'}"
            di as txt "{p}This file will execute automatically every time "
            di as txt "Stata starts.{p_end}"
        }
        else {
            di as txt "{p}No profile.do found at:{p_end}"
            di as res "  `profile_file'"
            di as txt "{p}Use {cmd:editprofile, create} to create a "
            di as txt "template profile.do at this location.{p_end}"
        }
        exit
    }

    /* ----------------------------------------------------------------
       CREATE — generate a template profile.do
       ---------------------------------------------------------------- */
    if "`create'" != "" {
        capture confirm file "`profile_file'"
        if _rc == 0 {
            di as txt "{p}profile.do already exists at:{p_end}"
            di as res "  {stata `profile_file':`profile_file'}"
            di as txt "{p}Opening the existing file instead of overwriting.{p_end}"
            doedit "`profile_file'"
        }
        else {
            /* Write a well-documented template */
            tempname fh
            quietly file open `fh' using "`profile_file'", write text replace

            file write `fh' "/*" _n
            file write `fh' "===============================================================================" _n
            file write `fh' "  profile.do — User startup file" _n
            file write `fh' "  Generated by editprofile.ado on `c(current_date)'" _n
            file write `fh' "  Author:  Wu Lianghai (agd2010@yeah.net)" _n
            file write `fh' "  School of Business, Anhui University of Technology (AHUT)" _n
            file write `fh' "  Ma'anshan, China" _n
            file write `fh' "===============================================================================" _n
            file write `fh' "  Stata executes profile.do automatically at startup." _n
            file write `fh' "  Uncomment and modify the lines below to customise your environment." _n
            file write `fh' "===============================================================================" _n
            file write `fh' "*/" _n _n

            file write `fh' "* ---- (1) Default working directory ----" _n
            file write `fh' "* cd " `"""' "C:/Users/" `"""' _n _n
            file write `fh' "* ---- (2) Graphics scheme ----" _n
            file write `fh' "* set scheme s2color" _n _n
            file write `fh' "* ---- (3) Memory allocation (Stata/MP or SE) ----" _n
            file write `fh' "* set max_memory 2g" _n _n
            file write `fh' "* ---- (4) Variable-value label length ----" _n
            file write `fh' "* set maxvar 32767" _n _n
            file write `fh' "* ---- (5) Matrices size ----" _n
            file write `fh' "* set matsize 800" _n _n
            file write `fh' "* ---- (6) Scrollback buffer ----" _n
            file write `fh' "* set scrollbufsize 500000" _n _n
            file write `fh' "* ---- (7) Additional ado directories ----" _n
            file write `fh' "* adopath ++ " `"""' "C:/ado/personal" `"""' _n _n
            file write `fh' "* ---- (8) Load favourite packages ----" _n
            file write `fh' "* cap which estout" _n
            file write `fh' "* cap which outreg2" _n
            file write `fh' "* cap which distinct" _n _n
            file write `fh' "* ---- (9) Set default log type ----" _n
            file write `fh' "* set logtype text" _n _n
            file write `fh' "* ---- (10) Quietly confirm startup ----" _n
            file write `fh' "display as txt " `"""' "(profile.do loaded)" `"""' _n

            quietly file close `fh'

            di as txt "{p}New profile.do created at:{p_end}"
            di as res "  {stata `profile_file':`profile_file'}"
            di as txt "{p}This file will run automatically every time you "
            di as txt "launch Stata.  Customise the settings and save.{p_end}"
            doedit "`profile_file'"
        }
        exit
    }

    /* ----------------------------------------------------------------
       OPEN — open an existing profile.do in the do-file editor
       ---------------------------------------------------------------- */
    if "`open'" != "" {
        capture confirm file "`profile_file'"
        if _rc == 0 {
            di as txt "{p}Opening profile.do:{p_end}"
            di as res "  {stata `profile_file':`profile_file'}"
            doedit "`profile_file'"
        }
        else {
            di as err "{p}profile.do not found at:{p_end}"
            di as res "  `profile_file'"
            di as txt "{p}Use {cmd:editprofile, create} to create a "
            di as txt "template profile.do at this location.{p_end}"
            di as txt "{p}Or use {cmd:editprofile, path(filename)} to "
            di as txt "specify a different location.{p_end}"
            exit 601
        }
    }

end

exit