#
# Copyright (c) 2020 SUSE LLC
#
# 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
#
################################################################
#
# scmsync database handling
#

package BSSrcServer::ScmsyncDB;

use strict;

use BSConfiguration;

require BSSrcServer::SQLite if $BSConfig::source_db_sqlite;

my $sourcedb = "$BSConfig::bsdir/db/source";

sub generate_scmsyncinfo {
  my ($scmsyncurl) = @_;

  require URI;
  my $uri = URI->new($scmsyncurl);
  my $scmsync_repo = $uri->scheme.'://'.$uri->host.$uri->path;
  $scmsync_repo =~ s/\.git$//;
  my $scmsync_branch = $uri->fragment;
  my %params = $uri->query_form;
  my $scmsyncinfo = { 'scmsync_repo' => $scmsync_repo };
  $scmsyncinfo->{'scmsync_branch'} = $scmsync_branch if $scmsync_branch;
  $scmsyncinfo->{'scmsync_trackingbranch'} = $params{'trackingbranch'} if $params{'trackingbranch'};
  return $scmsyncinfo;
}

sub generate_scmsyncurl {
  my ($scmsyncinfo) = @_;
  my $scmsyncurl = $scmsyncinfo->{'scmsync_repo'};
  $scmsyncurl .= "?trackingbranch=$scmsyncinfo->{'scmsync_trackingbranch'}" if $scmsyncinfo->{'scmsync_trackingbranch'};
  $scmsyncurl .= "#$scmsyncinfo->{'scmsync_branch'}" if $scmsyncinfo->{'scmsync_branch'};
  return $scmsyncurl;
}

sub storescmsync {
  my ($projid, $packid, $scmsyncurl) = @_;
  return unless $BSConfig::source_db_sqlite;
  my $scmsyncinfo = generate_scmsyncinfo($scmsyncurl);
  my $db = BSSrcServer::SQLite::opendb($sourcedb, 'scmsync');
  $db->store_scmsyncinfo($projid, $packid, $scmsyncinfo);
}

sub deletescmsync {
  my ($projid, $packid) = @_;
  return unless $BSConfig::source_db_sqlite;
  my $db = BSSrcServer::SQLite::opendb($sourcedb, 'scmsync');
  $db->store_scmsyncinfo($projid, $packid, undef);
}

sub getscmsyncpackages {
  my ($scmsync_repo, $scmsync_branch, $use_trackingbranch) = @_;
  return () unless $BSConfig::source_db_sqlite;
  my $db = BSSrcServer::SQLite::opendb($sourcedb, 'scmsync');
  $scmsync_repo =~ s/\.git$//;
  return $db->getscmsyncpackages($scmsync_repo, $scmsync_branch, $use_trackingbranch);
}

sub getscmsyncurl {
  my ($projid, $packid) = @_;
  return undef unless $BSConfig::source_db_sqlite;
  my $db = BSSrcServer::SQLite::opendb($sourcedb, 'scmsync');
  my $scmsyncinfo = $db->getscmsyncinfo($projid, $packid);
  return $scmsyncinfo ? generate_scmsyncurl($scmsyncinfo) : undef;
}

sub movescmsync {
  my ($projid, $oprojid) = @_;
  return unless $BSConfig::source_db_sqlite;
  my $db = BSSrcServer::SQLite::opendb($sourcedb, 'scmsync');
  $db->move_scmsyncinfos($projid, $oprojid);
}

1;
