#!/usr/bin/env perl
#
# smingest was written by Omar Polo <op@openbsd.org> and is placed in the
# public domain.  The author hereby disclaims copyright to this source
# code.

use strict;
use warnings;
use v5.32;
use utf8;

use Date::Parse;
use File::Basename;

die "usage: $0 dbpath\n" if @ARGV != 1;
my $dbpath = shift @ARGV;

open(my $sqlite, "|-", "sqlite3", $dbpath) or die "can't spawn sqlite3";

if (`uname` =~ "OpenBSD") {
	use OpenBSD::Pledge;
	use OpenBSD::Unveil;

	unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
	pledge("stdio proc exec") or die "pledge: $!";
}

say $sqlite ".import --csv /dev/stdin email"
    or die "can't speak to sqlite: $!";

while (<>) {
	chomp;

	open(my $fh, "-|", "mshow", "-Atext/plain", "-NF", $_)
	    or die "can't run mshow $_: $!";

	my ($time, $id) = split /\./, basename $_;
	my $mid = "$time.$id";
	$mid =~ s/"/""/g;

	my ($from, $subj, $date) = ('', '', undef);
	while (<$fh>) {
		chomp;
		last if /^$/;
		s/"/""/g;
		$from = s/.*?: //r if /^From:/;
		$subj = s/.*?: //r if /^Subject:/;
		$date = str2time(s/.*?: //r) if /^Date:/;
	}
	$date //= time;
	$from =~ s/ +<.*>//;

	print $sqlite "\"$mid\",\"$from\",\"$date\",\"$subj\",\"";
	while (<$fh>) {
		s/"/""/g;
		print $sqlite $_;
	}
	print $sqlite "\"\r\n";

	close $fh;
}

close $sqlite;
die "sqlite3 exited with $?\n" unless $? == 0;
