aboutsummaryrefslogtreecommitdiff
path: root/src/install_pkg.c
blob: c23397680686d5ac85c8680c6d3c97666f36c35c (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
/*

  pkgit - package it!

  Copyright (C) 2026 dacctal
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "add_repo.h"
#include "build.h"
#include "fetch_src.h"
#include "files.h"
#include "install_pkg.h"
#include "lua_state.h"
#include "name_from_url.h"
#include "vars.h"

void install_pkg(Pkg pkg) {
	if (is_directory(pkg.src)) {
		if (!is_forced) {
			if (is_verbose) printf(
				"%s %s is already installed.\n",
				print_skipped, pkg.name
			);
			return;
		} else {
			if (is_verbose) printf(
				"%s %s is already installed.\n",
				print_warning, pkg.name
			);
		}
	}
	char cwd[MAX_PATH_LEN];
	getcwd(cwd, MAX_PATH_LEN);
	if (strcmp(pkg.src, cwd) != 0) chdir(pkg.src);

	if (pkg.is_local) {
	cpdir(cwd, pkg.src);
	} else {
		printf(
			"%s fetching %s%s%s\n",
			print_pkgit, green, pkg.name, color_reset
		);
		fetch_src(pkg);
		if (is_verbose) printf(
			"%s fetched %s%s%s\n",
			print_pkgit, green, pkg.name, color_reset
		);
	}

	printf(
		"%s building %s%s%s\n",
		print_pkgit, green, pkg.name, color_reset
	);
	build(pkg);
	if (is_verbose) printf(
		"%s built %s%s%s\n",
		print_pkgit, green, pkg.name, color_reset
	);

	printf(
		"%s installing %s%s%s\n",
		print_pkgit, green, pkg.name, color_reset
	);
	bool install_success = false;
	if (!install_success && repo_install(pkg.name, pkg.target))
		install_success = true;
	if (!install_success && bldit_install(pkg.target))
		install_success = true;
	if (!install_success && config_install(pkg.src, pkg.target))
		install_success = true;
	if (!install_success) {
		printf(
			"%s no install function availible for package: %s\n",
			print_error, pkg.name
		);
		return;
	}
	printf(
		"%s installed %s%s%s\n",
		print_success, green, pkg.name, color_reset
	);

	bool repo_exists = false;
	for (size_t i = 0; i < cached_repos_count; i++) {
		char *repo_name = name_from_url(cached_repos[i].source_key);
		if (strcmp(repo_name, pkg.name) == 0) { repo_exists = true; }
		free(repo_name);
	}

	if (!repo_exists) {
		printf(
			"%s adding %s%s%s\n",
			print_pkgit, green, pkg.name, color_reset
		);
		if (pkg.url && strlen(pkg.url) > 0) {
			add_repo(pkg.url, pkg.name);
		}
		printf(
			"%s added %s%s%s\n",
			print_pkgit, green, pkg.name, color_reset
		);
	} else {
		if (is_verbose) printf(
			"%s repo already exists, done\n",
			print_pkgit
		);
	}
}