#!/usr/bin/perl

use strict;
use Getopt::Long;
use JSON;
use Lemonldap::NG::Common::FormEncode;
use Lemonldap::NG::Portal;
use Lemonldap::NG::Portal::Main::Constants qw(portalConsts PE_FIRSTACCESS);
my $exitCode = 0;
my %opts;
my $p;

my $req;

sub usage {
    print STDERR "Tool to search for a user

Usage:
`$0 <options> <username>`

Options:
 -v, --version: display version
 -h, --help:    display this
 -d, --debug:   enable debug mode
 -y, --yaml:    output in YAML mode
 -f, --field:   field to use (default: uid)
 --loglevel:    fix logLevel (default: error)
";
}

sub userSearch {
    my $uid = shift;
    $req->user($uid);
    $req->steps( [
            'getUser',        @{ $p->betweenAuthAndData },
            'setSessionInfo', $p->groupsAndMacros,
            'setLocalGroups',
        ]
    );
    $exitCode = $p->process($req);
    return $req->sessionInfo // ();
}

# MAIN

unless (
    GetOptions(
        \%opts,    'h|help',    'v|version', 'y|yaml',
        'd|debug', 'f|field=s', 'loglevel=s'
    )
  )
{
    $exitCode = 1;
    usage;
}

# HELP
elsif ( $opts{h} ) {
    usage;
}

# VERSION
elsif ( $opts{v} ) {
    print $Lemonldap::NG::Portal::VERSION. "\n";
}

# COMMANDS
else {
    $opts{f}        ||= 'uid';
    $opts{loglevel} ||= 'error';

    $p = Lemonldap::NG::Portal->new;
    my $findUserSearchingAttributes = { "$opts{f}##1" => 'Login' };
    $p->init( {
            logLevel                    => $opts{d} ? 'debug' : $opts{loglevel},
            findUser                    => 1,
            findUserSearchingAttributes => $findUserSearchingAttributes
        }
    );
    my $uid = $ARGV[0] or die 'Missing arg (string to search)';
    $req = Lemonldap::NG::Portal::Main::Request->new( {
            REQUEST_URI  => '/',
            REMOTE_ADDR  => '127.0.0.1',
            PATH_INFO    => '/',
            QUERY_STRING => build_urlencoded( $opts{f} => $uid ),
        }
    );
    $req->init( {} );
    $req->data->{_pwdcheck} = 1;
    my $res = $p->_userDB->findUser($req);

    my $resp = [];
    if ( !$res and $req->data->{findUserAll} and ref $req->data->{findUserAll} )
    {
        foreach my $id ( @{ $req->data->{findUserAll} } ) {
            $p->logger->info("Trying $id");
            push @$resp, userSearch($id);
        }
    }
    else {
        if ( $res eq PE_FIRSTACCESS ) {
            print STDERR
'findUser returned PE_FIRSTACCESS, did you forgot to set authChoiceAuthBasic?';
        }
        $p->logger->info('FindUser failed, trying direct login');
        $resp = [ userSearch($uid) ];
    }

    my $encoder;
    if ( $opts{y} ) {
        require YAML;
        $encoder = sub { YAML::Dump(@_); }
    }
    else {
        $encoder = sub { JSON->new->canonical->pretty->encode(@_); }
    }

    if ($exitCode) {
        print STDERR "Error " . portalConsts->{$exitCode} . " ($exitCode)\n";
    }
    print $encoder->($resp);
}
if ( $exitCode eq PE_FIRSTACCESS ) {
    print STDERR
'findUser returned PE_FIRSTACCESS, did you forgot to set authChoiceAuthBasic?';
}
exit $exitCode;
