summaryrefslogtreecommitdiff
path: root/cmdline/apt-extracttemplates.cc
blob: b7af0300586d46491d2afb50e13121b198142701 (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
140
141
142
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <wait.h>
#include <fstream.h>

#include <apt-pkg/init.h>
#if APT_PKG_MAJOR >= 3
#define APT_COMPATIBILITY 986
#include <apt-pkg/debversion.h>
#endif
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/progress.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/pkgcachegen.h>
#include <apt-pkg/version.h>
#include "debfile.h"
	
#define TMPDIR "/var/lib/debconf/"
#define STR(x) (x ? x : "")
//#define TMPDIR "tmp/"

void help(void)
{
	fprintf(stderr, "apt-extracttemplates deb [deb]\n");
	exit(0);
}

char *writefile(const char *prefix, const char *data)
{
	char fn[512];
	static int i;
	snprintf(fn, sizeof(fn), "%s%s.%u%d", TMPDIR, prefix, getpid(), i++);

	if (data == NULL) data = "";

	ofstream ofs(fn);
	if (!ofs) return NULL;
	ofs << data;
	ofs.close();
	return strdup(fn);
}

void writeconfig(const DebFile &file)
{
	char *templatefile = writefile("template", file.Template);
	char *configscript = writefile("config", file.Config);

	if (templatefile == 0 || configscript == 0)
	{
		fprintf(stderr, "Cannot write config script or templates\n");
		return;
	}
	printf("%s %s %s %s\n",
		STR(file.Package),	// Package
		STR(file.Version),	// Version
		templatefile,	// Template
		configscript 	// Config
	);
}

void init(MMap *&Map, pkgCache *&Cache)
{
	// Initialize the apt cache
	if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
	{
		fprintf(stderr, "Cannot initialize apt cache\n");
		return;
	}
	pkgSourceList List;
	List.ReadMainList();
	OpProgress Prog;
	pkgMakeStatusCache(List,Prog,&Map,true);
	Cache = new pkgCache(Map);
}

int main(int argc, char **argv, char **env)
{
	int idx = 0;
	char **debs = 0;
	int numdebs = 0;
	MMap *Map = 0;
	const char *debconfver = NULL;

	init(Map, DebFile::Cache);
	if (Map == 0 || DebFile::Cache == 0)
	{
		fprintf(stderr, "Cannot initialize APT cache\n");
		return 1;
	}

	debconfver = DebFile::GetInstalledVer("debconf");
	
	if (debconfver == NULL) 
	{
		fprintf(stderr, "Cannot get debconf version. Is debconf installed?\n");
		return 1;
	}

	numdebs = argc - 1;
	debs = new char *[numdebs];
	memcpy(debs, &argv[1], sizeof(char *) * numdebs);

	if (numdebs < 1) 
	{
		fprintf(stderr, "apt-extracttemplates foo.deb [...]\n");
		return 0;
	}

	for (idx = 0; idx < numdebs; idx++)
	{
		DebFile file(debs[idx]);
		if (file.Go() == false) 
		{
			fprintf(stderr, "Cannot read %s\n", debs[idx]);
			continue;
		}
		if (file.Template != 0 && file.ParseInfo() == true)
		{
			if (file.DepVer != 0 && *file.DepVer != 0 &&
			    pkgCheckDep(file.DepVer, 
			                debconfver, file.DepOp) == false) 
				continue;
			if (file.PreDepVer != 0 && *file.PreDepVer != 0 &&
			    pkgCheckDep(file.PreDepVer, 
			                debconfver, file.PreDepOp) == false) 
				continue;

			writeconfig(file);
		}
	}
	

	delete Map;
	delete DebFile::Cache;

	return 0;
}