#!/usr/bin/perl
#
# $Id$
#
# PBA svnprops tool
#
# (C) IBCL Informatik-Buero e.U. 2010
# (C) ITEG IT-Engineers GmbH 2010-2021
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

use strict;

require "/usr/lib/pba/pba-functions.pl";
require "/usr/lib/pba/devtools-functions.pl";

$main::scriptName = "pba-svnprops";

$main::helpMessage = '
 Syntax:
  '.$main::scriptName.' [CommandOptions] [Options]

 CommandOptions are:
  --help      Prints this help text and exits.
  --version   Prints the version info and exits.

 Without a command option, pba-svnprops suggests svn propset commands to
 set a few often needed svn properties like keyword \'Id\' on any text file 
 and execute \'*\' on assumed executable files.

 Options are:
  -c configdir   Read configdir/* rather than /etc/pba/*
  -d             Debug mode, logs everything.
  -q             Quiet mode, supresses informative messages, for  feeding.
  -v             Verbose verboser, logs more.
';
#-f configfile    Read /etc/pba/configfile rather than /etc/pba/builder.conf

$main::optionList = "dc:qv";

prepareGetopts();

initPBAFunctions();

initDevToolsFunctions();

# no config needed
#parseConfig();

# no need to check GPG key or DefaultBuildHost
#checkDevToolsConfig();

my @filesForKeywordId = ();
my @filesForExecutable = ();

my @svnedFiles=();

open (SVNLS, "svn ls -R |") || die("FATAL: Cannot execute\nsvn ls -R\n");
while ( my $svnedFile = <SVNLS> ) {
  chomp $svnedFile;
  push @svnedFiles, $svnedFile;
}
close SVNLS;

open (SVNADDED, "LANG=\"C\" svn status |egrep \"^A\" |cut -c2- |sed -e 's/ + / /' |tr -d ' ' |") || die("FATAL: Cannot execute\nLANG=\"C\" svn status |egrep \"^A\" |cut -c2- |tr -d ' '\n");
while ( my $svnedFile = <SVNADDED> ) {
  chomp $svnedFile;
  push @svnedFiles, $svnedFile;
}
close SVNADDED;

foreach my $svnedFile (@svnedFiles) {
  if (-d $svnedFile) {
    ($main::verbosity > 2) && print "Ignoring directory ".$svnedFile."\n";
    next;
  }
  if (! -f $svnedFile) {
    ($main::verbosity > 0) && print "Warning: svn-ed file missing: ".$svnedFile."\n";
    next;
  }
  ($main::verbosity > 2) && print "Checking ".$svnedFile."\n";
  if ($svnedFile =~ /src\/|^debian|\/Makefile|\/readme|\/license|\/todo|\.(bat|c|cmd|conf|cpp|css|h|in|html?|java|js|mk|pl|php|properties|py|sh|sql|txt|xml|)$/i) {
    if ($svnedFile !~ /\.(bin|bmp|bz2|bzip|conf|doc|docx|exe|gif|gz|ico|jpe?g|od.*|pdf|png|properties|py|svg|ttf|xdg|xls|xlsx|zip)$/i) {
      ($main::verbosity > 2) && print "Suggesting text file, checking Id keyword.\n";
      my $keywords = `svn propget svn:keywords $svnedFile`;
      ($main::verbosity > 2) && print "Keywords set are: ".$keywords."\n";
      if ($keywords !~ /Id/) {
        push @filesForKeywordId, $svnedFile;
      }
    }
  }
  if ($svnedFile =~ /\/bin|\.(bat|cmd|[a-z]*sh|pl|py)$/i) {
    ($main::verbosity > 2) && print "Suggesting executable file, checking executable property.\n";
    my $property = `svn propget svn:executable $svnedFile`;
    ($main::verbosity > 2) && print "Execute property set is: ".$property."\n";
    if ($property !~ /\*/) {
      push @filesForExecutable, $svnedFile;
    }
  }
}

if (scalar(@filesForKeywordId) == 0) {
  ($main::verbosity > 0) && print "No files for svn propset svn:keywords 'Id'\n";
} else {
  ($main::verbosity > 0) && print "Suggesting this propset command for keyword Id:\n";
  print "svn propset svn:keywords 'Id' ".join(" \\\n  ",@filesForKeywordId);
  ($main::verbosity > 0) && print "\n";
}
($main::verbosity > 0) && print "\n";

if (scalar(@filesForExecutable) == 0) {
  ($main::verbosity > 0) && print "No files for svn propset svn:executable '*'\n";
} else {
  ($main::verbosity > 0) && print "Suggesting this propset command for executable flag:\n";
  print "svn propset svn:executable '*' ".join(" \\\n  ",@filesForExecutable);
  ($main::verbosity > 0) && print "\n";
}
($main::verbosity > 0) && print "\n";

