package Ifweb::Ifupdown;

use strict;
use warnings;

use Dancer ':syntax';

my $ifstate_file = '/etc/network/run/ifstate';
my $interfaces_file = '/etc/network/interfaces';
my $iface_dir = '/sys/class/net';

sub ifstate {
    my @state = ();
    my $id = 0;

    open(my $ifstate, $ifstate_file);
    while (<$ifstate>) {
        chomp;
        next if /^$/;
        my ($phy, $log) = split(/=/);
        push @state, {phy => $phy, log => $log, id => $id};
        $id++;
    }
    close($ifstate);

    return \@state;
}

sub ifaces {
    opendir(my $dir, $iface_dir);
    my @ifaces = readdir($dir);
    closedir($dir);
    @ifaces = grep {$_ ne '.' and $_ ne '..'} @ifaces;

    foreach my $ignore (@{config->{ignored_ifaces}}) {
        print STDERR $ignore, "\n";
        @ifaces = grep(!/$ignore/, @ifaces);
    }

    return \@ifaces;
}

sub networks {
    my @networks = ();
    my $network = undef;
    my $id = 0;

    open(my $interfaces, $interfaces_file);
    while (<$interfaces>) {
        chomp;
        s/#.*$//;
        next if /^\s*$/;
        if (/\s*iface/) {
            if (defined $network) {
                push @networks, $network;
            }
            my ($dummy, $name, $family, $method) = split;
            $network = {
                name    => $name,
                family  => $family,
                method  => $method,
                id      => $id,
                details => $_ . "\n",
            };
            $id++;
        }
        else {
            if (defined $network) {
                /\s*(\S+)\s+(.*)$/;
                $network->{$1}      = $2;
                $network->{details} = $network->{details} . $_ . "\n";
            }
        }
    }
    push @networks, $network;
    close($interfaces);

    foreach (@networks) {
        if ($_->{method} eq "loopback") {
            $_->{guessed_iface} = "lo";
        }
        elsif (grep(/(wpa-|wireless-).*/, keys %{$_})) {
            $_->{guessed_iface} = "wlan0";            
        }
        else {
            $_->{guessed_iface} = "-";
        }
    }

    return \@networks;
}

sub up {
    my ($phy, $log) = @_;
    my $last;

    open(my $ifup, "ifup -v $phy=$log 2>&1 |");
    while (<$ifup>) {
        chomp;
        print STDERR $_, "\n";
        $last = $_;
    }
    close($ifup);

    if ($last =~ /Failed|Ignoring/) {
        return false;
    }
    else {
        return true;
    }
}

sub down {
    my $phy = shift;
    debug "ifdown $phy";
    system("ifdown -v $phy");
}

sub config_template {
    my ($cell, $name, $key) = @_;
    my $content = "iface $name inet dhcp\n";

    if ($cell->{mode} eq 'Ad-Hoc') {
        $content .= "      wireless-mode  Ad-Hoc\n";
    }

    if ($cell->{encryption} eq 'Off') {
        $content .= "      wireless-essid ". $cell->{ESSID}. "\n";
    }
    else {
        if ($cell->{encryption} eq 'WEP') {
            $content .= "      wireless-essid ". $cell->{ESSID}. "\n";
            $content .= "      wireless-key   $key\n";
        }
        else {
            if ($cell->{encryption} eq 'WPA-PSK') {
                $content .= "      wpa-ssid ". $cell->{ESSID}. "\n";
                $content .= "      wpa-psk  $key\n";
            }
        }
    }

    return $content;
}

true;
