#!/usr/bin/perl -w
#----------------------------------------------------------------------
# vim: ft=perl ts=2 sw=2 et:
#----------------------------------------------------------------------
#
# Copyright (C) 2012-2013 - Marco Hess <marco.hess@through-ip.com>
#
# This file is part of the "Git Repositories" panel in the
# SME Server server-manager panel to configure git repositories.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use File::Find;
use File::Path;
use esmith::util;
use esmith::templates;
use esmith::GitDB;

$ENV{'PATH'} = "/bin";

#----------------------------------------------------------------------

my $event                 = $ARGV [0];
my $gitRepositoryToDelete = $ARGV [1];

die "event argument missing" 
  unless defined ($event);

die "gitRepositoryToDelete argument missing" 
  unless defined ($gitRepositoryToDelete);

die "Invalid event: \"$event\". Expecting \"git-repository-delete\"\n"
  unless (($event) eq 'git-repository-delete');

#----------------------------------------------------------------------

my $git_db = esmith::GitDB->open_ro();

my $repository = $git_db->get($gitRepositoryToDelete) or 
  die "Couldn't find $gitRepositoryToDelete record in SME Server Git database\n";
  
die "'$gitRepositoryToDelete' is not an Git repository that is marked for deletion; git-repository-delete event failed.\n"
  unless ($repository->prop('type') eq 'repository-deleted');

#----------------------------------------------------------------------
  
my $GitRepositoryDirectory = "/home/e-smith/files/git/$gitRepositoryToDelete.git";
if( -d $GitRepositoryDirectory )
{
  # Clean up the directory tree
  rmtree( $GitRepositoryDirectory );
  if( -e $GitRepositoryDirectory )
  {
    warn "Could not remove Git repository in $GitRepositoryDirectory\n";
  }
  
  # Clean up the entry from the Git database. 
  # This allows new repos of the same name to be created again.
  $git_db->delete( $gitRepositoryToDelete )
}
else
{
  warn "Can't find the Git repository directory \"$GitRepositoryDirectory\"\n";
}

#------------------------------------------------------------

# Ensure that the git repository database is readable by the web server.
system("/bin/chmod", "644", "/home/e-smith/db/git") == 0
  or die "Could not change permissions on /home/e-smith/db/git";

# Ensure that the networks database is readable by the web server.
system("/bin/chmod", "644", "/home/e-smith/db/networks") == 0
  or die "Could not change permissions on /home/e-smith/db/networks";