*! version 1.0.0  Statalist distribution 20nov2001
/*
	sjutil install 1(1) [, replace]
	sjutil uninstall 1(1)

	Note, the volume issue may be written:
		# #
		#(#)
		#-#
		#.#
		#:#
*/

program define sjutil
	version 7
	gettoken cmd 0 : 0, parse(" :.,()-")

	Parse_Volume_Issue `0'
	local volume "`s(volume)'"
	local issue "`s(issue)'"
	local 0 `"`s(rest)'"'
	sret clear

	if "`cmd'"=="install" {
		SJ_Install `volume' `issue' `"`0'"'
	}
	else if "`cmd'"=="uninstall" {
		SJ_Uninstall `volume' `issue' `"`0'"'
	}
	else 	error 198
end

program define Parse_Volume_Issue, sclass
	sret clear

	gettoken volume 0 : 0, parse(" :.,()-")
	Check_vol_iss_no volume `"`volume'"'

	gettoken next 0 : 0, parse(" :.,()-")
	capture confirm integer number `next'
	if _rc==0 { 					/* # # */
		Check_vol_iss_no issue `"`next'"'
		SetReturn `volume' `next' `"`0'"'
		exit
	}
							/* #{.-:}# */
	if `"`next'"' == "." | `"`next'"'=="-" | `"`next'"'==":" {
		gettoken next 0 : 0, parse(" .,()-")
		Check_vol_iss_no issue `"`next'"'
		SetReturn `volume' `next' `"`0'"'
		exit
	}

	if `"`next'"' == "(" {				/* #(#) */
		gettoken issue 0 : 0, parse(" .,()-")
		Check_vol_iss_no issue `"`issue'"'
		gettoken next 0 : 0, parse(" .,()-")
		if `"`next'"' != ")" {
			error 198
		}
		SetReturn `volume' `issue' `"`0'"'
		exit
	}
	error 198
end

program define SetReturn, sclass
	sret local volume `"`1'"'
	sret local issue  `"`2'"'
	sret local rest   `"`3'"'
end

program define Check_vol_iss_no
	args type number

	capture confirm integer number `number'
	if _rc==0 {
		if `number'>0 {
			exit
		}
	}
	local number = trim("`number'")
	if "`number'"=="" | "`number'"=="," {
		di as err "nothing found where `type' number expected"
	}
	else	di as err `"`type' number "`number'" invalid"'
	exit 198
end

program define SJ_Install
	args vol iss rest
	local 0 `"`rest'"'
	syntax [, REPLACE]
	preserve
	LoadToc `vol' `iss'

	if "`replace'" != "" {
		local replace ", replace"
	}


	set more off
	local N = _N
	forvalues i=1(1)`N' {
		local pkg = pkg[`i']
		di
		di as txt "-> net install `pkg'`replace'"
		capture noi net install `pkg'`replace'
		if _rc==1 {
			exit 1
		}
	}
end


program define SJ_Uninstall
	args vol iss rest
	local 0 `"`rest'"'

	syntax
	preserve
	LoadToc `vol' `iss'

	di
	di as txt _col(11) "Package" _col(34) "Result"
	di as txt _col(11) "{hline 35}"

	local N = _N
	forvalues i=1(1)`N' {
		local pkg = pkg[`i']
		capture ado uninstall `pkg'
		if _rc == 0 {
			di in gr _col(11) "`pkg'" _col(34) "uninstalled"
		}
		else {
			if _rc == 1 {
				exit 1
			}
			di in gr _col(11) "`pkg'" _col(34) "not found"
		}
	}
end


program define LoadToc
	args vol iss

	local dir "http://www.stata-journal.com/software/sj`vol'-`iss'"

	quietly net from `dir'

	tempfile toc
	copy `dir'/stata.toc `"`toc'"'

	tempvar loc
	quietly {
		drop _all
		infix str pkg 1-40 using `"`toc'"'
		keep if substr(pkg,1,2)=="p "
		if _N==0 {
			noi di in gr "(nothing to load)"
			exit
		}
		replace pkg = trim(substr(pkg,3,.))
		gen byte `loc' = index(pkg," ")
		replace pkg = trim(substr(pkg,1,`loc'))
		drop `loc'
		drop if pkg=="-"
		if _N==0 {
			noi di in gr "(nothing to load)"
			exit
		}
		compress pkg
	}
end