package System::Introspector::MountPoints;
use Moo;

sub gather {
    my ($self) = @_;
    return {
        mtab  => $self->_parse_tab_fh($self->_open_fh('/etc/mtab')),
        fstab => $self->_parse_tab_fh($self->_open_fh('/etc/fstab')),
    };
}

sub _open_fh {
    my ($self, $file) = @_;
    open my $fh, '<', $file
        or die "Unable to open $file: $!\n";
    return $fh;
}

sub _parse_tab_fh {
    my ($self, $fh) = @_;
    my @mounts;
    while (defined( my $line = <$fh> )) {
        chomp $line;
        next if $line =~ m{^\s*$}
             or $line =~ m{^\s*#};
        my ($device, $point, $type, $opt, $dump, $pass)
            = split m{\s+}, $line;
        push @mounts, {
            device_name => $device,
            mount_point => $point,
            fs_type     => $type,
            dump_freq   => $dump,
            pass_num    => $pass,
            options     => {
                map {
                    my ($name, $value) = split m{=}, $_, 2;
                    $value = 1
                        unless defined $value;
                    ($name => $value);
                } split m{,}, $opt,
            },
        };
    }
    return \@mounts;
}

1;
