#!/usr/bin/perl

use strict;

my $cookie_jar = shift || "@prefix@/share/cookie/cookie-dough";
my $cookie = "";

# Eat the headers
while (<>) 
{
  if (/^\n$/) {
    last;
  }
}

# Now get the body of the message -- the cookie
while (<>) {
  $cookie .= $_;
}

# Quote backslashes for cookie dough format
$cookie =~ s/\\/\\\\/g;

open (COOKIEJAR, ">>${cookie_jar}") 
  or die ("unable to open ${cookie_jar} ($!)");

print COOKIEJAR "\\${cookie}\n";

close (COOKIEJAR);
