#!/usr/bin/perl

# Copyright (C) 2011 Thorsten Kukuk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# in Version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.


=head1 NAME

geo-gclogin - login to geocaching.com

=head1 SYNOPSIS

geo-gclogin [options]

=head1 DESCRIPTION

geo-gclogin tries to login to www.geocaching.com and creates
a cookie to speed up other geo-* tools.

=head1 OPTIONS

  -u|--user <account>       geocaching.com account
  -p|--password <password>  Password for geocaching.com
  --logout                  Logout from geocaching.com and destroy cookie
  --version                 Print version and exit
  --usage                   Print usage
  --man                     Display manual page
  -h|-?                     Help

=cut

use strict;
use warnings;
use Pod::Usage;
use GEO::GC;
use Config::IniFiles;

#
# process command line arguments
#
use Getopt::Long;
my $help = 0;
my $man = 0;
my $version = 0;
my $usage = 0;
my $dump_config = 0;
my $homedir = '';
my $logout = 0;
my $quiet = 0;

if ($ENV{HOME}) {
  $homedir = $ENV{HOME};
} elsif ($ENV{HOMEDRIVE} && $ENV{HOMEPATH}) {
  $homedir = $ENV{HOMEDRIVE}."/".$ENV{HOMEPATH};
}
my $cfgname = $homedir."/.geo-tools.cfg";

if (!-r $cfgname) {
  my $cfg = new Config::IniFiles(-default => "Global",
                                 -nocase => 1);
  $cfg->AddSection("Global");
  $cfg->newval("Global", "save_cookies", 1);
  $cfg->WriteConfig($cfgname);
}

my $cfg = new Config::IniFiles(-file => $cfgname,
                               -default => "Global");

my $save_cookies = $cfg->val("geo-gclogin", "save_cookies", 1);
my $sleep = $cfg->val("geo-gclogin", "sleep", 6);
my $gcuser = $cfg->val("geo-gclogin", "gcuser");
my $gcpassword = $cfg->val("geo-gclogin", "gcpassword");


GetOptions('u|user=s' => \$gcuser,
	   'p|password=s' => \$gcpassword,
	   'logout!' => \$logout,
	   'quiet!' => \$quiet,
	   'dump-config' => \$dump_config,
	   'version' => \$version,
	   'man' => \$man,
	   'usage' => \$usage,
	   'help|h|?' => \$help) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage(-exitstatus => 0, -verbose => 0) if $usage;

if ($version) {
  print "geo-gclogin (geo-tools) 1.23\n";
  exit;
}

if ($dump_config) {
  WriteConfig();
  exit;
}

my $gc = new GEO::GC(save_cookies => $save_cookies, sleep => $sleep,
                     quiet => $quiet,
                     account => $gcuser, password => $gcpassword);

if ($logout) {
  $gc->random_sleep($gc->sleep);
  $gc->logout();
} else {
  $gc->login($gcuser, $gcpassword);
}


exit;

sub WriteConfig
{
  $cfg->newval("Global", "gcuser", $gcuser);
  $cfg->newval("Global", "gcpassword", $gcpassword);
  $cfg->newval("Global", "logout", $logout);
  $cfg->newval("Global", "sleep", $sleep);
  $cfg->newval("Global", "save_cookies", $save_cookies);

  $cfg->WriteConfig($cfgname);
}
