#!/usr/bin/perl
#
# Copyright (c) 2020 SUSE LLC, Adrian Schroeter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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 (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
#
# Initialize a git repository to be used with Open Build Service (OBS)
#

use Cwd;
use Config::IniFiles;

my $workdir = getcwd;
$workdir = shift @ARGV if @ARGV == 1;

# set merge handler in .git/config
my $config_file = "$workdir/.git/config";
if ( -f "$workdir/.git" ) {
  # we are in a git submodule
  my $git_path;
  open(FH, '<', "$workdir/.git") || die "unable to read $workdir/.git";
  {
    local $/;
    $git_path = <FH>;
  }
  close(FH);
  $config_file = "$workdir/$1/config" if $git_path =~ m/^gitdir: (.*)/;
}
if ( ! -e $config_file) {
  print "No git tree found, initializing\n";
  system('git', 'init', '--object-format=sha256', '-b=main', $workdir) && die("ERROR: git initialization failed");
}
my $cfg = Config::IniFiles->new( -file => $config_file ) || die("ERROR: Reading of git configuration failed");

my $section = 'merge "merge-changes"';
unless ($cfg->exists($section, 'driver')) {
  $cfg->AddSection($section);
  $cfg->newval($section, 'name', 'Merging changes files by sorting of date');
  $cfg->newval($section, 'driver', '/usr/lib/obs/helper/bs_mergechanges %O %B %A');
  $cfg->WriteConfig($config_file) || die("ERROR: Write $config_file failed!\n");
}

# set .gitattributes file
my $attrib_file = "$workdir/.gitattributes";
my $gitattributes='';
if (-e $attrib_file) {
  open(FH, '<', $attrib_file) || die "unable to read .gitattributes";
  {
    local $/;
    $gitattributes = <FH>;
  }
  close(FH);
}

sub append {
  my ($prefix, $line, $lines) = @_;
  $lines .= "$line\n" unless $lines =~ m/^$prefix/;
  return $lines;
};

#
# Create or update .gitattributes
#
for (@{['7z', 'bsp', 'bz2', 'gem', 'gz', 'jar', 'lz', 'lzma', 'oxt',
	'pdf', 'png', 'rpm', 'tbz', 'tbz2', 'tgz', 'ttf', 'txz', 'whl',
	'xz', 'zip', 'zst']}) {
        $gitattributes = append("*.$_ ", "*.$_ filter=lfs diff=lfs merge=lfs -text", $gitattributes);
}
$gitattributes = append("*.changes ", "*.changes merge=merge-changes", $gitattributes);

open(FH, '>', $attrib_file) || die "unable to write .gitattributes";
print FH $gitattributes;
close(FH);
system('git', 'add', "$workdir/.gitattributes") && die("ERROR: git add .gitattributes failed");

#
# Create or update .gitignore
#
my $ignore_file = "$workdir/.gitignore";
my $gitignore = "";
if (-e $ignore_file) {
  open(FH, '<', $ignore_file) || die "unable to read .gitignore";
  {
    local $/;
    $gitignore = <FH>;
  }
  close(FH);
}
$gitignore = append("*.obscpio ", "*.obscpio", $gitignore);
$gitignore = append("*.osc ", "*.osc", $gitignore);
$gitignore = append("_build.* ", "_build.*", $gitignore);
$gitignore = append(".pbuild ", ".pbuild", $gitignore);

open(FH, '>', $ignore_file) or die $!;
print FH $gitignore;
close(FH);
system('git', 'add', "$workdir/.gitignore") && die("ERROR: git add .gitignore failed");

