summaryrefslogtreecommitdiffstats
path: root/.local/bin/system-upgrade
blob: b8fbcaf6e42c3eaccc9826ca87aa20d0f284c38f (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/local/bin/perl

use warnings;
use strict;
use Term::ANSIColor;
use File::Tee qw(tee);
use Net::Ping;
use File::Basename;
use Capture::Tiny qw(capture);

use constant DEFAULT_USER	=> 'jozan';
use constant {
	DASH_PATH			=> '/usr/local/bin/dash',
	SU_PATH				=> '/usr/bin/su',
	GREP_PATH			=> '/usr/bin/grep',
	WC_PATH				=> '/usr/bin/wc',
	TR_PATH				=> '/usr/bin/tr',
	TEE_PATH			=> '/usr/bin/tee',
	FREEBSD_UPDATE_PATH	=> '/usr/sbin/freebsd-update',
	PKG_PATH			=> '/usr/sbin/pkg',
	PORTSNAP_PATH		=> '/usr/sbin/portsnap',
	PORTMASTER_PATH		=> '/usr/local/sbin/portmaster',
	ESPEAK_PATH			=> '/usr/local/bin/espeak',
	NOTIFY_PATH			=> '/usr/local/bin/notify-send'
};
use constant {
	STDOUT_CAPTURE	=> 0,
	STDERR_CAPTURE	=> 1,
	EXIT_CAPTURE	=> 2
};

sub user_shell {
	my $ret = system(
		DASH_PATH,
		'-c',
		SU_PATH . ' ' . DEFAULT_USER . ' << EOF
' . $_[0] . ' >/dev/null 2>&1
EOF'
		);
	return $ret;
}

sub fbsd_update {
	my $output;
	print "
+----------------+
|                |
| FreeBSD update |
|                |
+----------------+\n\n";
	user_shell(NOTIFY_PATH . ' "Fetching" "Fetching FreeBSD updates"');
	user_shell(ESPEAK_PATH . ' "Initializing. Fetching FreeBSD updates" &');
	open(
		PS,
		DASH_PATH . ' -c "' .
		FREEBSD_UPDATE_PATH . ' fetch" |'
		);
	while (my $read = <PS>) {
		print $read;
		$output .= $read;
	}
	close(PS);
	if (!($output =~ m/No updates needed/)) {
		user_shell(NOTIFY_PATH . ' "Installing" "Installing FreeBSD updates"');
		user_shell(ESPEAK_PATH . ' "Installing FreeBSD updates" &');
		system(
			FREEBSD_UPDATE_PATH,
			'install'
			);
	}
	return;
}

sub ports_update {
	print "
+----------------+
|                |
|  ports update  |
|                |
+----------------+\n\n";
	user_shell(NOTIFY_PATH . ' "Refreshing repos" "Updating pkg and ports repositories"');
	user_shell(ESPEAK_PATH . ' "Updating pkg and ports repositories" &');
	system(
		PKG_PATH,
		'update'
		);
	system(
		PORTSNAP_PATH,
		'fetch',
		'update'
		);
	user_shell(NOTIFY_PATH . ' "Listing ports" "Listing ports in need for update, this may take a while..."');
	user_shell(ESPEAK_PATH . ' "Listing ports in need for update, please stand by" &');
	print "Listing ports in need for update, this may take a while...\n";
	my @pre_list = capture{
		system(
			PORTMASTER_PATH,
			'-L'
			);
	};
	my @pre_array = split("\n", $pre_list[STDOUT_CAPTURE]);
	my @pre_amount_array = grep(/new\ version/, @pre_array);
	my $pre_amount;
	foreach my $i (@pre_amount_array) {
		$pre_amount .= $i;
	}
	$pre_amount =~ s/\D//g;
	print colored("\nPorts to be updated: ", 'bold') . colored($pre_amount, 'bold green') . "\n";
	my @update_array;
	foreach my $i (@pre_array) {
		if ($i =~ /New\ version\ available/) {
			$i =~ s/.+?(?=:).+\s+//;
			$i .= "\n";
			push @update_array, $i;
		}
	}
	print @update_array;
	user_shell(NOTIFY_PATH . ' "Answer needed" "Do you whish to update these ' . $pre_amount . ' ports?"');
	user_shell(ESPEAK_PATH . ' "Listing complete. Should I upgrade these ' . $pre_amount . ' ports?" &');
	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;
	}
	user_shell(NOTIFY_PATH . ' "Initiating upgrade" "Ports upgrade has started\nTotal: ' . $pre_amount . ' to be updated"');
	if ($pre_amount == 1) {
		user_shell(ESPEAK_PATH . ' "Initiating ' . $pre_amount . ' port upgrade. Configuration might be needed before compilation." &');
	}
	else {
		user_shell(ESPEAK_PATH . ' "Initiating ' . $pre_amount . ' ports upgrade. Configuration might be needed before compilation." &');
	}
	if (
		system(
			PORTMASTER_PATH,
			'-dya',
			'--no-confirm'
		) == 0
		) {
		user_shell(NOTIFY_PATH . ' "Upgrade complete!" "Ports upgrade installed successfully\nTotal: ' . $pre_amount . ' installed"');
		user_shell(ESPEAK_PATH . ' "Success: ' . $pre_amount . ' ports installed successfully" &');
	}
	else {
		print "Some ports failed. Listing ports in need for update, this may take a while...\n";
		user_shell(NOTIFY_PATH . ' -u critical -t 15000 "Failure!" "One or several ports failed to build"');
		user_shell(ESPEAK_PATH . ' "Failure: one or several ports failed to install. Listing ports in need for update, please stand by" &');
		my @post_list = capture{
			system(
				PORTMASTER_PATH,
				'-L'
				);
		};
		my @post_array = split("\n", $post_list[STDOUT_CAPTURE]);
		my @post_amount_array = grep(/new\ version/, @post_array);
		my $post_amount;
		foreach my $i (@post_amount_array) {
			$post_amount .= $i;
		}
		$post_amount =~ s/\D//g;
		my $diff_nbr = $pre_amount - $post_amount;
		print . "\n"
			. colored($diff_nbr, 'bold green')
			. ' were updated, '
			. colored($post_amount, 'bold yellow') . " ports still need to be updated.\n";
		user_shell(NOTIFY_PATH . ' -u critical -t 15000 "Summary" "Upgraded - ' . $diff_nbr . '\nRemaining - ' . $post_amount . '"');
		user_shell(ESPEAK_PATH . ' "Failure: not all ports were upgraded - ' . $diff_nbr . ' upgraded - ' . $post_amount . ' remaining. Listing ports in need for update, please stand by" &');
	}
	return;
}

sub clean_exit {
	my $end_date = `/bin/date`;
	chomp($end_date);
	print "\n\n" . $end_date . ": system upgrade complete!\n";
	exit;
}

sub main {
	my $p = Net::Ping->new;
	if (!($p->ping('freebsd.org', 2))) {
		print STDERR colored(basename($0) . ":" , 'bold') . colored(" failure:", 'bold red') . " you seem not connected to the internet.\n";
		user_shell(NOTIFY_PATH . ' -u critical -t 10000 "Failure" "Internet connection required"');
		user_shell(ESPEAK_PATH . ' "Failure: internet connection required" &');
		exit 1;
	}
	my $user = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
	if ($user ne 'root') {
		print STDERR colored(basename($0) . ":" , 'bold') . colored(" failure:", 'bold red') . " insufficent privileges, needs to be run as root.\n";
		user_shell(NOTIFY_PATH . ' -u critical -t 10000 "Failure" "root privileges required"');
		user_shell(ESPEAK_PATH . ' "Failure: root privileges required" &');
		exit 2;
	}
	fbsd_update();
	ports_update();
	clean_exit();
}

main();

__END__