#!/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 { my $ret = system( DASH_PATH, '-c', $_[0] ); return $ret; } 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 = ) { 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__