summaryrefslogtreecommitdiffstats
path: root/.local/bin/install-port
blob: 3d5bf620f100816ef6dbcbd03bdc78b94556f9d4 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env perl -w

use strict;
use warnings;
use Term::ANSIColor;

sub upgrdports {
	my $upgrd_nbr_cmd = "/usr/sbin/pkg version -l '<' | /usr/bin/wc -l | /usr/bin/awk '{print \$1}'";
	if (system(
			'/usr/local/bin/doas',
			'/usr/sbin/pkg',
			'update'
		) != 0) {
		exit;
	}
	if (system(
			'/usr/local/bin/doas',
			'/usr/sbin/portsnap',
			'fetch',
			'update'
		) != 0) {
		exit;
	}
	print colored("\nPorts to be updated: ", 'bold');
	my $upgrd_nbr = `$upgrd_nbr_cmd`;
	chomp $upgrd_nbr;
	print colored($upgrd_nbr, 'bold green') . "\n";
	open(PS, "/usr/sbin/pkg version -l '<' |");
	while (<PS>) {
		print
	}
	close(PS);
	if ($upgrd_nbr == 0) {
		print colored("No ports to be updated.\n", 'bold green');
		exit;
	}
	print colored("\nUpgrade these ports? ", 'bold yellow')
		. colored('[', 'bold green')
		. colored('y', 'bold red')
		. colored('/N', 'bold green')
		. colored("]\n", 'bold green')
		. colored("~> ", 'yellow');
	my $answer = <STDIN>;
	chomp $answer;
	if ($answer ne "y" && $answer ne "Y") {
		print "Exiting...\n";
		exit;
	}
	system('/usr/local/bin/dash',
		   '-c',
		   '/usr/local/bin/notify-send "Initiating upgrade" "Ports upgrade has started\nTotal: ' . $upgrd_nbr . ' to be updated" >/dev/null 2>&1');
	if (system('/usr/local/bin/dash',
			   '-c',
			   '/usr/local/bin/doas /usr/local/sbin/portmaster -dya --no-confirm') == 0) {
		my $failed_nbr = `$upgrd_nbr_cmd`;
		chomp $failed_nbr;
		my $diff_nbr = $upgrd_nbr - $failed_nbr;
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/local/bin/notify-send "Upgrade complete!" "Ports upgrade installed successfully\nTotal: ' . $diff_nbr . ' installed" >/dev/null 2>&1');
	}
	else {
		my $failed_nbr = `$upgrd_nbr_cmd`;
		substr($failed_nbr, -1) = "";
		my $diff_nbr = $upgrd_nbr - $failed_nbr;
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/local/bin/notify-send -u critical -t 10000 "Upgrade failed!" "Some ports failed to compile\nTotal: ' . $diff_nbr . ' installed - ' . $failed_nbr . ' failed" >/dev/null 2>&1');
	}
	exit;
}

sub configport {
	my $port = $_[0];
	my $port_basename = $_[1];
	if (system(
			'/usr/local/bin/doas',
			'/usr/bin/make',
			'config-recursive') == 0) {
		if (system(
				'/usr/local/bin/doas',
				'/usr/bin/make',
				'config-recursive') == 0) {
			if (system(
					'/usr/local/bin/doas',
					'/usr/bin/make',
					'config-recursive') == 0) {
			}
			system(
				'/usr/local/bin/notify-send',
				'Configuration success!',
				'Ports config - ' . $port . ' - configured successfully',
				);
		}
	}
	else {
		system(
			'/usr/local/bin/notify-send',
			'-u',
			'critical',
			'-t',
			'10000',
			'Configuration failure!',
			'Ports config - ' . $port . ' - failed to configure'
			);
	}
}

sub installport {
	my $port = $_[0];
	my $port_basename = $_[1];
	if (system(
			'/usr/local/bin/doas',
			'/usr/bin/make',
			'install',
			'clean'
		) == 0) {
		system(
			'/usr/local/bin/notify-send',
			'Compilation success!',
			'Ports - ' . $port . ' - installed successfully'
			);
	}
	else {
		system(
			'/usr/local/bin/notify-send',
			'-u',
			'critical',
			'-t',
			'10000',
			'Compilation failure!',
			'Ports - ' . $port . ' - failed to install'
			);
	}
}

sub main {
	my $argc = $#ARGV + 1;
	if ($argc == 0) {
		print colored("Failed!\n", 'bold red')
			. "No port or argument specified\n";
		exit 1;
	}
	my $port = $ARGV[0];
	if ($ARGV[0] eq "upgrade") {
		upgrdports();
	}
	elsif (-d "/usr/ports/" . $port) {
		my $port_basename = `echo $ARGV[0] | awk -F '/' '{print \$2}'`;
		print colored("Port ", 'bold')
			. colored($port, 'bold green')
			. colored(" found\n", 'bold');
		chdir "/usr/ports/" . $port;
		configport($port, $port_basename);
		installport($port, $port_basename);
	}
	else {
		print colored("Failed!\n", 'bold red')
			. "Port "
			. colored($port, 'bold yellow')
			. " doesn't exist in "
			. colored("/usr/ports/\n", 'bold');
	}
	exit;
}

main();

__END__