:
#	@(#) swconfig.sh 21.1 89/10/31 
#
#	Copyright (C) The Santa Cruz Operation, 1988, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
# swconfig [-p] [-a]
#	gives the user a report of what packages have been added to the
#	system since the initial instilation (including the initial inst.)
#	The standard (no flags) command will give the Set, the Release and
#	tell the user whether it is fully, partially or not there at all.
#	The [-p] flag tells swconfig to print out all the packages that have
#	been installed or removed. The [-a] flag gives a complete history
#	of the computer's cutsom log, including failed instilation/removal
#	attempts and bad log entries.
#	   The entire program is writen in AWK (piped through UNIQ)

exec awk '
BEGIN	{	FS="=" 
			FLAG=""
			if (ARGC == 3) {
				if (ARGV[2] ~ /-a/) FLAG="a"
				else if (ARGV[2] ~ /-p/) {
					FLAG="p"
					printf("%-32s %-9s %-11s %s\n","Set","Release","Notes", "Packages")
					printf("%-32s %-9s %-11s %s\n","---","-------","-----", "--------")
					}
				else {
					print "usage:"
					print "   swconfig [-a] [-p]"
					exit(1)
					}
				}
			else if (ARGC == 2)  {
				printf("%-32s %-9s %-17s\n","Set","Release","Notes")
				printf("%-32s %-9s %-17s\n","---","-------","-----")
				}
			ARGV[2]=""
			num = -1
		}

# date()
#		load date out of /usr/lib/custom/history and chop off the quotes
#		and backslash

/^date/	{	num = num + 1
			date[num]=substr($2, 2, length($2) - 4)
			}

# prd()
#		Get all information off the line begining with "prd="
#		including product (prd), release (rel), type (typ), set (st)
#		and whether it is and update or a first installation (upd)

/^prd/	{
			split($2 , i, " ")
			prd[num]=i[1]
			split($3 , i, " ")
			if (i[1] == "upd") rel[num]="   "
			else rel[num]=i[1]
			split($4, i, " ")
			if (i[1] == "typ") upd[num]="installed"
			else upd[num]="update " i[1]
			split($5, i, " ")
			if (i[1] == "set") typ[num]="(unknown)"
			else typ[num]=i[1]
			st[num]=substr($6, 2, length($6) - 4) 
			}

# action()
#		If the action was an update then set upd accordinly. Also note
#		if action was a removal that did not fail. If the action failed
#		then set "failed" to yes.

/^action/ && $3 ~ /Update/ {
			upd[num]="update to " rel[num]
			}
/^action/ && $2 ~ /Remov/ && $3 !~ /Fail/ {
			upd[num]="removed"
			}
/^action/ && $3 !~ /Success/ { 
			failed[num]="yes" 
			}

# pkgs()
#		Load the list of packages installed in, without the double quotes
#		around the end.

/^pkgs/ {
			pkgs[num]=substr($2, 2, length($2) - 2)
			if (pkgs[num] == "") pkgs[num] = "  "
			}

# print out the list of all the sets read in, with conditions on
#		whether the list is (-p) with packages or (-a) with all pertinant
#		data included.

END		{	
			testcond = "(null)"
			for (j=num; j>=0; j--) {
				if (FLAG == "p" && failed[j] != "yes" && already[st[j]] != "yes") {
					if (length(st[j]) == 0) st[j]=" (prd = " prd[j] ")"
					if (pkgs[j] ~ /ALL/) already[st[j]] = "yes"
					printf("%-32.32s %-9.9s %-11.11s", st[j], rel[j], upd[j])
					output(pkgs[j], 23)
				}
				else if (FLAG == "a") {
					if (testcond != st[j]) {
						if (length(st[j]) == 0) st[j]="Unknown"
						printf("Set: %s (prd = %s)\n",st[j], prd[j])
						testcond = st[j]
					}
					printf("  %s\n", date[j])
					if (failed[j] == "yes")
						printf("  %-22.22s ", upd[j] " FAILED")
					else printf("  %-22.22s ", upd[j] " successfully")
					printf("   Release: %-9.9s", rel[j])
					printf("   Type: %s\n", typ[j])
					printf("  Packages: ")
					if (length(pkgs[j]) == 0) printf("\n")
					output(pkgs[j], 65)
					printf("\n")
				}
				else if (failed[j] != "yes" && already[st[j]] !~ /yes/) {
					if (length(st[j]) == 0) st[j]=" (prd = " prd[j] ")"
					if (pkgs[j] ~ /ALL/) printf("%-32.32s %-9.9s %-17.17s\n",
						 st[j],rel[j],upd[j]) 
					else printf("%-32.32s %-9.9s partially installed\n",
						 st[j],rel[j]) 
				   already[st[j]] = "yes"
					}
			}
		}

# output()
#		Only print out as many of the packages in "h" that will fit from
#		screen characters 62 to 77. If there are more than that, then
#		save the next packages for the next line (with 62 spaces of white
#		space). Also chop off the single quotes around the package names.

function output(h, w) {
	while (length(h) > 0) {
		split(h, i, " ")
		o=""
		c=1
		while((length(i[c]) + length(o)) < w) {
			if (i[c] ~ /^'\''/ && i[c] ~ /'\''$/) o=o" "substr(i[c],2,length(i[c])-2)
			else if (i[c] ~ /^'\''/) o=o" "substr(i[c],2,length(i[c])-1)
			else if (i[c] ~ /'\''$/) o=o" "substr(i[c],1,length(i[c])-1)
			else o=o" "i[c]
			h = substr(h, length(i[c]) + 2)
			c++
			}
		printf("%s\n", o)
		if (length(h) > 0) printf("%-54s"," ")
	}
}
' /usr/lib/custom/history ${1} | uniq

