#!/usr/bin/perl
use Time::HiRes qw (sleep);
use Term::ReadKey;
use strict;

ReadMode 'noecho';
$| = 1;

our $login      = "lnykryn";
our $hostname   = "notas";
our $dir        = "~";
our $type_sleep = 0.04;

our $root       = 1;


sub pause {
    <STDIN>;
}

sub print_prompt {
        print '['. ($root ? "root" : $login) .'@'.$hostname.' '.$dir.']'. ($root ? '#' : '$').' ';
}


sub terminal {
        my @letters = split //, $_[0];
        my $letter;
        my $wait = defined $_[1] ? $_[1] : 0;
        my $newline = defined $_[2] ? $_[2] : 1;
        my $sleep = defined $_[3] ? $_[3] : $type_sleep;
        print_prompt();
        pause if $wait == 1 || $wait == 3;
        foreach $letter (@letters) {
                print $letter;
                sleep $sleep;
        }
        pause if $wait == 2 || $wait == 3;
        if ($newline) {
                sleep 0.5;
                print "\n";
        }
}

sub figlet {
        system("figlet $_[0]");
}

sub clear_screen {
        my $prompt = defined $_[0] ? $_[0] : 1;
        terminal("clear", 1) if $prompt;
        print "\033[2J";
        print "\033[0;0H";
}

sub terminal_do {
        terminal @_;
        system($_[0]);
}

goto USE if $ARGV[0] eq "use";
goto TIMER if $ARGV[0] eq "timer";
goto PATH if $ARGV[0] eq "path";
goto SOCKET if $ARGV[0] eq "socket";
goto JOURNAL if $ARGV[0] eq "journal";
goto NSPAWN if $ARGV[0] eq "nspawn";
goto KONEC if $ARGV[0] eq "konec";

clear_screen 0;
$root=0;
figlet "SYSTEMD" ;
print "\tSysV times one hundred! \n\n\n";
terminal "getent passwd `whoami` | cut -d ':' -f 5";
print "Lukáš Nykrýn <lnykryn\@redhat.com>\n";


clear_screen;
$root=1;
terminal_do "readlink /sbin/init";

clear_screen;
terminal "systemctl stop cups";
terminal "systemctl start cups";
terminal_do "systemctl status cups";
terminal_do "systemctl list-units --no-pager --no-legend --failed", 1;
clear_screen;

terminal_do "loginctl --no-pager";
clear_screen;

terminal "systemctl set-property httpd.service CPUShares=600 MemoryLimit=500M";
#terminal_do "systemd-cgls";


USE:
clear_screen;
terminal "echo \$ZAKLADNI_OTAZKA";
print "Tohle mě nezajímá. K čemu mi to je?\n";
clear_screen;
terminal_do "cat /etc/rss-list";
terminal_do "less /usr/bin/rsscheck", 3;
terminal_do "less /usr/lib/systemd/system/rsscheck.service", 3;
terminal_do "systemctl start rsscheck.service";
terminal_do "less /var/lib/rss/storage", 3;
terminal_do "systemctl list-dependencies rsscheck.service", 3;


TIMER:
clear_screen;
terminal_do "less /usr/lib/systemd/system/rsscheck.timer", 3;
terminal_do "systemctl start rsscheck.timer";
terminal_do "systemctl status rsscheck.timer";
terminal_do "sleep 10; systemctl status rsscheck.service";
terminal_do "systemctl stop rsscheck.timer", 1;

PATH:
clear_screen;
terminal_do "less /usr/lib/systemd/system/rsscheck.path", 2;
terminal_do "systemctl start rsscheck.path";
terminal_do "systemctl status rsscheck.path";
terminal_do "echo 'http://broken/' > /etc/rss-list", 1;
terminal_do "systemctl status rsscheck.service";

system "systemctl stop rsscheck.timer";
system "systemctl stop rsscheck.path";
system "systemctl stop rssremote.socket";
system 'cp /home/lnykryn/.rss/list /etc/rss-list';
system 'sed --in-place \'/^.*broken.*$/d\' /etc/rss-list';

SOCKET:
clear_screen;
terminal_do "less /usr/bin/rssremote", 3;
terminal_do 'less /usr/lib/systemd/system/rssremote@.service', 2;
terminal_do 'less /usr/lib/systemd/system/rssremote.socket', 2;
terminal_do "systemctl start rssremote.socket";
terminal_do "systemctl status rssremote.socket";
terminal_do 'echo \'http://broken2/\' | ncat localhost 8888', 3;
terminal_do "cat /etc/rss-list", 2;

system "systemctl stop rsscheck.timer";
system "systemctl stop rsscheck.path";
system "systemctl stop rssremote.socket";
system 'cp /home/lnykryn/.rss/list /etc/rss-list';
system 'sed --in-place \'/^.*broken.*$/d\' /etc/rss-list';


JOURNAL:
clear_screen;
terminal_do "journalctl -b -r", 3;
terminal_do "journalctl --output verbose _SYSTEMD_UNIT=rsscheck.service PRIORITY=1", 3;
terminal_do "journalctl -u rsscheck PRIORITY=1 PRIORITY=5", 3;

NSPAWN:
clear_screen;
terminal "yum -y --releasever=rawhide --nogpg --installroot=/srv/nspawn-container --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release vim-minimal perl";
terminal "mkdir -p /srv/nspawn-container/var/lib/rss/storage";
terminal "cp /usr/lib/systemd/system/*rss* /srv/nspawn-container/usr/lib/systemd/system/";
terminal "cp /etc/rss-list /srv/nspawn-container/etc/";
terminal "cp /usr/bin/rss* /srv/nspawn-container/usr/bin/";
terminal "systemctl --root /srv/nspawn-container/ enable rsscheck.timer";
terminal_do "systemd-nspawn -bD /srv/nspawn-container/", 3;

KONEC:
clear_screen;
print "
* http://www.freedesktop.org/wiki/Software/systemd/‎ \n
* #systemd on irc.freenode.org \n
* http://lists.freedesktop.org/mailman/listinfo/systemd-devel \n
* git://anongit.freedesktop.org/systemd/systemd \n\n\n
";


ReadMode 'normal';
