summaryrefslogtreecommitdiffstats
path: root/.local/bin/system-upgrade
blob: bbf936380f3a5e584962bb3f147fa62d238fcad9 (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
#!/usr/local/bin/perl

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

use constant DEFAULT_USER	=> 'jozan';
use constant LOG_FILE		=> '/var/log/system-upgrade.log';
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',
	PORTMASTER_PATH		=> '/usr/local/sbin/portmaster',
	ESPEAK_PATH			=> '/usr/local/bin/espeak',
	NOTIFY_PATH			=> '/usr/local/bin/notify-send',
};

sub root_shell {
	system(
		DASH_PATH,
		'-c',
		$_[0]
		);
	return;
}

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

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" &');
		root_shell(FREEBSD_UPDATE_PATH . ' install');
	}
	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(ESPEAK_PATH . ' "Failure: internet connection required" &');
		exit 1;
	}
	open(my $fh, '>>:encoding(UTF-8)', LOG_FILE);
	print $fh "\n\n===============\nBegining update: " . `/bin/date` . "\n";
	close($fh);
	tee(STDOUT, '>>', LOG_FILE);
	fbsd_update();
	clean_exit();
}

main();

__END__