#!/usr/bin/perl -w
# #
# Software subject to following license(s):
#   Apache 2 License (http://www.opensource.org/licenses/apache2.0)
#   Copyright (c) Responsible Organization
#

# #
# Current developer(s):
#   Luis Fernando Muñoz Mejías <Luis.Munoz@UGent.be>
#

# 
# #
# ccm, 16.2.0-rc2, rc2_1, 2016-02-17T15:34:50Z
#
#
# Initialise local config cache
#

BEGIN {
    # use perl libs in /usr/lib/perl
    unshift(@INC, '/usr/lib/perl');
}

use strict;
use warnings;
use Getopt::Long;
use File::Path;
use EDG::WP4::CCM::CCfg;

# Options
my $debug = undef;
my $config = undef;
my $cache_root = undef;
my $world_readable = 0;
my $group_readable = undef;

sub Quit
{
    my $msg = shift;

    if ($msg) {
        print STDERR "cache-initialise: $msg\n";
        exit(1);
    } else {
        exit(0);
    }
}

sub Warn
{
    my $msg = shift;

    print "[WARN] $msg \n";

    return 'ok';
}

sub Debug
{
    my $msg = shift;
    if (defined($debug)) {
        print $msg . "\n";
    }

    return 'ok';
}

sub Config
{
    EDG::WP4::CCM::CCfg::initCfg(shift);

    $cache_root = EDG::WP4::CCM::CCfg::getCfgValue('cache_root');
    $group_readable = EDG::WP4::CCM::CCfg::getCfgValue('group_readable');
    $world_readable = EDG::WP4::CCM::CCfg::getCfgValue('world_readable');
}


# Main program

# validate options
Quit("usage: cache-initialise [OPTIONS]
  -d, --debug          turn on debugging
  --config=FILE        location of fetch config file (for cache root)")
    unless (GetOptions('debug' => \$debug, 'config=s' => \$config)
            and scalar @ARGV == 0);

# process config file
Config($config);

# remove any existing cache
rmtree($cache_root, 0, 0);

# make directory structure
my $owner = 0;
my $gid;
my $mode = 0700;
my $mask = 077;

if ($group_readable) {
    $gid = getgrnam($group_readable);
    if(defined($gid)) {
        $mode = 0750;
        $mask = 027;
    } else {
        Warn("Invalid group name for group_readable $group_readable");
    };
};

if ($world_readable) {
    if($group_readable) {
        Warn("Both group_readable and world_readable are set, world_readable setting honoured.");
    }
    $mask = undef;
    $mode = 0755;
};

my @paths = ($cache_root, "$cache_root/data", "$cache_root/tmp");
foreach my $path (@paths) {
    if (-d $path) {
        # chmod returns number of changed files, croaks on error
        chmod $mode, $path;
    } else {
        my $ok = mkdir $path, $mode;
        die "Can't create $path: $!\n" unless $ok;
    }
    if (defined($gid)) {
        # use effective UID
        # chown returns number of changed files, croaks on error
        chown $>, $gid, $path;
    };
};

# make sure files are created so only
# root and possibly the group can see them
umask($mask) if $mask;


# make lock files
open TMP, ">$cache_root/global.lock";
print TMP "no\n";
close TMP;

Quit();

__END__

=head1 NAME

ccm-initialise - initialise local config cache

=head1 DESCRIPTION

Create an initial directory hierarchy for the local config cache, along
with the necessary lock files.

=head1 SYNOPSIS

ccm-initialise [I<OPTIONS>]

=head1 OPTIONS

=over 4

=item B<-d>, B<--debug>

Turn on debugging messages.

=item B<--config>=I<file>

The I<file> is the WP4 local cache configuration file, also used by the
B<fetch> program; cache-initialise uses it to establish the location of
the cache root directory.  If no I<file> is specified, the default path
is /etc/ccm.conf.  If the file is missing or specifies no value
for the cache root, that defaults to /var/lib/ccm.

=back

=cut
