program define winup
*! version 1.0 24Oct2001
	version 7
	args fix fix2


	if "`fix'"=="" {
		capture update q

		if (r(inst_ado)==r(avbl_ado) & r(inst_exe)==r(avbl_exe)) /*
			*/ & (r(inst_exe)!=. & r(inst_ado)!=.) {
				di
				di as text "No updates needed."
				exit
			}

		if r(inst_ado)<r(avbl_ado) | r(inst_ado)==. {
			di
			di as text "Downloading new ado-files, please wait..."
			capture update ado
			di
			di as text "Download and ado-capture file installation complete."
		}

		if r(inst_exe)<r(avbl_exe) | r(inst_exe)==. {
			di
			di as text "Downloading new executable, please wait..."
			capture update executable
			local dir : sysdir STATA
			local cdir : pwd
			capture cd "`dir'"
			capture file open counter using exeup, read write text
			capture file read counter count
			tokenize "`count'"
			local 1 = `1'+1
			!rename wstata.exe wstata.old`1'
			!rename wstata.bin wstata.exe
			capture file seek counter tof
			capture file write counter "`1' installed on $S_DATE" _newline
			capture file close counter
			capture cd "`cdir'"
			di
			di as text "Download and executable installation complete."
			di
			di as text "Now exit and restart Stata to use the update."
			di
			di "NOTE:  If you notice any problems with the update,"
			di "you can type -" as input "winup, restore" as text "- "
			di "to restore the previous version of Stata."
			exit
		}
	}

	if "`fix'"=="restore" {
		local dir : sysdir STATA
		local cdir : pwd
		capture cd "`dir'"
		capture confirm capture file exeup
		local exeup_exists _rc
		capture file open counter using exeup, read write text
		capture file read counter count
		tokenize "`count'"
		capture confirm capture file wstata.old`1'
		if _rc!=0 & `exeup_exists'!=0 {
			di
			di as text "Nothing local to restore from.   You can try:"
			di
			di as text "     1. -" as input "winup repair, force" /*
				*/ as text "- to correct the installation"
			di as text "          or "
			di as text "     2.  reinstall Stata"
			di as text "          or "
			di as text "     3.  call Stata for help."
			capture file close counter
			exit
		}
		!del wstata.res
		!rename wstata.exe wstata.res
		!rename wstata.old`1' wstata.exe
		local 1 = `1'-1
		capture file seek counter tof
		capture file write counter "`1' restored on $S_DATE" _newline
		capture file close counter
		capture cd "`cdir'"
		di
		di as text "Previous version of Stata has been restored."
		di as text "You must exit and restart for the change to take effect."
		exit
	}

	if "`fix'"=="repair," & "`fix2'"=="force"{
		nobreak {
		local cdir : pwd
		local updates : sysdir UPDATES
		capture cd "`updates'"
		!del update
		local dir : sysdir STATA
		local base : sysdir BASE
		di
		di as text "Downloading and rebuilding ado-files, please wait..."
		capture cd "`base'"
		copy http://www.stata.com/users/huckelba/winup/base.exe base.exe
		!base
		!del base.exe
		capture update ado
		di
		di as text "Download and ado-capture file installation complete."
		di
		di as text "Downloading new executable, please wait..."
		capture cd "`dir'"
		!del wstata.dl
		capture copy http://www.stata.com/updates2/win/wstata.bin wstata.dl
		!del exeup wstata.bin* wstata.old* wstata.res*
		capture file open counter using exeup, read write text
		capture file read counter count
		tokenize "`count'"
		local 1 = `1'+1
		!rename wstata.exe wstata.old`1'
		!rename wstata.dl wstata.exe
		capture file seek counter tof
		capture file write counter "`1' installed on $S_DATE" _newline
		capture file close counter
		!rename wstata.dl wstata.exe
		di
		di as text "Download and executable installation complete."
		di
		di as text "Now exit and restart Stata to use the update."
		}
		exit
	}
	else {
		error 198
	}
end