#!/usr/bin/perl -w

use strict;

my $sircam = shift or die "usage: $0 filename.doc.foo\n";

my $dir;
my $base;
my $doc;
my $exe;

(($dir, $base, $doc, $exe) = $sircam =~ m#^(.*/)?([^/]+)\.([^\./]+)\.([^\./]+)$#) or die "unable to parse filename $sircam\n";

$doc eq 'doc' or die "only able to handle .doc files\n";

# sircam attaches the file starting at offset 137216
open(IN, "<$sircam") or die "unable to open $sircam for reading: $!\n";
seek(IN, 137216, 0) or die "unable to seek in $sircam to magic 137216 offset\n";

open(OUT, ">$base.doc") or die "unable to open $base.doc for writing: $!\n";

# copy the tail of the input file
print(OUT <IN>) or die "error writing: $!\n";

close(OUT);
close(IN);

open(STDOUT, ">$base.html") or die "unable to redirect stdout to $base.html: $!\n";
system("wvHtml", "-d", "$base.dir", "$base.doc");

unlink("$base.doc");
