#!/usr/bin/perl
#
# ClamTk, copyright (C) 2004-2024 Dave M
#
# This file is part of ClamTk
# https://github.com/dave-theunsub/clamtk/
# https://gitlab.com/dave_m/clamtk/
#
# ClamTk is free software; you can redistribute it and/or modify it
# under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
# Foundation; either version 1, or (at your option) any later version, or
#
# b) the "Artistic License".
#

# use strict;
# use warnings;
use utf8;
$| = 1;

use ClamTk::Analysis;
use ClamTk::App;
use ClamTk::Assistant;
use ClamTk::GUI;
use ClamTk::History;
use ClamTk::Icons;
use ClamTk::Network;
use ClamTk::Prefs;
use ClamTk::Results;
use ClamTk::Scan;
use ClamTk::Schedule;
use ClamTk::Settings;
use ClamTk::Shortcuts;
use ClamTk::Startup;
use ClamTk::Update;
use ClamTk::Quarantine;
use ClamTk::Whitelist;

use Encode 'decode';
use Locale::gettext;
use POSIX 'locale_h';
textdomain( 'clamtk' );
setlocale( LC_ALL, '' );

# This seems horrible, but it works.
setlocale( LC_TIME, 'C' );
bind_textdomain_codeset( 'clamtk', 'UTF-8' );

my $arg = decode( 'utf8', $ARGV[ 0 ] );
#
# Unfortunately, I don't know how to make python keep a
# URI with spaces intact.  So....
# my $arg = '';
# $arg = join( ' ', @ARGV );

# Might be in the Trash
# // = entire directory
my $trash_dir = ClamTk::App->get_path( 'trash_dir' );
if ( $arg eq '//' ) {
    # The whole "Trash" directory
    $arg = $trash_dir;
} elsif ( $arg =~ m#^//(.*?)$# ) {
    # individual file
    my $trash_dir_files = ClamTk::App->get_path( 'trash_dir_files' );
    if ( -e "$trash_dir_files/$1" ) {
        $arg = "$trash_dir_files/$1";
    }
}

# Ensure all the normal directories are created
ClamTk::Prefs->structure;

# Ensure the preferences are normalized
# Create defaults if they do not exist
ClamTk::Prefs->custom_prefs;

# If we get no arguments, bring up GUI;
# otherwise, we're scanning.
# Let Scanner know this is a commandline scan, so
# if we can't scan (e.g. due to permissions), exit
if ( !$arg or !-e $arg ) {
    ClamTk::GUI->start_gui();
} else {
    ClamTk::Scan->filter( $arg, 1, 'startup' );
}

# End.
