#!/usr/bin/env perl
# cookies-sql2txt --- generate cookies.txt from sqlite index
# Author: Noah Friedman <friedman@splode.com>
# Created: 2009-02-10
# Public domain

# $Id: cookies-sql2txt,v 1.3 2019/06/07 19:00:50 friedman Exp $

# Commentary:

# Firefox 3.0 and later use a SQLite database for storing cookies.
# Other web fetching tools such as wget still use the traditional Netscape
# cookies.txt file.  This program will generate the latter from the former.

# Code:

use strict;
use warnings qw(all);

use DBI;

sub main
{
  my ($cookiedb) = @_;
  unless (defined $cookiedb)
    {
      (my $prog = $0) =~ s=.*/==;
      print STDERR "Usage: $prog [cookies.sqlite]\n";
      return 1;
    }

  my $dbh = DBI->connect ("DBI:SQLite:dbname=$cookiedb") or return 1;

  $dbh->do (q{ pragma busy_timeout = 500 });

  my $stm = q{ select host,
                      replace(replace(substr(host,1,1)='.',1,'TRUE'),0,'FALSE') as flag,
                      path,
                      isSecure,
                      expiry,
                      name,
                      value
               from moz_cookies
               where expiry >= ?
               order by baseDomain,
                        host,
                        name,
                        path
            };
  my $sth = $dbh->prepare ($stm) or return 1;
  $sth->execute (time);
  while (my @row = $sth->fetchrow_array)
    {
      print join( "\t", @row ), "\n";
    }
  return 0;
}

exit (main (@ARGV));

# eof