Regularize command-line flags.

All the notice binaries have -title

All the binaries that can -stripPrefix can strip multiple.

Bug: 68860345
Bug: 151177513
Bug: 151953481
Bug: 213388645
Bug: 210912771

Test: m all
Test: m systemlicense
Test: m bom; out/soong/host/linux-x85/bom ...
Test: m dumpgraph; out/soong/host/linux-x85/dumpgraph ...
Test: m dumpresolutions; out/soong/host/linux-x85/dumpresolutions ...
Test: m htmlnotice; out/soong/host/linux-x85/htmlnotice ...
Test: m rtrace; out/soong/host/linux-x85/rtrace ...
Test: m textnotice; out/soong/host/linux-x85/textnotice ...
Test: m xmlnotice; out/soong/host/linux-x85/xmlnotice ...

where ... is the path to the .meta_lic file for the system image. In my
case if

$ export PRODUCT=$(realpath $ANDROID_PRODUCT_OUT --relative-to=$PWD)

... can be expressed as:

${PRODUCT}/gen/META/lic_intermediates/${PRODUCT}/system.img.meta_lic

Change-Id: I08357bf1adb048abba6563cf3cea6ee6d60405e0
diff --git a/tools/compliance/cmd/textnotice/textnotice.go b/tools/compliance/cmd/textnotice/textnotice.go
index eebb13d..35c5c24 100644
--- a/tools/compliance/cmd/textnotice/textnotice.go
+++ b/tools/compliance/cmd/textnotice/textnotice.go
@@ -16,6 +16,7 @@
 
 import (
 	"bytes"
+	"compress/gzip"
 	"flag"
 	"fmt"
 	"io"
@@ -32,7 +33,8 @@
 var (
 	outputFile  = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
 	depsFile    = flag.String("d", "", "Where to write the deps file")
-	stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
+	stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
+	title       = flag.String("title", "", "The title of the notice file.")
 
 	failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
 	failNoLicenses    = fmt.Errorf("No licenses found")
@@ -42,10 +44,27 @@
 	stdout      io.Writer
 	stderr      io.Writer
 	rootFS      fs.FS
-	stripPrefix string
+	stripPrefix []string
+	title       string
 	deps        *[]string
 }
 
+func (ctx context) strip(installPath string) string {
+	for _, prefix := range ctx.stripPrefix {
+		if strings.HasPrefix(installPath, prefix) {
+			p := strings.TrimPrefix(installPath, prefix)
+			if 0 == len(p) {
+				p = ctx.title
+			}
+			if 0 == len(p) {
+				continue
+			}
+			return p
+		}
+	}
+	return installPath
+}
+
 func init() {
 	flag.Usage = func() {
 		fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
@@ -58,6 +77,19 @@
 	}
 }
 
+// newMultiString creates a flag that allows multiple values in an array.
+func newMultiString(name, usage string) *multiString {
+	var f multiString
+	flag.Var(&f, name, usage)
+	return &f
+}
+
+// multiString implements the flag `Value` interface for multiple strings.
+type multiString []string
+
+func (ms *multiString) String() string     { return strings.Join(*ms, ", ") }
+func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
+
 func main() {
 	flag.Parse()
 
@@ -89,14 +121,21 @@
 	}
 
 	var ofile io.Writer
+	var closer io.Closer
 	ofile = os.Stdout
+	var obuf *bytes.Buffer
 	if *outputFile != "-" {
-		ofile = &bytes.Buffer{}
+		obuf = &bytes.Buffer{}
+		ofile = obuf
+	}
+	if strings.HasSuffix(*outputFile, ".gz") {
+		ofile, _ = gzip.NewWriterLevel(obuf, gzip.BestCompression)
+		closer = ofile.(io.Closer)
 	}
 
 	var deps []string
 
-	ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps}
+	ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, *title, &deps}
 
 	err := textNotice(ctx, flag.Args()...)
 	if err != nil {
@@ -106,8 +145,12 @@
 		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
 		os.Exit(1)
 	}
+	if closer != nil {
+		closer.Close()
+	}
+
 	if *outputFile != "-" {
-		err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666)
+		err := os.WriteFile(*outputFile, obuf.Bytes(), 0666)
 		if err != nil {
 			fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err)
 			os.Exit(1)
@@ -152,11 +195,7 @@
 		for _, libName := range ni.HashLibs(h) {
 			fmt.Fprintf(ctx.stdout, "%s used by:\n", libName)
 			for _, installPath := range ni.HashLibInstalls(h, libName) {
-				if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
-					fmt.Fprintf(ctx.stdout, "  %s\n", installPath[len(ctx.stripPrefix):])
-				} else {
-					fmt.Fprintf(ctx.stdout, "  %s\n", installPath)
-				}
+				fmt.Fprintf(ctx.stdout, "  %s\n", ctx.strip(installPath))
 			}
 			fmt.Fprintln(ctx.stdout)
 		}
diff --git a/tools/compliance/cmd/textnotice/textnotice_test.go b/tools/compliance/cmd/textnotice/textnotice_test.go
index 39f711d..0ed3394 100644
--- a/tools/compliance/cmd/textnotice/textnotice_test.go
+++ b/tools/compliance/cmd/textnotice/textnotice_test.go
@@ -564,7 +564,7 @@
 
 			var deps []string
 
-			ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix, &deps}
+			ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}, "", &deps}
 
 			err := textNotice(&ctx, rootFiles...)
 			if err != nil {