#!/usr/bin/perl -w
# exparse will disassemble /etc/exports for editing,
# then reassemble into proper format

use strict;
use Getopt::Std;

my $version="1.0";
my $exportfile="/etc/exports";
my $tmpexport="exparse.out";
my $newexport="exports.new";
my ($newln,%opts,$vb);

getopts('f:uo:hv', \%opts)||die "exparse [-f export_file | -u | -o parsed_file] [-h] [-v]\nbad args, try -h for help\n";
if ($opts{h}) {usage()}
if ($opts{v}) {$vb="true";print "exparse starting, version $version"}
if (($opts{f} && $opts{u})||($opts{f} && $opts{o})||($opts{u} && $opts{o})) {
        die "specify -f or -u or -o separately\ntry -h for usage\n";
}
if ($opts{u}) {
        if ($vb) {print "performing unparse on program default $tmpexport\n"}
        unparse($tmpexport);
}
if ($opts{o} && -r $opts{o}) {
        if ($vb) {print "performing unparse on $opts{o} supplied from command line\n"}
        unparse($opts{o});
} elsif ($opts{o}) {die "file $opts{o} failed read test\n"}
if ($opts{f} && -e $opts{f}){
        if ($vb) {print "performing parse on $opts{f} supplied from command line\n"}
        parse($opts{f});
} elsif ($opts{f}) {
        die "file $opts{f} not found\n";
} else {
        if ($vb) {print "performing parse on program default $exportfile\n"}
        parse($exportfile);
}
sub parse {
my $infile=$_[0];
open(EXFIL,$infile)||die "OOPS opening original file: [$!]";
open(TMPPA,">$tmpexport")||die "OOPS on creating parsed file: [$!]";
while (<EXFIL>) {
        s/\n/\n##########\n/;
        s/ /\n/;
        s/=/=\n/g;
        s/:/\n/g;
        print TMPPA;
}
close(EXFIL);
close(TMPPA);
if ($vb) {print "created $tmpexport for editing\n"}
exit 0;
}
sub unparse {
my $infile=$_[0];
open(TMPIN,$infile)||die "OOPS on open of file $infile: [$!]";
open(NEWEX,">$newexport")||die "oops on creating unparsed file $newexport: [$!]";
while (<TMPIN>) {
        chomp;
        $newln .= "$_:";
}
$newln =~ s/:##########:(.)/\n$1/g;
$newln =~ s/(\/[a-z]+):/$1 /g;
$newln =~ s/=:/=/g;
$newln =~ s/::/:/g;
$newln =~ s/:##########://;
print NEWEX "$newln\n";
close(TMPIN);
close(NEWEX);
if ($vb) {print "created $newexport in exports-format\n"}
exit 0;
}
sub usage {
print "exparse, version $version\nUsage: exparse [-f export_file | -u] [-o parsed_file] [-h] [-v]\n";
print "-f export_file\n\tspecify a file to parse, default is /etc/exports\n";
print "\tcreates parsed version of export-format file named exparse.out\n";
print "-u\n\tunparse a file into exports-formated file exports.new\n";
print "\tdefault filename is exparse.out\n";
print "-o parsed_file\n\tunparse parsed_file into exports-formated file exports.new\n";
print "-v\n\tturns on some messages\n";
print "-h\n\tprint this help and exits\n\nExamples:\nexparse\n\tparses /etc/exports into ./exparse.out\n\n";
print "exparse -f/dir/subdir/myexportfile\n\tparses /dir/subdir/myexportfile into ./exparse.out\n\n";
print "exparse -vf/dir/subdir/myexportfile\n\tsame thing but with informational messages\n\n";
print "exparse -u\n\tunparses ./exparse.out into exports-formatted file named ./exports.new\n\n";
print "exparse -o/dir/subdir/myfile\n\tunparses /dir/subdir/myfile into exports-formatted file named ./exports.new\n\n";
print "exparse -vo/dir/subdir/myfile\n\tsame thing but with informational messages\n\n";
exit 0;
}