summaryrefslogtreecommitdiff
path: root/control.write.go
blob: 52c74d648badee240013608325384263e946a541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/go-cmd/cmd"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

/*
The default order to write the fields in the control file:

root@mirrors:/home/mirrors/debian/pool/main/m/moon-buggy# dpkg -I moon-buggy_1.0.51-15_arm64.deb
 new Debian package, version 2.0.
 size 196540 bytes: control archive=6240 bytes.
     202 bytes,    12 lines   *  config               #!/bin/sh
     601 bytes,    14 lines      control
    1020 bytes,    15 lines      md5sums
    1757 bytes,    59 lines   *  postinst             #!/bin/sh
     551 bytes,    22 lines   *  postrm               #!/bin/sh
   11703 bytes,   104 lines      templates
 Package: moon-buggy
 Version: 1:1.0.51-15
 Architecture: arm64
 Maintainer: Christian T. Steigies <[email protected]>
 Installed-Size: 329
 Depends: debconf (>= 0.5) | debconf-2.0, libc6 (>= 2.34), libncurses6 (>= 6), libtinfo6 (>= 6)
 Conflicts: moon-buggy-pause, suidmanager (<< 0.50)
 Section: games
 Priority: optional
 Homepage: http://seehuhn.de/pages/moon-buggy
 Description: Drive a car across the moon
  Moon-buggy is a simple character graphics game, where you drive some
  kind of car across the moon's surface.  Unfortunately there are
  dangerous craters there.  Fortunately your car can jump over them!
*/

func writeDebianControlFile(repo *gitpb.Repo) bool {
	filename := "files/DEBIAN/control"
	cf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Info("open control file failed", err)
		return false
	}
	fmt.Fprintln(cf, "Package:", repo.Control["Package"]) // c.Package.String())
	// never do Source as it has something to do with where aptly puts the files
	// fmt.Fprintln(cf, "Source:", repo.Control["Source"])   // c.Source.String())
	fmt.Fprintln(cf, "Version:", repo.Control["Version"])
	if repo.Control["Architecture"] == "" {
		repo.Control["Architecture"] = "amd64"
	}
	fmt.Fprintln(cf, "Architecture:", repo.Control["Architecture"]) // c.Architecture.String())

	writeControlVar(cf, repo, "Maintainer")
	writeControlVar(cf, repo, "Depends")
	writeControlVar(cf, repo, "Build-Depends")
	writeControlVar(cf, repo, "Conflicts")
	writeControlVar(cf, repo, "Packager")
	writeControlVar(cf, repo, "GoPath")
	writeControlVar(cf, repo, "URL")

	stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC")
	// update to now now despite what the GUI is showing
	fmt.Fprintln(cf, "Package-Build-Date:", stamp)
	fmt.Fprintln(cf, "Git-Tag-Date:", "todo: get from repo")

	desc, _ := repo.Control["Description"] // c.Description.String()
	parts := strings.Split(desc, "\n")
	fmt.Fprintln(cf, "Description:", strings.Join(parts, "\n "))

	log.Info("file written as:", filename)
	return true
}

func writeControlVar(f *os.File, repo *gitpb.Repo, varname string) {
	val, _ := repo.Control[varname]
	if val == "" {
		return
	}
	// fix this when this code is rewritten to use the .proto
	if varname == "URL" {
		varname = "Homepage"
	}
	fmt.Fprintln(f, varname+":", val)
}

// try to guess or figure out the config file values
// if there is not a control file
func computeControlValues(repo *gitpb.Repo) bool {
	if repo.Control["Package"] == "" {
		// get the package name from the repo name
		path := repo.Control["pathL"] // c.pathL.String()
		parts := strings.Split(path, "/")
		name := parts[len(parts)-1]
		repo.Control["Package"] = name
	}
	if repo.Control["Source"] == "" {
		repo.Control["Source"] = repo.Control["Package"]
	}
	if repo.Control["Build-Depends"] == "" {
		repo.Control["Build-Depends"] = repo.Control["golang"]
	}
	if repo.Control["Recommends"] == "" {
		repo.Control["Recommends"] = repo.Control["go-gui-toolkits"]
	}
	if repo.Control["Maintainer"] == "" {
		repo.Control["Maintainer"] = "todo: get from ENV"
	}
	if repo.Control["Description"] == "" {
		repo.Control["Description"] = "todo: put URL here"
	}
	return true
}

// stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC")

func getDateStamp(tag string) string {
	var r cmd.Status
	r = me.repo.Run([]string{"git", "log", "-1", "--format=%at", tag})

	out := strings.Join(r.Stdout, "\n")
	out = strings.TrimSpace(out)

	// Convert the string to an integer
	gitTagTimestampInt, err := strconv.ParseInt(out, 10, 64)
	if err != nil {
		fmt.Println("Error converting timestamp:", err)
		return "git tag " + tag + " unknown"
	}

	// Parse the Unix timestamp into a time.Time object
	gitTagDate := time.Unix(gitTagTimestampInt, 0)
	return gitTagDate.UTC().Format("2006-01-02_15:04:05_UTC") // close to RFC3339
}