#!/usr/bin/perl -w
#
# Copyright (c) 2006, 2007 Michael Schroeder, Novell Inc.
# Copyright (c) 2008 Adrian Schroeter, Novell Inc.
#
# 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
#
################################################################
#
# The Source Server
#

BEGIN {
  my ($wd) = $0 =~ m-(.*)/- ;
  $wd ||= '.';
  chdir($wd);
  unshift @INC,  "$wd/build";
  unshift @INC,  "$wd";
}

use XML::Structured ':bytes';
use POSIX;
use Fcntl qw(:DEFAULT :flock);
use Digest::MD5 ();
use Digest::SHA ();
use Data::Dumper;
use Storable ();
use Symbol;
use JSON::XS ();

use BSConfiguration;
use BSRPC ':https';
use BSServer;
use BSUtil;
use BSFileDB;
use BSXML;
use BSProductXML;
use BSVerify;
use BSHandoff;
use BSWatcher ':https';
use BSXPath;
use BSStdServer;
use BSSrcdiff;
use Build;
use BSNotify;
use BSPGP;
use BSSrcrep;
use BSRevision;
use BSKiwiXML;
use BSUrlmapper;

use BSXPath;
use BSXPathKeys;
use BSDB;
use BSDBIndex;

use BSSrcServer::Remote;
use BSSrcServer::Partition;
use BSSrcServer::Access;
use BSSrcServer::Projlink;
use BSSrcServer::Link;
use BSSrcServer::Service;
use BSSrcServer::Product;
use BSSrcServer::Multibuild;
use BSSrcServer::Blame;

# configure modules
$BSSrcServer::Projlink::getrev       = \&getrev;
$BSSrcServer::Projlink::findpackages = \&findpackages;
$BSSrcServer::Projlink::readpackage  = \&readpackage;

# links point to service expanded files
$BSSrcServer::Link::getrev = \&getrev;
$BSSrcServer::Link::lsrev_linktarget = \&lsrev_service;

$BSSrcServer::Service::getrev = \&getrev;
$BSSrcServer::Service::readpackage  = \&readpackage;
$BSSrcServer::Service::addrev = \&addrev;
$BSSrcServer::Service::notify = \&notify;
$BSSrcServer::Service::notify_repservers = \&notify_repservers;

$BSSrcServer::Blame::getrev = \&getrev;
$BSSrcServer::Blame::lsrev_expanded = \&lsrev_expanded;
$BSSrcServer::Blame::lsrev_service = \&lsrev_service;

$BSSrcServer::Product::notify_repservers = \&notify_repservers;

$Build::Rpm::unfilteredprereqs = 1 if defined $Build::Rpm::unfilteredprereqs;
$Build::Rpm::conflictdeps = 1 if defined $Build::Rpm::conflictdeps;
$Build::Kiwi::repoextras = 1 if defined $Build::Kiwi::repoextras;

use strict;

my $port = 5352;	#'SR'
$port = $1 if $BSConfig::srcserver =~ /:(\d+)$/;
my $proxy;
$proxy = $BSConfig::proxy if defined($BSConfig::proxy);

BSUtil::set_fdatasync_before_rename() unless $BSConfig::disable_data_sync || $BSConfig::disable_data_sync;

my $projectsdir = "$BSConfig::bsdir/projects";
my $eventdir = "$BSConfig::bsdir/events";
my $srcrep = "$BSConfig::bsdir/sources";
my $treesdir = $BSConfig::nosharedtrees ? "$BSConfig::bsdir/trees" : $srcrep;
my $requestsdir = "$BSConfig::bsdir/requests";
my $oldrequestsdir = "$BSConfig::bsdir/requests.old";
my $rundir = $BSConfig::rundir || "$BSConfig::bsdir/run";
my $diffcache = "$BSConfig::bsdir/diffcache";
my $extrepodir = "$BSConfig::bsdir/repos";

my $reqindexdb = "$BSConfig::bsdir/db/request";
my $extrepodb = "$BSConfig::bsdir/db/published";
my $sourcedb = "$BSConfig::bsdir/db/source";

my $notificationpayloaddir = "$BSConfig::bsdir/notificationpayload";

my $srcrevlay = [qw{rev vrev srcmd5 version time user comment requestid}];
my $eventlay = [qw{number time type project package repository arch}];
my $notificationlay = [qw{number type time data []}];


my $ajaxsocket = "$rundir/bs_srcserver.ajax";
my $uploaddir = "$srcrep/:upload";

my @binsufs = qw{rpm deb pkg.tar.gz pkg.tar.xz};
my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);

my $datarepoid;
my %packagequota;


sub notify {
  my ($type, $p) = @_;
  my $time = $p->{'time'} || time();
  delete $p->{'time'};
  if ($type eq 'PACKTRACK' && BSServer::have_content()) {
    my $payload = Storable::thaw(BSServer::read_data());
    if ($payload) {
      my $json_payload = JSON::XS::encode_json($payload);
      my $payloadkey = "$time.".Digest::MD5::md5_hex($json_payload);
      mkdir_p($notificationpayloaddir);
      writestr("$notificationpayloaddir/.$payloadkey", "$notificationpayloaddir/$payloadkey", $json_payload);
      $p->{'payload'} = $payloadkey;
      print "notification payload for $p->{'project'}/$p->{'repo'} stored in $payloadkey\n" if $p->{'project'} && $p->{'repo'};
    }
  }
  my @n = map {"$_=$p->{$_}"} grep {defined($p->{$_}) && !ref($p->{$_})} sort keys %{$p || {}};
  mkdir_p($eventdir);
  BSFileDB::fdb_add_i("$eventdir/lastnotifications", $notificationlay, {'type' => $type, 'time' => $time, 'data' => \@n});
}

sub prune_notificationpayloads {
  my ($cuttime) = @_;
  my @pl = ls($notificationpayloaddir);
  for my $p (@pl) {
    next unless $p =~ /^(\d+)\./;
    unlink("$notificationpayloaddir/$p") if $1 < $cuttime;
  }
}

sub prune_lastnotifications {
  my ($cutoff) = @_;
  local *F;
  return unless BSUtil::lockopen(\*F, '<', "$eventdir/lastnotifications", 1);
  my $filter = sub { $_[0]->{'number'} >= $cutoff ? 1 : 0 };
  my @l = BSFileDB::fdb_getall("$eventdir/lastnotifications", $notificationlay, undef, $filter);
  if (@l) {
    unlink("$eventdir/.lastnotifications.$$");
    if (! -e "$eventdir/.lastnotifications.$$") {
      BSFileDB::fdb_add_multiple("$eventdir/.lastnotifications.$$", $notificationlay, @l);
      rename("$eventdir/.lastnotifications.$$", "$eventdir/lastnotifications") || die("rename $eventdir/.lastnotifications.$$ $eventdir/lastnotifications: $!\n");
      prune_notificationpayloads($l[0]->{'time'} - 240 * 3600) if -d $notificationpayloaddir;	# keep 10 days
    }
  }
  close F;
}

sub lastnotifications {
  my ($cgi) = @_;
  my $view = $cgi->{'view'};
  die("unsupported view\n") if $view && $view ne 'json';
  if (!$cgi->{'start'}) {
    # just fetch the current event number
    my $lastev = BSFileDB::fdb_getlast("$eventdir/lastnotifications", $notificationlay);
    my $lastno = $lastev ? $lastev->{'number'} : 0;
    my $ret = {'next' => $lastno + 1, 'sync' => 'lost'};
    return (JSON::XS::encode_json($ret), 'Content-Type: application/json') if $view && $view eq 'json';
    return ($ret, $BSXML::notifications);
  }
  # check if we need to prune
  if (!$BSStdServer::isajax && !$cgi->{'noprune'}) {
    my $firstev = (BSFileDB::fdb_getall("$eventdir/lastnotifications", $notificationlay, undef, sub {-1}))[0];
    if ($firstev && $cgi->{'start'} - $firstev->{'number'} > 1000) {
      my $lastev = BSFileDB::fdb_getlast("$eventdir/lastnotifications", $notificationlay);
      prune_lastnotifications($cgi->{'start'} - 500) if $lastev && $cgi->{'start'} <= $lastev->{'number'} + 1;
    }
  }
  my $filter = sub { $cgi->{'start'} > $_[0]->{'number'} ? -2 : 1 };
  my @l = BSFileDB::fdb_getall_reverse("$eventdir/lastnotifications", $notificationlay, undef, $filter);
  if ($cgi->{'block'} && !@l) {
    # pass on to AJAX
    if (!$BSStdServer::isajax) {
      BSHandoff::handoff('/lastnotifications', undef, "start=$cgi->{'start'}", 'block=1');
    }
    # wait untill we have a winner
    BSWatcher::addfilewatcher("$eventdir/lastnotifications");
    my $lastev = BSFileDB::fdb_getlast("$eventdir/lastnotifications", $notificationlay);
    return undef if !$lastev || $lastev->{'number'} < $cgi->{'start'};
    if ($cgi->{'start'} == $lastev->{'number'}) {
      @l = ($lastev);
    } else {
      @l = BSFileDB::fdb_getall_reverse("$eventdir/lastnotifications", $notificationlay, undef, $filter);
      return undef unless @l;
    }
  }
  my $res = {};
  @l = reverse @l;
  if (@l) {
    $res->{'next'} = $l[-1]->{'number'} + 1;
  } else {
    my $lastev = BSFileDB::fdb_getlast("$eventdir/lastnotifications", $notificationlay);
    my $no = ($lastev->{'number'} || 0);
    $no = $cgi->{'start'} - 1 if $no >= $cgi->{'start'};
    $res->{'next'} = $no + 1;
  }
  if (@l && $l[0]->{'number'} > $cgi->{'start'}) {
    $res->{'sync'} = 'lost';
    @l = ();
  }
  # don't send more than 1000 notifications to the poor api
  if (@l > 1000) {
    $res->{'limit_reached'} = 1; # tell the api that there is more
    $res->{'next'} = $l[1000]->{'number'};
    @l = splice(@l, 0, 1000);
  }
  $res->{'notification'} = \@l;
  delete $_->{'number'} for @l;		# XXX: why?
  if ($view && $view eq 'json') {
    for my $l (@l) {
      my %d;
      for (@{$l->{'data'} || []}) {
        my @s = split('=', $_, 2);
        $d{$s[0]} = $s[1];
      }
      $l->{'data'} = \%d;
    }
    return (JSON::XS::encode_json($res), 'Content-Type: application/json');
  }
  for my $l (@l) {
    for (@{$l->{'data'} || []}) {
      my @s = split('=', $_, 2);
      $_ = {'key' => $s[0]};
      $_->{'_content'} = $s[1] if defined $s[1];
    }
  }
  return ($res, $BSXML::notifications);
}

sub getnotificationpayload {
  my ($cgi, $payloadkey) = @_;
  my $file = "$notificationpayloaddir/$payloadkey";
  die("404 payload does not exist!\n") unless -e $file;
  return (readstr($file), 'Content-Type: application/json');
}

sub deletenotificationpayload {
  my ($cgi, $payloadkey) = @_;
  unlink("$notificationpayloaddir/$payloadkey");
  return $BSStdServer::return_ok;
}

sub notify_repservers {
  my ($type, $projid, $packid, $job) = @_;

  my $ev = {'type' => $type, 'project' => $projid};
  $ev->{'package'} = $packid if defined $packid;
  addevent($ev) unless $type eq 'suspendproject';

  my @args = ("type=$type", "project=$projid");
  push @args, "package=$packid" if defined $packid;
  push @args, "job=$job" if defined $job;
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/event",
    'request'   => 'POST',
    'background' => 1,
  };
  eval {
    BSWatcher::rpc($param, undef, @args);
  };
  print "warning: $reposerver: $@" if $@;
}

# this is only used from getfilelist_ajax.
# as projid is a remote project, we don't know which reposerver
# we need to notify. so send the event to all of them.
# we also do not call addevent in this specific case.
sub notify_all_repservers {
  my ($type, $projid, $packid) = @_;

  my @reposervers = BSSrcServer::Partition::allreposervers();
  my @args = ("type=$type", "project=$projid");
  push @args, "package=$packid" if defined $packid;
  for my $reposerver (@reposervers) {
    my $param = {
      'uri' => "$reposerver/event",
      'request'   => 'POST',
      'background' => 1,
    };
    eval {
      BSWatcher::rpc($param, undef, @args);
    };
    print "warning: $reposerver: $@" if $@;
  }
}

sub triggerservicerun {
  my ($cgi, $projid, $packid) = @_;
  my $rev = getrev($projid, $packid);
  my $linkinfo = {};
  my $files = BSRevision::lsrev($rev, $linkinfo);
  $cgi->{'triggerservicerun'} = 1;	# hack
  if ($BSConfig::old_style_services) {
    # old style, just run the service again...
    BSSrcServer::Service::runservice($cgi, $rev, $files);
  } else {
    my $servicemark = BSSrcServer::Service::genservicemark($projid, $packid, $files);
    if ($servicemark || $linkinfo->{'xservicemd5'} || $packid eq '_product') {
      # have to do a new commit...
      $cgi->{'comment'} ||= 'trigger service run';
      $cgi->{'servicemark'} = $servicemark;
      $rev = addrev($cgi, $projid, $packid, $files);
      BSSrcServer::Service::runservice($cgi, $rev, $files);
    } else {
      die("404 no source service defined!\n");
    }
  }
  return $BSStdServer::return_ok;
}

sub waitservicerun {
  my ($cgi, $projid, $packid) = @_;
  die("not implemented for old style services\n") if $BSConfig::old_style_services;
  if (!$BSStdServer::isajax) {
    my $rev = getrev($projid, $packid);
    my $linkinfo = {};
    my $files = BSRevision::lsrev($rev, $linkinfo);
    my $servicemark = $linkinfo->{'xservicemd5'};
    return $BSStdServer::return_ok unless $servicemark;
    eval {
      BSSrcServer::Service::handleservice($rev, $files, $servicemark);
    };
    return $BSStdServer::return_ok unless $@;
    die($@) if $@ !~ /service in progress/;
    # pass on to ajax
    BSHandoff::handoff("/source/$projid/$packid", undef, 'cmd=waitservice', "servicemark=$servicemark");
  }
  my $servicemark = $cgi->{'servicemark'};
  die("need a servicemark\n") unless $servicemark;
  BSWatcher::addfilewatcher(BSSrcrep::serviceerrorfile($projid, $packid, $servicemark));
  my $serror = BSSrcrep::getserviceerror($projid, $packid, $servicemark);
  return $BSStdServer::return_ok unless $serror;
  return undef if $serror =~ /service in progress/;
  die("$serror\n");
}

sub mergeservicerun {
  my ($cgi, $projid, $packid) = @_;
  my $rev = getrev($projid, $packid);
  my $linkinfo = {};
  my $files = BSRevision::lsrev($rev, $linkinfo);
  my $servicemark = $linkinfo->{'xservicemd5'};
  die("package has no service\n") unless $servicemark;
  $files = BSSrcServer::Service::handleservice($rev, $files, $servicemark);
  # merge
  delete $files->{'_service'};
  for (sort keys %$files) {
    next unless /^_service:.*:(.*?)$/s;
    $files->{$1} = $files->{$_};
    delete $files->{$_};
    BSSrcrep::copyonefile($projid, $packid, $1, $projid, $packid, $_, $files->{$1});
  }
  $rev = addrev($cgi, $projid, $packid, $files);
  BSSrcServer::Service::runservice($cgi, $rev, $files);
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

#
# return version and release of commit
#
sub getcommitinfo {
  my ($projid, $packid, $srcmd5, $files) = @_;

  # get version/release from rpm spec/deb dsc/kiwi xml file
  my $version = 'unknown';
  my $release;
  if ($files->{'_link'}) {
    # can't know the version/release of a link as it is
    # a moving target
    return ('unknown', '0');
  }
  my $cfile;
  $cfile = "$projectsdir/$projid.conf" if -e "$projectsdir/$projid.conf";
  my $bconf = Build::read_config('noarch', $cfile);
  for my $type ('spec', 'dsc', 'kiwi') {
    my $rev = {'project' => $projid, 'package' => $packid, 'srcmd5' => $srcmd5};
    my $file = findfile($rev, undef, $type, $files);
    next unless defined $file;
    my $d = Build::parse($bconf, BSRevision::revfilename($rev, $file, $files->{$file}));
    next unless defined $d->{'version'};
    $version = $d->{'version'};
    $release = $d->{'release'} if defined $d->{'release'};
    $version = 'unknown' if $d->{'multiversion'};
    last;
  }
  if (defined($release)) {
    if ($release =~ /(\d+)\.<B_CNT>/) {
      $release = $1;
    } elsif ($release =~ /<RELEASE(\d+)>/) {
      $release = $1;
    } elsif ($release =~ /^(\d+)/) {
      $release = $1;
    } else {
      $release = '0';
    }
  }
  $release ||= '0';
  if ($bconf->{'cicntstart'} && $bconf->{'cicntstart'} =~ /(\d+)$/) {
    my $r = $release;
    $release = $bconf->{'cicntstart'};
    $release =~  s/\d+$/$r/ if $r > $1;
  }
  return ($version, $release);
}

#
# get a revision object from a revision identifier
#
sub getrev {
  my ($projid, $packid, $revid, $linked, $missingok) = @_;
  if ($packid =~ /(?<!^_product)(?<!^_patchinfo):./) {
    my $lpackid = $packid;
    $lpackid =~ s/(?<!^_product)(?<!^_patchinfo):.*//;
    my $rev = getrev($projid, $lpackid, $revid, $linked, $missingok);
    $rev->{'originpackage'} = $packid if $rev;
    return $rev;
  }
  my $rev = BSRevision::getrev_local($projid, $packid, $revid);
  return $rev if $rev;
  my $proj = BSRevision::readproj_local($projid, 1);
  if ($proj && $proj->{'link'}) {
    $rev = BSSrcServer::Projlink::getrev_projlink($projid, $proj, $packid, $revid, $linked, $missingok);
    return $rev if $rev;
  }
  if ($revid && $revid =~ /^[0-9a-f]{32}$/) {
    $rev = BSRevision::getrev_deleted_srcmd5($projid, $packid, $revid);
    return $rev if $rev;
  }
  if (!$proj || $proj->{'remoteurl'}) {
    $proj = BSSrcServer::Remote::remoteprojid($projid);
    $rev = BSSrcServer::Remote::getrev_remote($projid, $proj, $packid, $revid, $linked, $missingok) if $proj;
    return $rev if $rev;
  }
  return {'project' => $projid, 'package' => $packid, 'srcmd5' => 'pattern', 'rev' => 'pattern'} if $packid eq '_pattern';
  if ($missingok) {
    $rev = { 'project' => $projid, 'package' => $packid, 'srcmd5' => $BSSrcrep::emptysrcmd5 };
    if ($proj && $proj->{'link'}) {
      my $vrev = BSSrcServer::Projlink::getnewvrev($projid, $proj);
      $rev->{'vrev'} = $vrev if $vrev;
    }
    return $rev;
  }
  die("404 package '$packid' does not exist\n") if -e "$projectsdir/$projid.xml";
  die("404 project '$projid' does not exist\n");
}

sub addmeta_upload {
  my ($projid, $packid, $files) = @_;

  # calculate new meta sum
  my $meta = '';
  $meta .= "$files->{$_}  $_\n" for sort keys %$files;
  my $srcmd5 = Digest::MD5::md5_hex($meta);
  mkdir_p($uploaddir);
  mkdir_p("$projectsdir/$projid.pkg");
  writestr("$uploaddir/addmeta$$", "$projectsdir/$projid.pkg/$packid.upload-MD5SUMS", $meta);
  return $srcmd5;
}

##
# generate_commit_flist($files_old, $files_new)
#
#   $files_old/$files_new are hash references as returned by lsrep
#
#   returns a list of changed files categorized similar to svn commit mails
#
sub generate_commit_flist {
  my $ret = "";
  my %categorized_files;
  my ($files_old, $files_new) = @_;
  my %files_all = (%$files_new, %$files_old);
  for my $fname (sort keys %files_all) {
    if(!$files_old->{$fname}) {
      my $flist = $categorized_files{"Added:"} ||= [];
      push(@$flist, $fname);
    } elsif(!$files_new->{$fname}) {
      my $flist = $categorized_files{"Deleted:"} ||= [];
      push(@$flist, $fname);
    } elsif($files_old->{$fname} ne $files_new->{$fname}) {
      my $flist = $categorized_files{"Modified:"} ||= [];
      push(@$flist, $fname);
    }
  }

  for my $cat (sort keys %categorized_files) {
    $ret .= "$cat\n";
    for my $fname (@{$categorized_files{$cat}}) {
      $ret .= "  $fname\n";
    }
    $ret .= "\n";
  }
  return $ret;
}

#
# create a new revision from a file list, returns revision object
#
sub addrev {
  my ($cgi, $projid, $packid, $files, $target) = @_;
  die("404 project '$projid' does not exist\n") unless -e "$projectsdir/$projid.xml";
  if ($packid =~ /^_product:/) {
    die("403 package '$packid' is read-only if a '_product' package exists\n") if -e "$projectsdir/$projid.pkg/_product.xml";
  }
  die("403 package '$packid' is read-only\n") if $packid =~ /(?<!^_product)(?<!^_patchinfo):./;
  my $user = $cgi->{'user'};
  my $comment = $cgi->{'comment'};
  my $requestid = $cgi->{'requestid'};
  $user = '' unless defined $user;
  $user = 'unknown' if $user eq '';
  $comment = '' unless defined $comment;
  $user = str2utf8xml($user);
  $comment = str2utf8xml($comment);

  # check if the commit will need a service run
  my $servicemark;
  delete $files->{'/SERVICE'};	# just in case...
  if (!$BSConfig::old_style_services && $packid ne '_project') {
    if ($cgi->{'noservice'}) {
      ($servicemark, $files) = BSSrcServer::Service::servicemark_noservice($cgi, $projid, $packid, $files, $target);
    } else {
      # we do not want any generated files in the commit!
      delete $files->{$_} for grep {/^_service[:_]/} keys %$files;
      $servicemark = exists($cgi->{'servicemark'}) ? $cgi->{'servicemark'} : BSSrcServer::Service::genservicemark($projid, $packid, $files, $target);
    }
  }
  if ($packid eq '_pattern' && ! -e "$projectsdir/$projid.pkg/$packid.xml") {
    # upgrade pseudo _pattern package to real package
    my $pack = {
      'name' => $packid,
      'project' => $projid,
      'title' => 'pseudo package to store pattern information',
      'description' => "pseudo package to store pattern information\n",
    };
    mkdir_p($uploaddir);
    writexml("$uploaddir/$$.2", undef, $pack, $BSXML::pack);
    BSRevision::addrev_meta_replace($cgi, $projid, $packid, [ "$uploaddir/$$.2", "$projectsdir/$projid.pkg/$packid.xml", '_meta' ]);
  }
  die("404 package '$packid' does not exist\n") unless $packid eq '_project' || -e "$projectsdir/$projid.pkg/$packid.xml";
  if ($target && $target eq 'upload') {
    my $srcmd5 = addmeta_upload($projid, $packid, $files);
    my $filenames = join( ', ', keys %$files);
    notify("SRCSRV_UPLOAD", {project => $projid, package => $packid, filename => $filenames, comment => $comment, 
                             target => $target, requestid => $requestid, user => $user});
    return {'project' => $projid, 'package' => $packid, 'rev' => 'upload', 'srcmd5' => $srcmd5};
  } elsif ($target && $target eq 'repository') {
    # repository only upload.
    return {'project' => $projid, 'package' => $packid, 'rev' => 'repository', 'srcmd5' => $BSSrcrep::emptysrcmd5};
  } elsif (defined($target)) {
    # internal version only upload.
    my $srcmd5 = BSSrcrep::addmeta($projid, $packid, $files);
    return {'project' => $projid, 'package' => $packid, 'rev' => $srcmd5, 'srcmd5' => $srcmd5};
  }
  die("bad projid\n") if $projid =~ /\// || $projid =~ /^\./;
  die("bad packid\n") if $packid =~ /\// || $packid =~ /^\./;
  die("bad files (slash)\n") if grep {/\// && $_ ne '/SERVICE'} keys %$files;
  die("bad files (glyph)\n") if grep {!/^[0-9a-f]{32}$/} values %$files;

  if ($files->{'_patchinfo'}) {
    die("bad files in patchinfo container\n") if grep {$_ ne '_patchinfo'} keys %$files;
    my $p = BSRevision::revreadxml({'project' => $projid, 'package' => $packid}, '_patchinfo', $files->{'_patchinfo'}, $BSXML::patchinfo);
    BSVerify::verify_patchinfo($p);
  }

  # create tree entry
  $files->{'/SERVICE'} = $servicemark if $servicemark;
  my $srcmd5 = BSSrcrep::addmeta($projid, $packid, $files);
  delete $files->{'/SERVICE'};

  my $rev = {'srcmd5' => $srcmd5, 'time' => time(), 'user' => $user, 'comment' => $comment, 'requestid' => $requestid};

  if ($packid eq '_product') {
    BSSrcServer::Product::expandproduct($projid, $packid, $rev, $files, $user, 1);
  }

  if ($packid ne '_project' && $packid ne '_pattern') {
    my ($version, $release) = getcommitinfo($projid, $packid, $srcmd5, $files);
    $rev->{'version'} = $version;
    $rev->{'vrev'} = $release;
  }
  
  my $rev_old = getrev($projid, $packid);
  $rev_old->{'keepsignkey'} = 1;
  my $files_old = BSRevision::lsrev($rev_old);
  delete $rev_old->{'keepsignkey'};
  my $filestr = generate_commit_flist($files_old, $files);

  $rev->{'version'} = $cgi->{'version'} if defined $cgi->{'version'};
  $rev->{'vrev'} = $cgi->{'vrev'} if defined $cgi->{'vrev'};
  if ($cgi->{'time'}) {
    die("specified time is less than time in last commit\n") if ($rev_old->{'time'} || 0) > $cgi->{'time'};
    $rev->{'time'} = $cgi->{'time'};
  }

  my $acceptinfo;
  if ($requestid) {
    $acceptinfo = {};
    $acceptinfo->{'osrcmd5'} = $rev_old->{'srcmd5'} if $rev_old->{'srcmd5'} ne 'empty';
    if ($files_old->{'_link'}) {
      # see if we can expand it
      eval {
	my %rev = %$rev_old;
	BSSrcServer::Link::handlelinks(\%rev, $files_old);
	$acceptinfo->{'oxsrcmd5'} = $rev{'srcmd5'};
      };
    }
  }
  if ($packid eq '_project') {
    $rev = BSRevision::addrev_local($cgi, $projid, $packid, $rev);
    if ($acceptinfo && $cgi->{'withacceptinfo'}) {
      $acceptinfo->{'rev'} = $rev->{'rev'};
      $acceptinfo->{'srcmd5'} = $rev->{'srcmd5'};
      $rev->{'acceptinfo'} = $acceptinfo;
    }
    notify_repservers('project', $projid);
    notify("SRCSRV_UPDATE_PROJECT_CONFIG", { "project" => $projid, "files" => $filestr, "comment" => $comment, "sender" => $user });
    return $rev;
  }

  # help a little with link<->nolink and singleversion<->multiversion changes
  if (defined($rev->{'version'}) && defined($rev_old->{'version'}) && !defined($cgi->{'vrev'})) {
    # if this is a known -> unknown version change, max with vrev of last commit
    # same for unknown -> known
    if (($rev->{'version'} eq 'unknown' && $rev_old->{'version'} ne 'unknown') ||
        ($rev->{'version'} ne 'unknown' && $rev_old->{'version'} eq 'unknown')) {
      my $l_old = 0;
      $l_old = $1 if $rev_old->{'vrev'} =~ /(\d+)$/;
      my $l_new = 0;
      $l_new = $1 if $rev->{'vrev'} =~ /(\d+)$/;
      $rev->{'vrev'} =~ s/\d+$/$l_old + 1/e if $l_old + 1 > $l_new;
    }
  }

  # add to revision database
  $rev = BSRevision::addrev_local($cgi, $projid, $packid, $rev, $files);

  # update request acceptinfo
  if ($acceptinfo && $cgi->{'withacceptinfo'}) {
    $acceptinfo->{'rev'} = $rev->{'rev'};
    $acceptinfo->{'srcmd5'} = $rev->{'srcmd5'};
    if ($files->{'_link'}) {
      # see if we can expand it
      eval {
	my %rev = %$rev;
	BSSrcServer::Link::handlelinks(\%rev, $files);
	$acceptinfo->{'xsrcmd5'} = $rev{'srcmd5'};
      };
    }
    $rev->{'acceptinfo'} = $acceptinfo;
  }

  # send out notification
  notify("SRCSRV_COMMIT", {project => $projid, package => $packid, files => $filestr, rev => $rev->{'rev'}, user => $user, comment => $comment, 'requestid' => $requestid});
  $rev_old->{'version'} = "unknown" unless defined($rev_old->{'version'});
  if (defined($rev->{'version'}) && defined($rev_old->{'version'}) && $rev->{'version'} ne $rev_old->{'version'}) {
    notify("SRCSRV_VERSION_CHANGE", {project => $projid, package => $packid, files => $filestr, rev => $rev->{'rev'},
                                     oldversion => $rev_old->{'version'}, newversion => $rev->{'version'},
                                     user => $user, comment => $comment, 'requestid' => $requestid});
  }

  notify_repservers('package', $projid, $packid);

  # put marker back
  $files->{'/SERVICE'} = $servicemark if $servicemark;
  return $rev;
}

# returns service expanded filelist
# modifies $rev->{'srcmd5'}
sub lsrev_service {
  my ($rev, $linkinfo) = @_;
  $linkinfo ||= {};
  my $files = BSRevision::lsrev($rev, $linkinfo);
  $files = BSSrcServer::Service::handleservice($rev, $files, $linkinfo->{'xservicemd5'}) if $linkinfo->{'xservicemd5'};
  return $files;
}

# returns expanded filelist
# modifies $rev->{'srcmd5'}, $rev->{'vrev'}
sub lsrev_expanded {
  my ($rev, $linkinfo) = @_;
  my $files = lsrev_service($rev, $linkinfo);
  return $files unless $files->{'_link'};
  $files = BSSrcServer::Link::handlelinks($rev, $files, $linkinfo);
  die("$files\n") unless ref $files;
  return $files;
}

###########################################################################
###
###  project/package management
###

sub findprojects {
  my ($deleted) = @_;
  # add support for remote projects?
  return BSRevision::lsprojects_local($deleted);
}

sub findpackages {
  my ($projid, $proj, $nonfatal, $origins, $noexpand, $deleted) = @_;

  $proj ||= readxml("$projectsdir/$projid.xml", $BSXML::proj, 1);
  if (!$proj || $proj->{'remoteurl'}) {
    $proj = BSSrcServer::Remote::remoteprojid($projid);
    return BSSrcServer::Remote::findpackages_remote($projid, $proj, $nonfatal, $origins, $noexpand, $deleted) if $proj;
  }

  # get local packages
  my @packids = BSRevision::lspackages_local($projid, $deleted);
  if ($origins) {
    for (@packids) {
      $origins->{$_} = $projid unless defined $origins->{$_};
    }
  }
  @packids = BSSrcServer::Multibuild::addmultibuildpackages($projid, $origins, @packids) unless $deleted;

  # handle project links (but not if deleted)
  if ($proj && $proj->{'link'} && !$noexpand && !$deleted) {
    push @packids, BSSrcServer::Projlink::findpackages_projlink($projid, $proj, $nonfatal, $origins);
    @packids = sort(BSUtil::unify(@packids));
  }

  return @packids;
}

sub readpackage {
  my ($projid, $proj, $packid, $rev, $missingok) = @_;

  $proj ||= BSRevision::readproj_local($projid, 1);
  if (!$proj || $proj->{'remoteurl'}) {
    $proj = BSSrcServer::Remote::remoteprojid($projid);
    return BSSrcServer::Remote::readpackage_remote($projid, $proj, $packid, $rev, $missingok) if $proj;
  }
  my $pack = BSRevision::readpack_local($projid, $packid, 1, $rev);
  $pack->{'project'} ||= $projid if $pack;

  if (!$pack && $proj && $proj->{'link'}) {
    if (!defined($rev) || !BSRevision::readpack_local($projid, $packid, 1)) {
      $pack = BSSrcServer::Projlink::readpackage_projlink($projid, $proj, $packid, $rev, $missingok);
    }
  }

  die("404 package '$packid' does not exist in project '$projid'\n") if !$missingok && !$pack;
  return $pack;
}

sub readproject {
  my ($projid, $proj, $rev, $missingok) = @_;
  $proj ||= BSRevision::readproj_local($projid, 1, $rev);
  if (!$proj || ($proj->{'remoteurl'} && $proj->{'remoteproject'})) {
    $proj = BSSrcServer::Remote::remoteprojid($projid);
    return BSSrcServer::Remote::readproject_remote($projid, $proj, $rev, $missingok) if $proj && $proj->{'remoteproject'};
  }
  $proj->{'name'} ||= $projid if $proj;
  die("404 project '$projid' does not exist\n") if !$missingok && !$proj;
  return $proj;
}

# collect all global source services via all package and project links
sub getprojectservices {
  my ($cgi, $projid, $packid) = @_;
  my $services = BSSrcServer::Service::getprojectservices($projid, $packid);
  return ($services, $BSXML::services);
}

sub notifypackagechange {
  my ($cgi, $projid, $packid) = @_;
  notify_repservers('package', $projid, $packid);
  return $BSStdServer::return_ok;
}

# find matching .spec/.dsc/.kiwi file depending on packid and/or repoid
sub findfile {
  my ($rev, $repoid, $ext, $files) = @_;

  return undef if !$files || !$ext || $ext eq 'none';

  # map services files to their real name
  my %files = map {$_ => $_} keys %$files;
  if ($files{'_service'}) {
    for (sort keys %files) {
      next unless /^_service:.*:(.*?)$/s;
      $files{$1} = delete($files{$_}) if $files{$_};
    }
  }

  return $files{'_preinstallimage'} if $ext ne 'kiwi' && keys(%files) == 1 && $files{'_preinstallimage'};
  return $files{'simpleimage'} if $files{'simpleimage'};
  return $files{'snapcraft.yaml'} if $files{'snapcraft.yaml'} && $ext eq 'snapcraft';
  return $files{'appimage.yml'} if $files{'appimage.yml'} && $ext eq 'appimage';
  return $files{'Dockerfile'} if $files{'Dockerfile'} && $ext eq 'docker';
  return $files{'fissile.yml'} if $files{'fissile.yml'} && $ext eq 'fissile';

  if ($ext eq 'arch') {
    return $files{'PKGBUILD'} if $files{'PKGBUILD'};
    return undef;
  }

  my $packid = $rev->{'package'};
  $packid = $1 if $rev->{'originpackage'} && $rev->{'originpackage'} =~ /:([^:]+)$/;
  return $files{"$packid-$repoid.$ext"} if defined($repoid) && $files{"$packid-$repoid.$ext"};
  # 28.4.2009 mls: deleted "&& defined($repoid)"
  return $files{"$packid.$ext"} if $files{"$packid.$ext"};
  # try again without last components
  if ($packid =~ /^(.*?)\./) {
    return $files{"$1.$ext"} if $files{"$1.$ext"};
  }
  my @files = grep {/\.$ext$/} keys %files;
  @files = grep {/^\Q$packid\E/i} @files if @files > 1;
  return $files{$files[0]} if @files == 1;
  if (@files > 1) {
    if (!defined($repoid)) {
      # return undef;
      @files = sort @files;
      return $files{$files[0]};
    }
    @files = grep {/^\Q$packid-$repoid\E/i} @files if @files > 1;
    return $files{$files[0]} if @files == 1;
  }
  return undef;
}

#########################################################################

# set up kiwi project callback

sub kiwibootcallback {
  my ($projid, $packid) = @_;
  BSVerify::verify_projid($projid);
  BSVerify::verify_packid($packid);
  BSSrcServer::Access::checksourceaccess($projid, $packid);
  my $rev = getrev($projid, $packid);
  my $files = BSRevision::lsrev($rev);
  my $file = findfile($rev, undef, 'kiwi', $files);
  die("no kiwi file found\n") unless $file;
  my $xml = BSRevision::revreadstr($rev, $file, $files->{$file});
  return ($xml, {'project' => $projid, 'package' => $packid, 'srcmd5' => $rev->{'srcmd5'}, 'file' => $file});
}
$Build::Kiwi::bootcallback = \&kiwibootcallback;

$Build::Kiwi::urlmapper = \&BSUrlmapper::urlmapper;

#########################################################################

sub getprojquotapackage {
  my ($projid) = @_;
  if (!exists($packagequota{':packages'})) {
    my $quotaxml = readxml($BSConfig::bsquotafile, $BSXML::quota, 1);
    for my $p (@{$quotaxml->{'project'} || []}) {
      $packagequota{$p->{'name'}} = $p->{'packages'};
    }
    $packagequota{':packages'} = $quotaxml->{'packages'};
  }
  while ($projid) {
    return $packagequota{$projid} if exists $packagequota{$projid};
    last unless $projid =~ s/:[^:]*$//;
  }
  return $packagequota{':packages'};
}

sub readbuildenv {
  my ($pinfo, $rev, $files, $repoid, $arch) = @_;
  my $bifile = "_buildenv.$repoid.$arch";
  $bifile = '_buildenv' unless $files->{$bifile};
  eval {
    die("$bifile does not exist\n") unless $files->{$bifile};
    $pinfo->{'hasbuildenv'} = BSRevision::revreadstr($rev, $bifile, $files->{$bifile});
  };
  if ($@) {
    $pinfo->{'error'} = "buildenv: $@";
    chomp($pinfo->{'error'});
  }
}

sub getprojpack {
  my ($cgi, $projids, $repoids, $packids, $arch) = @_;
  local *oldbsrpc = *BSRPC::rpc;
  local *BSRPC::rpc;
  die("unsupported view\n") if $cgi->{'view'} && $cgi->{'view'} ne 'storable';
  if ($cgi->{'noremote'}) {
    *BSRPC::rpc = sub {die("400 interconnect error: noremote option\n");};
  } else {
    *BSRPC::rpc = sub {
      my $r = eval { oldbsrpc(@_) };
      if ($@) {
	$@ = "interconnect error: $@" unless $@ =~ /(?:remote|interconnect) error:/;
	die($@);
      }
      return $r;
    };
  }
  $arch ||= 'noarch';
  if ($cgi->{'buildinfo'}) {
    die("getprojpack buildinfo requests must have at least one project parameter\n") unless @{$projids || []} >= 1;
    die("getprojpack buildinfo requests must have exactly one repository parameter\n") unless @{$repoids || []} == 1;
    die("getprojpack buildinfo requests must not have more than one package parameter\n") unless @{$packids || []} <= 1;
    $cgi->{'parseremote'} = 1;
    $cgi->{'withremotemap'} = 1;
    $cgi->{'withconfig'} = 1;
    $cgi->{'withrepos'} = 1;
    $cgi->{'ignoredisable'} = 1;
    $cgi->{'nopackages'} = 1 unless @{$packids || []};
  }
  if ($cgi->{'remotemaponly'}) {
    die("getprojpack remotemaponly makes no sense with packages\n") if @{$packids || []};
    $cgi->{'withremotemap'} = 1;
  }
  my $partition = $cgi->{'partition'};
  die("No such partition '$partition'\n") if $partition && $BSConfig::partitionservers && !$BSConfig::partitionservers->{$partition};
  my $remotemap = $cgi->{'withremotemap'} ? {} : undef;
  if ($remotemap && $partition) {
    $remotemap->{':partition'} = $partition;
    $remotemap->{':partitions'} = {};
  }
  $projids = [ findprojects() ] unless $projids;
  if ($partition) {
    for my $projid (splice @$projids) {
      my $par = BSSrcServer::Partition::projid2partition($projid);
      die("cannot determine partition for $projid\n") unless defined $par;
      if ($par ne $partition) {
	# check if it is remote, if not then it belongs to another partition
	my $r = BSRevision::readproj_local($projid, 1);
	$r = BSSrcServer::Remote::remoteprojid($projid) unless defined $r;
	if (!($r && $r->{'remoteurl'})) {
	  # not remote, but on wrong partition
	  # if asked for a specific project, put it in remotemap
	  next unless $remotemap && $cgi->{'project'};
	  BSSrcServer::Partition::checkpartition($remotemap, $projid, $r) if $remotemap->{':partitions'} && !$remotemap->{':partitions'}->{$projid};
	  if ($remotemap->{$projid} && $cgi->{'withconfig'} && $remotemap->{$projid}->{'partition'}) {
	    # also put config in remotemap if asked for a specific project
	    my $c = readstr("$projectsdir/$projid.conf", 1);
	    $remotemap->{$projid}->{'config'} = defined($c) ? $c : '';
	  }
	  next;
	}
      }
      push @$projids, $projid;
    }
  }
  if ($BSConfig::limit_projects && $BSConfig::limit_projects->{$arch}) {
    my %limit_projids = map {$_ => 1} @{$BSConfig::limit_projects->{$arch}};
    $projids = [ grep {$limit_projids{$_}} @$projids ];
  }
  $repoids = { map {$_ => 1} @$repoids } if $repoids;
  $packids = { map {$_ => 1} @$packids } if $packids;
  my $bconf = Build::read_config($arch);

  BSSrcServer::Projlink::enable_frozenlinks_cache();
  my %channeldata;
  my @res;
  my @projids = @$projids;
  while (@projids) {
    my $projid = shift @projids;
    my $jinfo = { 'name' => $projid };
    my $proj = BSRevision::readproj_local($projid, 1);
    if ($remotemap && (!$proj || $proj->{'remoteurl'})) {
      if ($cgi->{'project'}) {
	# fill remote data if asked for that specific project
	my $r = BSSrcServer::Remote::remoteprojid($projid);
	if ($r) {
	  eval {
	    BSSrcServer::Remote::fetchremoteproj($r, $projid, $remotemap);
	    BSSrcServer::Remote::fetchremoteconfig($r, $projid, $remotemap) if $cgi->{'withconfig'};
	  };
	}
      } elsif (!exists($remotemap->{$projid}) ) {
        # put at least the proto into the remotemap
        my $r = BSSrcServer::Remote::remoteprojid($projid);
        $remotemap->{$projid} = {%$r, 'proto' => 1} if $r;
      }
    }
    next if $cgi->{'remotemaponly'};
    if (!$proj && $cgi->{'parseremote'} && $cgi->{'project'} && $remotemap && $remotemap->{$projid}) {
      $proj = $remotemap->{$projid};
    }
    if ($cgi->{'buildinfo'}) {
      if (!@res) {
        die("project $projid does not exist\n") unless $proj;
        die("repository @{[keys %$repoids]} does not exist in $projid\n") unless grep {$repoids->{$_->{'name'}}} @{$proj->{'repository'} || []};
      } else {
	# kiwi projects, we just need the project data and config
	$packids = undef;
	$repoids = undef;;
	$cgi->{'nopackages'} = 1;
	$cgi->{'parseremote'} = 0;
      }
    }
    next unless $proj;
    for (qw{kind}) {
      $jinfo->{$_} = $proj->{$_} if exists $proj->{$_};
    }

    my %expandedrepos;

    if ($cgi->{'withrepos'}) {
      $jinfo->{'repository'} = $proj->{'repository'} || [];
      if ($repoids && !$cgi->{'buildinfo'}) {
	$jinfo->{'repository'} = [ grep {$repoids->{$_->{'name'}}} @{$proj->{'repository'} || []} ];
      }
      if ($cgi->{'expandedrepos'}) {
	$jinfo->{'repository'} = Storable::dclone($jinfo->{'repository'});
	for my $repo (@{$jinfo->{'repository'}}) {
	  my $base = {};
	  my @prps = expandsearchpath($projid, $repo->{'name'}, $remotemap, $base);
	  $expandedrepos{"$projid/$repo->{'name'}"} = [ @prps ];
	  for my $prp (@prps) {
	    my @s = split('/', $prp, 2);
	    $prp = {'project' => $s[0], 'repository' => $s[1]};
	  }
	  $repo->{'path'} = \@prps;
	  $repo->{'base'} = $base;
	}
      } elsif ($remotemap) {
	for my $repo (@{$jinfo->{'repository'}}) {
	  next if $repoids && !$repoids->{$repo->{'name'}};
	  eval {
	    my @prps = expandsearchpath($projid, $repo->{'name'}, $remotemap);
            $expandedrepos{"$projid/$repo->{'name'}"} = [ @prps ];
	  };
	  $expandedrepos{"$projid/$repo->{'name'}"} = $@ if $@;
	}
      }
    }

    if ($cgi->{'buildinfo'} && !@res) {
      # add all projects from the expanded path
      my $er = $expandedrepos{"$projid/$cgi->{'repository'}->[0]"};
      die($er) unless ref($er);
      eval {
	concatconfigs($projid, $cgi->{'repository'}->[0], $remotemap, @$er);
      };
      for my $prp (@$er) {
        my ($p) = split('/', $prp, 2);
        next if $remotemap && $remotemap->{$p};
	push @projids, $p unless $p eq $projid || grep {$_ eq $p} @projids;
      }
    }

    if ($remotemap) {
      for my $lprojid (map {$_->{'project'}} @{$proj->{'link'} || []}) {
        my $lproj = BSSrcServer::Remote::remoteprojid($lprojid);
	eval {
	  BSSrcServer::Remote::fetchremoteproj($lproj, $lprojid, $remotemap) if $lproj;
	};
        BSSrcServer::Partition::checkpartition($remotemap, $lprojid) if $remotemap->{':partitions'} && !$remotemap->{':partitions'}->{$lprojid};
      }
    }

    if ($cgi->{'withconfig'}) {
      my $config = readstr("$projectsdir/$projid.conf", 1);
      if ($config) {
	#my $s1 = '^\s*macros:\s*$.*?^\s*:macros\s*$';
	#my $s2 = '^\s*macros:\s*$.*\Z';
	#$config =~ s/$s1//gmsi;
	#$config =~ s/$s2//gmsi;
	$jinfo->{'config'} = $config unless $config =~ /^\s*$/s;
      }
    }
    if ($cgi->{'withsrcmd5'} && -s "$projectsdir/$projid.pkg/pattern-MD5SUMS") {
      my $patterns = readstr("$projectsdir/$projid.pkg/pattern-MD5SUMS", 1);
      $jinfo->{'patternmd5'} = Digest::MD5::md5_hex($patterns) if $patterns;
    } elsif ($cgi->{'withsrcmd5'} && $cgi->{'nopackages'}) {
      # used by publisher to get patternmd5
      eval {
	my $rev = getrev($projid, '_pattern');
	my $files = lsrev_expanded($rev);
	$jinfo->{'patternmd5'} = $rev->{'srcmd5'};
      };
    }
    my @packages;
    @packages = findpackages($projid, $proj, 2) unless $cgi->{'nopackages'} || $proj->{'remoteurl'};
    @packages = @{$cgi->{'package'}} if $proj->{'remoteurl'} && $cgi->{'package'} && $cgi->{'parseremote'};
    my $missing_packages = grep {$_ eq ':missing_packages'} @packages;
    if (!$proj->{'remoteurl'} && !$missing_packages && !$cgi->{'nopackages'}) {
      BSSrcServer::Multibuild::prunemultibuild($projid, \@packages);
    }
    if ($missing_packages) {
      @packages = grep {$_ ne ':missing_packages'} @packages;
      $jinfo->{'missingpackages'} = 1;
    }
    next if $repoids && !grep {$repoids->{$_->{'name'}}} @{$proj->{'repository'} || []};
    next if $packids && !grep {$packids->{$_}} @packages;
    for (qw{title description build publish debuginfo useforbuild remoteurl remoteproject download link sourceaccess privacy access lock}) {
      $jinfo->{$_} = $proj->{$_} if exists $proj->{$_};
    }
    if ($proj->{'access'}) {
      # we need the roles if the project is protected, see checkroles() in the scheduler
      my ($person, $group) = BSSrcServer::Access::mergeroles($projid, $proj);
      $jinfo->{'person'} = $person if $person && @$person;
      $jinfo->{'group'} = $group if $group && @$group;
    }
    # Check build flags in project meta data
    # packages inherit the project wide settings and may override them
    my $pdisabled;
    my $pdisable = {};
    my $penable = {};
    undef($penable) if $cgi->{'ignoredisable'};
    if ($jinfo->{'build'} && $penable) {
      for (@{$proj->{'repository'} || []}) {
        my $disen = BSUtil::enabled($_->{'name'}, $jinfo->{'build'}, 1, $arch);
        if ($disen) {
          $penable->{$_->{'name'}} = 1;
        } else {
          $pdisable->{$_->{'name'}} = 1;
        }
      }
      $pdisabled = 1 if !keys(%$penable);
    } else {
      # build is enabled
      undef($penable);
    }
    # check for a global lock
    my $plocked;
    if (!$cgi->{'ignoredisable'} && $jinfo->{'lock'}) {
      for (@{$proj->{'repository'} || []}) {
        if (BSUtil::enabled($_->{'name'}, $jinfo->{'lock'}, 0, $arch)) {
	  $plocked = 1;
	} else {
	  $plocked = undef;	# at least one repo is not locked
	  last;
	}
      }
    }

    # Check package number quota
    my $quota_exceeded;
    if ($BSConfig::bsquotafile) {
      my $pquota = getprojquotapackage($projid);
      $quota_exceeded = 1 if defined($pquota) && @packages > $pquota;
    }

    if (!$cgi->{'ignoredisable'} && !grep {!$_->{'status'} || $_->{'status'} ne 'disabled'} @{$proj->{'repository'} || []}) {
      # either no repositories or all disabled. No need to check packages
      @packages = ();
    }
    @packages = () if $cgi->{'nopackages'};
    my @pinfo;
    my %bconfs;

    my $exclude_all;
    my $exclude_repos;
    if (!$cgi->{'ignoredisable'} && defined($cgi->{'arch'})) {
      $exclude_repos = {};
      $exclude_all = 1;
      for (@{$proj->{'repository'} || []}) {
	if (grep {$_ eq $arch} @{$_->{'arch'} || []}) {
	  undef $exclude_all;
	} else {
          $exclude_repos->{$_->{'name'}} = 1;
	}
      }
    }

    my %packages_multibuild;
    for my $packid (@packages) {
      next unless $packid =~ /(?<!^_product)(?<!^_patchinfo):./ && $packid =~ /^(.*):/;
      $packages_multibuild{$1} = 1;
    }

    my @packages_delayed;
    my $packages_pass = 0;
    while (1) {
      if (!@packages) {
	last if !@packages_delayed || $packages_pass;
	$packages_pass = 1;
	BSSrcServer::Remote::fill_remote_getrev_cache();
	@packages = @packages_delayed;
	next;
      }
      my $packid = shift(@packages);

      if ($packids && !$packids->{$packid}) {
	next unless $packid =~ /(?<!^_product)(?<!^_patchinfo):./ && $packid =~ /^(.*):/;
	next unless $packids->{$1};
      }
      my $pinfo = {'name' => $packid};
      push @pinfo, $pinfo;
      my $pack;
      if ($packid =~ /(?<!^_product)(?<!^_patchinfo):./) {
        my $lpackid = $packid;
	$lpackid =~ s/:[^:]+$//;
        $pack = BSRevision::readpack_local($projid, $lpackid, 1);
      } else {
        $pack = BSRevision::readpack_local($projid, $packid, 1);
      }
      ($pack) = getpackage({}, $projid, $packid) if $proj->{'remoteurl'} && $cgi->{'parseremote'};
      $pack ||= {} if $proj->{'link'};
      if (!$pack) {
	$pinfo->{'error'} = 'no metadata';
	next;
      }
      for (qw{build publish debuginfo useforbuild bcntsynctag sourceaccess privacy access lock releasename}) {
	$pinfo->{$_} = $pack->{$_} if $pack->{$_};
      }
      if (!$pinfo->{'build'}) {
        $pinfo->{'build'}->{'enable'} = $pack->{'enable'} if $pack->{'enable'};
        $pinfo->{'build'}->{'disable'} = $pack->{'disable'} if $pack->{'disable'};
      }
      if ($exclude_all) {
	$pinfo->{'error'} = 'excluded';
	next;
      }

      if ($plocked && !$pinfo->{'lock'}) {
	$pinfo->{'error'} = 'locked';
	next;
      }

      my $enable = defined($penable) ? {%$penable} : undef;
      my $disable = {%$pdisable};
      if (!$cgi->{'ignoredisable'} && $pinfo->{'build'}) {
        for (@{$proj->{'repository'} || []}) {
          my $default = exists($disable->{$_->{'name'}}) ? 0 : 1;
          my $disen = BSUtil::enabled($_->{'name'}, $pinfo->{'build'}, $default, $arch);
          if ($disen) {
            $enable->{$_->{'name'}} = 1;
            delete $disable->{$_->{'name'}};
          } else {
            $disable->{$_->{'name'}} = 1;
            delete $enable->{$_->{'name'}};
          }
        }
      }
      undef($disable) if $enable && !keys(%$enable);
      undef($enable) if $disable && !keys(%$disable);
      if ((!$disable || $pdisabled) && $enable && !%$enable) {
	$pinfo->{'error'} = 'disabled';
	next;
      }
      if ($quota_exceeded) {
	$pinfo->{'error'} = 'quota exceeded';
	next;
      }
      if ($cgi->{'withsrcmd5'} || $cgi->{'withdeps'}) {
        my $rev;
	my $linked = [];
	$BSSrcServer::Remote::collect_remote_getrev = 1 unless $packages_pass;
	eval {
	  $rev = getrev($projid, $packid, 'build', $linked);
	};
	$BSSrcServer::Remote::collect_remote_getrev = 0;
        $pinfo->{'originproject'} = $rev->{'originproject'} if $rev && $rev->{'originproject'};
        $pinfo->{'linked'} = $linked if @$linked;
	if ($@) {
	  $pinfo->{'error'} = $@;
	  $pinfo->{'error'} =~ s/\n$//s;
	  if (!$packages_pass && $pinfo->{'error'} =~ /collect_remote_getrev$/) {
	    pop @pinfo;
	    push @packages_delayed, $packid;
	  }
 	  next;
	}
	if (!$rev || $rev->{'srcmd5'} eq 'empty' || $rev->{'srcmd5'} eq $BSSrcrep::emptysrcmd5) {
	  $pinfo->{'error'} = 'no source uploaded';
	  next;
	}
	$pinfo->{'srcmd5'} = $rev->{'srcmd5'};
	$pinfo->{'rev'} = $rev->{'rev'};
	$pinfo->{'revtime'} = $rev->{'time'} if $rev->{'time'};
	# need the relsynctag/releasename for packages from a project link
	if ($rev->{'originproject'} && !%$pack) {
	  # XXX: what about remote projects?
	  my $lpack = BSRevision::readpack_local($rev->{'originproject'}, $rev->{'package'}, 1);
	  if ($lpack) {
	    for (qw{bcntsynctag releasename}) {
	      $pinfo->{$_} = $lpack->{$_} if $lpack->{$_};
	    }
	  }
	}
	my $files;
	eval {
	  my $linkinfo = {};
          $files = BSRevision::lsrev($rev, $linkinfo);
	  if ($linkinfo->{'xservicemd5'}) {
	    $files = BSSrcServer::Service::handleservice($rev, $files, $linkinfo->{'xservicemd5'});
	    $pinfo->{'srcmd5'} = $rev->{'srcmd5'};
	  }
	  if ($linkinfo->{'xservicemd5'} || $linkinfo->{'lservicemd5'} || $linkinfo->{'lsrcmd5'}) {
	    my $meta = '';
	    $meta .= "$files->{$_}  $_\n" for sort keys %$files;
	    $pinfo->{'verifymd5'} = Digest::MD5::md5_hex($meta);
	  }
	};
	if ($@) {
	  $pinfo->{'error'} = $@;
	  $pinfo->{'error'} =~ s/\n$//s;
 	  next;
	}
	if ($files->{'_service'} && -e "$eventdir/service/${projid}::$packid") {
	  $pinfo->{'error'} = 'source update running';
 	  next;
	}
        if ($files->{'_service_error'}) {
	  $pinfo->{'error'} = 'source service failed';
 	  next;
        }
	if ($files->{'_link'}) {
	  $BSSrcServer::Remote::collect_remote_getrev = 1 unless $packages_pass;
	  eval {
	    $files = BSSrcServer::Link::handlelinks($rev, $files, {'linked' => $linked});
	  };
	  $BSSrcServer::Remote::collect_remote_getrev = 0;
	  if ($@) {
	    $files = "$@";
	    $files =~ s/\n$//;
	  }
	  if (@$linked) {
	    $pinfo->{'linked'} = $linked;
	    if ($remotemap && $remotemap->{':partitions'}) {
	      # we need to have all partition infos set for the links
	      for my $li (@$linked) {
		my $lprojid = $li->{'project'};
		next if $remotemap->{$lprojid} || $remotemap->{':partitions'}->{$lprojid};
		my $lproj = BSRevision::readproj_local($lprojid, 1);
		if ($lproj && !$lproj->{'remoteurl'}) {
		  BSSrcServer::Partition::checkpartition($remotemap, $lprojid, $lproj);
		} else {
		  $remotemap->{':partitions'}->{$lprojid} = 1;  # not on a partition
		}
	      }
	    }
	  }
	  if (!ref $files) {
	    $pinfo->{'error'} = defined($files) ? $files : "could not get file list";
	    if (!$packages_pass && $pinfo->{'error'} =~ /collect_remote_getrev$/) {
	      pop @pinfo;
	      push @packages_delayed, $packid;
	    }
	    next;
	  }
	  $pinfo->{'srcmd5'} = $rev->{'srcmd5'};
	  my $meta = '';
	  $meta .= "$files->{$_}  $_\n" for sort keys %$files;
	  $pinfo->{'verifymd5'} = Digest::MD5::md5_hex($meta);
	}
	# get rid of old multibuild packages, this assumes the main package comes first
	@packages = grep {!/^\Q$packid\E:/} @packages if $packages_multibuild{$packid};
	# get current multibuild state
	my $mb = BSSrcServer::Multibuild::updatemultibuild($projid, $packid, $files, 1);
	unshift @packages, map {"$packid:$_"} @{$mb->{'flavor'} || $mb->{'package'} || []} if $mb;

	if ($packid eq '_pattern') {
	  $jinfo->{'patternmd5'} = $pinfo->{'srcmd5'};
	  $pinfo->{'error'} = 'excluded';
	  next;
	}
	if ($files->{'_aggregate'}) {
	  my $aggregatelist = BSRevision::revreadxml($rev, '_aggregate', $files->{'_aggregate'}, $BSXML::aggregatelist, 1);
	  if (!$aggregatelist) {
	    $pinfo->{'error'} = "bad aggregatelist data";
	    next;
	  }
          eval {
	    BSVerify::verify_aggregatelist($aggregatelist);
          };
	  if ($@) {
	    my $err = $@;
	    $err =~ s/\n$//s;
	    $pinfo->{'error'} = "bad aggregatelist: $err";
	    next;
	  }
	  $pinfo->{'aggregatelist'} = $aggregatelist;
	  if ($remotemap && $aggregatelist) {
	    for my $aggregate (@{$aggregatelist->{'aggregate'} || []}) {
	      my $aprojid = $aggregate->{'project'};
	      next if $remotemap->{$aprojid} && !$remotemap->{$aprojid}->{'proto'};
	      my $aproj = BSRevision::readproj_local($aprojid, 1);
	      if (!$aproj || $aproj->{'remoteurl'}) {
		$aproj = BSSrcServer::Remote::remoteprojid($aprojid);
		eval {
		  BSSrcServer::Remote::fetchremoteproj($aproj, $aprojid, $remotemap) if $aproj;
		};
	      } else {
		BSSrcServer::Partition::checkpartition($remotemap, $aprojid, $aproj) if $remotemap->{':partitions'} && !$remotemap->{':partitions'}->{$aprojid};
	      }
	    }
	  }
	  if (($enable && %$enable) || ($disable && %$disable)) {
	    my @dinfo = ();
	    for my $repo (@{$proj->{'repository'} || []}) {
	      my $repoid = $repo->{'name'};
	      next if $repoids && !$repoids->{$repoid};
	      if ((!$disable || $disable->{$repoid}) && !(!$enable || $enable->{$repoid})) {
	        push @dinfo, {'repository' => $repoid, 'error' => 'disabled'};
		next;
	      }
	    }
	    $pinfo->{'info'} = \@dinfo if @dinfo;
	  }
	} elsif ($files->{'_patchinfo'}) {
	  my $patchinfo = BSRevision::revreadxml($rev, '_patchinfo', $files->{'_patchinfo'}, $BSXML::patchinfo, 1);
          if (!$patchinfo) {
	    $pinfo->{'error'} = "bad patchinfo data";
	    next;
	  }
          eval {
	    BSVerify::verify_patchinfo($patchinfo);
          };
	  if ($@) {
	    my $err = $@;
	    chomp $err;
	    $pinfo->{'error'} = "bad patchinfo: $err";
	    next;
	  }
	  $pinfo->{'patchinfo'} = $patchinfo;
	  if (($enable && %$enable) || ($disable && %$disable)) {
	    my @dinfo = ();
	    for my $repo (@{$proj->{'repository'} || []}) {
	      my $repoid = $repo->{'name'};
	      next if $repoids && !$repoids->{$repoid};
	      if ((!$disable || $disable->{$repoid}) && !(!$enable || $enable->{$repoid})) {
	        push @dinfo, {'repository' => $repoid, 'error' => 'disabled'};
		next;
	      }
	    }
	    $pinfo->{'info'} = \@dinfo if @dinfo;
	  }
	} elsif ($files->{'_channel'}) {
	  if (!exists($channeldata{$files->{'_channel'}})) {
	    eval {
	      my $channel = BSRevision::revreadxml($rev, '_channel', $files->{'_channel'}, $BSXML::channel);
	      BSVerify::verify_channel($channel);
	      $channeldata{$files->{'_channel'}} = $channel;
	    };
	    if ($@) {
	      my $err = $@;
	      chomp $err;
	      $channeldata{$files->{'_channel'}} = $err;
	    }
	  }
	  my $channel = $channeldata{$files->{'_channel'}} || 'bad data';
          if (!ref($channel)) {
	    $pinfo->{'error'} = "bad channel: $channel";
	    next;
	  }
          $pinfo->{'channelmd5'} = $files->{'_channel'};
	  if (($enable && %$enable) || ($disable && %$disable)) {
	    my @dinfo = ();
	    for my $repo (@{$proj->{'repository'} || []}) {
	      my $repoid = $repo->{'name'};
	      next if $repoids && !$repoids->{$repoid};
	      if ((!$disable || $disable->{$repoid}) && !(!$enable || $enable->{$repoid})) {
	        push @dinfo, {'repository' => $repoid, 'error' => 'disabled'};
		next;
	      }
	    }
	    $pinfo->{'info'} = \@dinfo if @dinfo;
	  }
        } elsif ($cgi->{'withdeps'}) {
	  my @dinfo;

	  if (!%$files) {
	    $pinfo->{'error'} = 'empty';
	    next;
	  }
	  $pinfo->{'constraintsmd5'} = $files->{'_constraints'} if $files->{'_constraints'};
	  if ($files->{'_buildenv'}) {
	    $pinfo->{'hasbuildenv'} = 1;
	    readbuildenv($pinfo, $rev, $files, (keys %$repoids)[0], $arch) if $cgi->{'buildinfo'};
	  }
	  for my $repo (@{$proj->{'repository'} || []}) {
	    my $repoid = $repo->{'name'};
	    next if $repoids && !$repoids->{$repoid};

	    my $rinfo = {'repository' => $repoid};
	    push @dinfo, $rinfo;
	    if ($exclude_repos && $exclude_repos->{$repoid}) {
	      $rinfo->{'error'} = 'excluded';
	      next;
	    }
	    if ((!$disable || $disable->{$repoid}) && !(!$enable || $enable->{$repoid})) {
	      $rinfo->{'error'} = 'disabled';
	      next;
	    }
            if (!$bconfs{$repoid}) {
	      print "calculating config for $projid/$repoid $arch\n";
	      my $path = $expandedrepos{"$projid/$repoid"};
	      if (!$path) {
	        eval {
		  my @path = expandsearchpath($projid, $repoid, $remotemap);
		  $expandedrepos{"$projid/$repoid"} = \@path;
		};
		$expandedrepos{"$projid/$repoid"} = $@ if $@;
		$path = $expandedrepos{"$projid/$repoid"};
	      }
	      eval {
		die($path) unless ref $path;
		my $c = concatconfigs($projid, $repoid, $remotemap, @$path);
	        $bconfs{$repoid} = Build::read_config($arch, [ split("\n", $c) ]);
	      };
	      if ($@) {
	        my $err = $@;
	        chomp $err;
	        $bconfs{$repoid} = {'error' => $err};
	      }
            }
	    my $conf = $bconfs{$repoid};
	    if ($conf->{'error'}) {
	      $rinfo->{'error'} = $conf->{'error'};
	      next;
	    }
	    my $type = $conf->{'type'};
	    if (!$type || $type eq 'UNDEFINED') {
	      $rinfo->{'error'} = 'bad build configuration, no build type defined or detected';
	      next;
	    }
            my $file = findfile($rev, $repoid, $type, $files);
	    if (!defined($file)) {
	      $rinfo->{'error'} = 'excluded';
	      next;
	    }
	    $rinfo->{'file'} = $file;
	    my $buildtype = Build::recipe2buildtype($file);
	    if (!$buildtype) {
	      $rinfo->{'error'} = "don't know how to build $file";
	      next;
	    }
	    if (($type eq 'kiwi' || $buildtype eq 'kiwi') && $BSConfig::kiwiprojects && !$cgi->{'ignoredisable'}) {
	      my %kiwiprojects = map {$_ => 1} @$BSConfig::kiwiprojects;
	      if (!$kiwiprojects{$projid}) {
		$rinfo->{'error'} = 'kiwi image building is not enabled for this project';
	        next;
	      }
	    }
	    # get build dependency info
	    local $conf->{'buildflavor'};
	    local $conf->{'obspackage'} = $packid;
	    if ($packid =~ /(?<!^_product)(?<!^_patchinfo):./) {
	      $packid =~ /^(.*):(.*?)$/;
	      $conf->{'obspackage'} = $1;
	      $conf->{'buildflavor'} = $2;
	    }
	    my $d;
	    eval {
	      $d = Build::parse_typed($conf, BSRevision::revfilename($rev, $file, $files->{$file}), $buildtype);
	    };
	    if ($@) {
	      $d = {'error' => $@};
	      $d->{'error'} =~ s/\n.*//s;
	    }
	    $d = { 'error' => "can not parse $file" } unless $d;
	    data2utf8xml($d);
	    if ($d->{'error'}) {
	      $rinfo->{'error'} = $d->{'error'};
	      next;
	    }
	    if (!defined($d->{'name'})) {
	      $rinfo->{'error'} = "can not parse name from $file";
	      next;
	    }
	    my $version = defined($d->{'version'}) ? $d->{'version'} : 'unknown';
	    $pinfo->{'versrel'} ||= "$version-$rev->{'vrev'}";
	    $rinfo->{'name'} = $d->{'name'};
	    $rinfo->{'dep'} = $d->{'deps'};
	    if ($d->{'prereqs'}) {
	      my %deps = map {$_ => 1} (@{$d->{'deps'} || []}, @{$d->{'subpacks'} || []});
	      my @prereqs = grep {!$deps{$_} && !/^%/} @{$d->{'prereqs'}};
	      $rinfo->{'prereq'} = \@prereqs if @prereqs;
	    }
            # add all source services to be used at build time
	    if ($files->{'_service'}) {
              my $services = BSRevision::revreadxml($rev, '_service', $files->{'_service'}, $BSXML::services, 1) || {};
              for my $service (@{$services->{'service'} || []}) {
                next unless $service->{'mode'} && $service->{'mode'} eq 'buildtime';
                push @{$rinfo->{'buildtimeservice'}}, $service->{'name'};
              }
            }
	    # KIWI Products support debugmedium and sourcemedium filters
	    if ($type eq 'kiwi' && ($d->{'imagetype'}[0] || '') eq 'product') {
	      $rinfo->{'nodbgpkgs'} = 1 if defined($d->{'debugmedium'}) && $d->{'debugmedium'} <= 0;
	      $rinfo->{'nosrcpkgs'} = 1 if defined($d->{'sourcemedium'}) && $d->{'sourcemedium'} <= 0;
	    }
	    # KIWI Images don't build with local arch
	    if ($type eq 'kiwi' && ($d->{'imagetype'}[0] || '') ne 'product') {
	      $rinfo->{'error'} = 'excluded' if defined($BSConfig::localarch) && $arch eq 'local';
	    }
	    if ($type eq 'kiwi' && ($d->{'imagetype'}[0] || '') eq 'product') {
	      # KIWI Products always build on the first repo arch
	      $rinfo->{'imagearch'} = [ @{$d->{'exclarch'}} ] if $d->{'exclarch'};
	    } else {
	      my $myarch = $conf->{'target'} ? (split('-', $conf->{'target'}))[0] : $arch;
	      $rinfo->{'error'} = 'excluded' if $d->{'exclarch'} && !grep {$_ eq $myarch} @{$d->{'exclarch'}};
	      $rinfo->{'error'} = 'excluded' if $d->{'badarch'} && grep {$_ eq $myarch} @{$d->{'badarch'}};
	    }
	    for ('imagetype', 'extrasource') {
	      $rinfo->{$_} = $d->{$_} if exists $d->{$_};
	    }
	    for (@{$d->{'path'} || []}) {
	      my $r = { 'project' => $_->{'project'}, 'repository' => $_->{'repository'} };
	      $r->{'priority'} = $_->{'priority'} if defined $_->{'priority'};
	      push @{$rinfo->{'path'}}, $r;
	    }
	    for (@{$d->{'containerpath'} || []}) {
	      my $r = { 'project' => $_->{'project'}, 'repository' => $_->{'repository'} };
	      $r->{'priority'} = $_->{'priority'} if defined $_->{'priority'};
	      push @{$rinfo->{'containerpath'}}, $r;
	    }
	    if ($remotemap && ($rinfo->{'path'} || $rinfo->{'containerpath'})) {
	      my @kiwipath = (@{$rinfo->{'path'} || []}, @{$rinfo->{'containerpath'} || []});
	      @kiwipath = map {"$_->{'project'}/$_->{'repository'}"} grep {$_->{'project'} ne '_obsrepositories'} @kiwipath;
	      # simple way to fill the remote map
	      eval {
		concatconfigs($projid, $repoid, $remotemap, @kiwipath);
	      };
	      if ($cgi->{'buildinfo'} && !@res) {
		# add kiwipath projects
	        for my $prp (@kiwipath) {
		  my ($p) = split('/', $prp, 2);
		  next if $remotemap && $remotemap->{$p};
		  push @projids, $p unless $p eq $projid || grep {$_ eq $p} @projids;
	        }
	      }
	    }
	  }
	  $pinfo->{'info'} = \@dinfo if @dinfo;
	}
      }
    }
    $jinfo->{'package'} = \@pinfo;
    push @res, $jinfo;
  }
  BSSrcServer::Projlink::disable_frozenlinks_cache();

  my $ret = {'repoid' => $datarepoid, 'project' => \@res};
  if ($remotemap) {
    delete $remotemap->{':partition'};
    delete $remotemap->{':partitions'};
  }
  #print Dumper($remotemap);
  if ($remotemap && %$remotemap) {
    for my $p (sort keys %$remotemap) {
      next unless $remotemap->{$p};
      my $r = {'project' => $p};
      # keep in sync with BSXML! (we only use access/publish from the flags)
      for (qw{kind root remoteurl remoteproject remoteroot partition proto config publish access person group repository error}) {
        $r->{$_} = $remotemap->{$p}->{$_} if defined($remotemap->{$p}->{$_});
      }
      $r->{'error'} =~ s/\n$// if $r->{'error'};
      push @{$ret->{'remotemap'}}, $r;
    }
  }
  if (%channeldata) {
    for my $md5 (sort keys %channeldata) {
      next unless ref($channeldata{$md5});
      push  @{$ret->{'channeldata'}}, {'md5' => $md5, 'channel' => $channeldata{$md5} };
    }
  }
  if ($cgi->{'view'} && $cgi->{'view'} eq 'storable') {
    return ($ret, \&BSUtil::tostorable, 'Content-Type: application/octet-stream');
  }
  return ($ret, $BSXML::projpack);
}

sub getprojectlist {
  my ($cgi) = @_;
  my @projects = findprojects($cgi->{'deleted'});
  @projects = map {{'name' => $_}} @projects;
  return ({'entry' => \@projects}, $BSXML::dir);
}

sub getproject {
  my ($cgi, $projid) = @_;
  my $proj = readproject($projid, undef, $cgi->{'rev'});
  return ($proj, $BSXML::proj);
}

#########################################################################

sub pubkey2sslcert {
  my ($projid, $pubkeyfile, $signkeyfile) = @_;
  die("don't know how to generate a ssl cert\n") unless $BSConfig::sign;
  $pubkeyfile ||= "$projectsdir/$projid.pkg/_pubkey";
  $signkeyfile ||= "$projectsdir/$projid.pkg/_signkey";
  my @signargs;
  push @signargs, '--project', $projid if $BSConfig::sign_project;
  my $cert = '';
  eval {
    $cert = BSUtil::xsystem(undef, $BSConfig::sign, @signargs, '-P', $signkeyfile, '-C', $pubkeyfile);
  };
  if ($@) {
    die("Need an RSA key for openssl signing, please create a new key for $projid\n") if $@ =~ /not an RSA private key/i;
    die($@);
  }
  return $cert;
}

sub updatesslcert {
  my ($projid, $pubkeyfile, $signkeyfile) = @_;
  my $rev = BSRevision::getrev_meta($projid, undef);
  return undef unless $rev;
  my $files = BSRevision::lsrev($rev);
  return undef unless $files->{'_sslcert'};
  my $cert = pubkey2sslcert($projid, $pubkeyfile, $signkeyfile);
  mkdir_p($uploaddir);
  writestr("$uploaddir/sslcert.$$", undef, $cert);
  return "$uploaddir/sslcert.$$";
}

sub createkey {
  my ($cgi, $projid) = @_;
  $cgi->{'comment'} ||= 'create sign key';
  die("don't know how to create a key\n") unless $BSConfig::sign;
  die("404 project $projid does not exist\n") unless -s "$projectsdir/$projid.xml";
  mkdir_p($uploaddir);
  unlink("$uploaddir/signkey.$$");
  my @keyargs = ('rsa@2048', '800');
  my @signargs;
  push @signargs, '--project', $projid if $BSConfig::sign_project;
  push @signargs, '-P', "$uploaddir/signkey.$$";
  #push @signargs, '-h', 'sha256';
  my $obsname = $BSConfig::obsname || 'build.opensuse.org';
  my $pubkey = '';
  local *F;
  open(F, '-|', $BSConfig::sign, @signargs, '-g', @keyargs, "$projid OBS Project", "$projid\@$obsname") || die("$BSConfig::sign: $!\n");
  1 while sysread(F, $pubkey, 4096, length($pubkey));
  close(F) || die("$BSConfig::sign: $?\n");
  die("sign did not create signkey\n") unless -s "$uploaddir/signkey.$$";
  mkdir_p("$projectsdir/$projid.pkg");
  writestr("$uploaddir/pubkey.$$", undef, $pubkey);
  my $certfile = updatesslcert($projid, "$uploaddir/pubkey.$$", "$uploaddir/signkey.$$");
  BSRevision::addrev_meta_replace($cgi, $projid, undef,
	[ "$uploaddir/pubkey.$$",  "$projectsdir/$projid.pkg/_pubkey",  '_pubkey' ],
	[ "$uploaddir/signkey.$$", "$projectsdir/$projid.pkg/_signkey", '_signkey' ],
	[ $certfile, undef, '_sslcert' ]);
  return $BSStdServer::return_ok;
}

sub extendkey {
  my ($cgi, $projid) = @_;
  $cgi->{'comment'} ||= 'extend public key expiry date';
  die("don't know how to extend a key\n") unless $BSConfig::sign;
  die("project does not have a key\n") unless -s "$projectsdir/$projid.pkg/_pubkey";
  die("project does not have a signkey\n") unless -s "$projectsdir/$projid.pkg/_signkey";
  my @keyargs = ('800');
  my @signargs;
  push @signargs, '--project', $projid if $BSConfig::sign_project;
  push @signargs, '-P', "$projectsdir/$projid.pkg/_signkey";
  my $pubkey = '';
  local *F;
  open(F, '-|', $BSConfig::sign, @signargs, '-x', @keyargs, "$projectsdir/$projid.pkg/_pubkey") || die("$BSConfig::sign: $!\n");
  1 while sysread(F, $pubkey, 4096, length($pubkey));
  close(F) || die("$BSConfig::sign: $?\n");
  mkdir_p($uploaddir);
  writestr("$uploaddir/pubkey.$$", undef, $pubkey);
  my $certfile = updatesslcert($projid, "$uploaddir/pubkey.$$");
  BSRevision::addrev_meta_replace($cgi, $projid, undef,
	[ "$uploaddir/pubkey.$$",  "$projectsdir/$projid.pkg/_pubkey",  '_pubkey' ],
	[ $certfile, undef, '_sslcert' ]);
  return $BSStdServer::return_ok;
}

sub deletekey {
  my ($cgi, $projid) = @_;
  $cgi->{'comment'} ||= 'delete sign key';
  BSConfiguration::check_configuration_once();
  if ($BSConfig::forceprojectkeys) {
    my $pprojid = $projid;
    $pprojid =~ s/:[^:]*$//;
    my $sk;
    ($sk) = getsignkey({}, $pprojid) if $projid ne $pprojid;
    die("must have a key for signing in this or upper project\n") unless $sk;
  }
  BSRevision::addrev_meta_replace($cgi, $projid, undef,
	[ undef, "$projectsdir/$projid.pkg/_pubkey",  '_pubkey' ],
	[ undef, "$projectsdir/$projid.pkg/_signkey", '_signkey' ],
	[ undef, undef,                               '_sslcert' ]);
  rmdir("$projectsdir/$projid.pkg");
  return $BSStdServer::return_ok;
}

sub getpubkey {
  my ($cgi, $projid) = @_;
  my $pubkey;

  my $proj = BSRevision::readproj_local($projid, 1, $cgi->{'rev'});
  $proj = BSSrcServer::Remote::remoteprojid($projid) if !$proj || $proj->{'remoteurl'};
  die("404 project '$projid' does not exist\n") unless $proj;

  if ($proj->{'remoteurl'}) {
    $pubkey = BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/source/$proj->{'remoteproject'}/_pubkey", 'proxy' => $proxy}, undef);
  } else {
    if ($cgi->{'rev'}) {
      my $rev = BSRevision::getrev_meta($projid, undef, $cgi->{'rev'});
      my $files = $rev ? BSRevision::lsrev($rev) : {};
      $pubkey = BSRevision::revreadstr($rev, '_pubkey', $files->{'_pubkey'}, 1) if $files->{'_pubkey'};
    } else {
      $pubkey = readstr("$projectsdir/$projid.pkg/_pubkey", 1);
    }
  }
  die("404 $projid: no pubkey available\n") unless $pubkey;
  return ($pubkey, 'Content-Type: text/plain');
}

#########################################################################

sub putproject {
  my ($cgi, $projid) = @_;
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$");
  die("upload failed\n") unless $uploaded;
  my $proj = readxml("$uploaddir/$$", $BSXML::proj);
  $proj->{'name'} = $projid unless defined $proj->{'name'};
  BSVerify::verify_proj($proj, $projid);
  writexml("$uploaddir/$$.2", undef, $proj, $BSXML::proj);
  unlink("$uploaddir/$$");
  my $oldproj = BSRevision::readproj_local($projid, 1);
  notify($oldproj ? "SRCSRV_UPDATE_PROJECT" : "SRCSRV_CREATE_PROJECT", { "project" => $projid, "sender" => ($cgi->{'user'} || "unknown") });
  mkdir_p($projectsdir) || die("creating $projectsdir: $!\n");
  BSRevision::addrev_meta_replace($cgi, $projid, undef, [ "$uploaddir/$$.2", "$projectsdir/$projid.xml", '_meta' ]);
  BSConfiguration::check_configuration_once();
  if ($BSConfig::forceprojectkeys) {
    my ($sk) = getsignkey({}, $projid);
    createkey({ %$cgi, 'comment' => 'autocreate key' }, $projid) if $sk eq '';
  }

  my %except = map {$_ => 1} qw{title description person group url attributes};
  if (!BSUtil::identical($oldproj, $proj, \%except)) {
    my $type = ($cgi->{'lowprio'}) ? 'lowprioproject' : 'project';
    if ($proj->{'remoteurl'} || ($oldproj || {})->{'remoteurl'}) {
      # inform all repserves about a remote project
      # need to add the event here since notify_all_repservers() doesn't do it
      my $ev = {'type' => $type, 'project' => $projid};
      addevent($ev);
      notify_all_repservers($type, $projid);
    } else {
      notify_repservers($type, $projid);
    }
  }

  $proj = BSRevision::readproj_local($projid);
  return ($proj, $BSXML::proj);
}

sub delproject {
  my ($cgi, $projid) = @_;

  $cgi->{'comment'} ||= 'project was deleted';
  die("404 project '$projid' does not exist\n") unless -e "$projectsdir/$projid.xml";

  # add delete commit to both source and meta
  BSRevision::addrev_local_replace($cgi, $projid, undef);
  BSRevision::addrev_meta_replace($cgi, $projid, undef);

  unlink("$projectsdir/$projid.conf");
  unlink("$projectsdir/$projid.xml");
  notify_repservers('project', $projid);

  notify("SRCSRV_DELETE_PROJECT", { "project" => $projid, "comment" => $cgi->{'comment'}, "sender" => ($cgi->{'user'} || "unknown"), "requestid" => $cgi->{'requestid'} });

  # send reply
  BSServer::reply($BSStdServer::return_ok);
  BSServer::done(1);

  # now delete all packages
  eval {
    if (-d "$projectsdir/$projid.pkg") {
      mkdir_p("$projectsdir/_deleted/$projid.pkg");
      # make room in old delete area by deleting all old packages
      BSRevision::delete_deleted($cgi, $projid);
      # make sure we have mrev files for all packages
      for my $f (ls("$projectsdir/$projid.pkg")) {
	next unless $f =~ /^(.*)\.xml$/;
        BSRevision::addrev_meta_replace($cgi, $projid, $1) unless -f "$projectsdir/$projid.pkg/$1.mrev";
      }
      # now do the real delete on all of those files
      for my $f (ls("$projectsdir/$projid.pkg")) {
        if ($f =~ /^(.*)\.m?rev(?:\.del)?$/) {
	  BSRevision::delete_rev($cgi, $projid, $1, "$projectsdir/$projid.pkg/$f", "$projectsdir/_deleted/$projid.pkg/$f");
        }
        unlink("$projectsdir/$projid.pkg/$f");
      }
      rmdir("$projectsdir/$projid.pkg") || die("rmdir $projectsdir/$projid.pkg: $!\n");
    }
  };
  warn($@) if $@;
  return undef;
}

sub undeleteproject {
  my ($cgi, $projid) = @_;

  die("404 project '$projid' already exists\n") if -e "$projectsdir/$projid.xml";
  die("404 project '$projid' is not deleted\n") unless -e "$projectsdir/_deleted/$projid.pkg";
  $cgi->{'comment'} ||= 'project was undeleted';
  mkdir_p($uploaddir);
  mkdir_p("$projectsdir/$projid.pkg");
  for my $f (ls("$projectsdir/_deleted/$projid.pkg")) {
    if ($f =~ /\.m?rev\.del$/) {
      BSUtil::cp("$projectsdir/_deleted/$projid.pkg/$f", "$uploaddir/$$.2", "$projectsdir/$projid.pkg/$f");
    } elsif ($f =~ /^(.*)\.(m?rev)$/) {
      BSRevision::undelete_rev($cgi, $projid, $1, "$projectsdir/_deleted/$projid.pkg/$f", "$projectsdir/$projid.pkg/$f");
    }
  }
  notify_repservers('project', $projid);
  notify("SRCSRV_UNDELETE_PROJECT", { "project" => $projid, "comment" => $cgi->{'comment'}, "sender" => ($cgi->{'user'} || "unknown") });

  return $BSStdServer::return_ok;
}

#########################################################################

sub getpackagelist {
  my ($cgi, $projid, $repoid, $arch) = @_;
  my $origins = $cgi->{'noorigins'} ? undef : {};
  my $proj;
  $proj = checkprojrepoarch($projid, $repoid, $arch, 1) unless $cgi->{'deleted'};
  my @packages = findpackages($projid, $proj, 0, $origins, !$cgi->{'expand'}, $cgi->{'deleted'});
  for (@packages) {
    $_ = {'name' => $_};
    $_->{'originproject'} = $origins->{$_->{'name'}} if $origins && $origins->{$_->{'name'}} ne $projid;
    $_->{'originpackage'} = $1 if $_->{'name'} =~ /^(.*?)(?<!^_product)(?<!^_patchinfo):./;
  }
  return ({'entry' => \@packages}, $BSXML::dir);
}

sub getpackage {
  my ($cgi, $projid, $packid) = @_;
  if ($cgi->{'meta'} || $cgi->{'view'}) {
    $cgi->{'meta'} ||= 1;
    return getfile($cgi, $projid, $packid, '_meta');
  }
  my $proj;
  $proj = checkprojrepoarch($projid, undef, undef, 1) unless $cgi->{'deleted'};
  if ((!$proj || !$proj->{'remoteurl'}) && ($cgi->{'rev'} || $cgi->{'deleted'} || $packid eq '_project')) {
    # return the exact file here
    # we also do not look at project links
    # we return the data as string so that the md5 sum matches
    my $rev = BSRevision::getrev_meta($projid, $packid, $cgi->{'rev'}, $cgi->{'deleted'});
    my $files = BSRevision::lsrev($rev);
    die("404 _meta: no such file\n") unless $files->{'_meta'};
    my $meta = BSRevision::revreadstr($rev, '_meta', $files->{'_meta'});
    return ($meta);
  }
  my $pack = readpackage($projid, $proj, $packid, $cgi->{'rev'});
  return ($pack, $BSXML::pack);
}

sub putpackage {
  my ($cgi, $projid, $packid) = @_;
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$");
  die("upload failed\n") unless $uploaded;
  my $pack = readxml("$uploaddir/$$", $BSXML::pack);
  $pack->{'name'} = $packid unless defined $pack->{'name'};
  BSVerify::verify_pack($pack, $packid);
  writexml("$uploaddir/$$.2", undef, $pack, $BSXML::pack);
  unlink("$uploaddir/$$");
  my $proj = BSRevision::readproj_local($projid);
  die("$projid is a remote project\n") if $proj->{'remoteurl'};
  if ($packid eq '_product' && ! -e "$projectsdir/$projid.pkg/$packid.xml") {
    # creating a _product package, make sure that there is no _product:xxx package
    my @pkgs = BSRevision::lspackages_local($projid);
    die("cannot create '$packid' if _product:* packages already exist\n") if grep {/^_product:/} @pkgs;
  }
  if (($packid =~ /^_product:/) && ! -e "$projectsdir/$projid.pkg/$packid.xml") {
    die("403 cannot create '$packid' if a '_product' package exists\n") if -e "$projectsdir/$projid.pkg/_product.xml";
  }
  mkdir_p("$projectsdir/$projid.pkg");

  my $oldpack = BSRevision::readpack_local($projid, $packid, 1);
  notify($oldpack ? "SRCSRV_UPDATE_PACKAGE" : "SRCSRV_CREATE_PACKAGE", { "project" => $projid, "package" => $packid, "sender" => ($cgi->{'user'} || "unknown")});

  BSRevision::addrev_meta_replace($cgi, $projid, $packid, [ "$uploaddir/$$.2", "$projectsdir/$projid.pkg/$packid.xml", '_meta' ]);
  my %except = map {$_ => 1} qw{title description devel person group url};
  if (!BSUtil::identical($oldpack, $pack, \%except)) {
    notify_repservers('package', $projid, $packid);
  }
  $pack = BSRevision::readpack_local($projid, $packid);
  return ($pack, $BSXML::pack);
}

sub delpackage {
  my ($cgi, $projid, $packid) = @_;
  $cgi->{'comment'} ||= 'package was deleted';
  die("404 project '$projid' does not exist\n") unless -e "$projectsdir/$projid.xml";
  die("404 package '$packid' does not exist in project '$projid'\n") unless -e "$projectsdir/$projid.pkg/$packid.xml";
  if ($packid =~ /^_product:/) {
    die("403 cannot delete '$packid' if a '_product' package exists\n") if -e "$projectsdir/$projid.pkg/_product.xml";
  }
  # kill upload revision
  unlink("$projectsdir/$projid.pkg/$packid.upload-MD5SUMS");

  # add delete commit to both source and meta
  BSRevision::addrev_local_replace($cgi, $projid, $packid);
  BSRevision::addrev_meta_replace($cgi, $projid, $packid);

  # now do the real delete of the package
  BSRevision::delete_rev($cgi, $projid, $packid, "$projectsdir/$projid.pkg/$packid.rev", "$projectsdir/$projid.pkg/$packid.rev.del");
  BSRevision::delete_rev($cgi, $projid, $packid, "$projectsdir/$projid.pkg/$packid.mrev", "$projectsdir/$projid.pkg/$packid.mrev.del");

  # get rid of the generated product packages as well
  if ($packid eq '_product') {
    BSSrcServer::Product::expandproduct($projid, $packid, undef, undef);
  }
  notify_repservers('package', $projid, $packid);
  notify("SRCSRV_DELETE_PACKAGE", { "project" => $projid, "package" => $packid, "sender" => ($cgi->{'user'} || "unknown"), "comment" => $cgi->{'comment'}, "requestid" => $cgi->{'requestid'} });
  return $BSStdServer::return_ok;
}

sub undeletepackage {
  my ($cgi, $projid, $packid) = @_;
  $cgi->{'comment'} ||= 'package was undeleted';
  die("404 project '$projid' does not exist\n") unless -e "$projectsdir/$projid.xml";
  die("403 package '$packid' already exists\n") if -e "$projectsdir/$projid.pkg/$packid.xml";
  die("403 package '$packid' was not deleted\n") unless -e "$projectsdir/$projid.pkg/$packid.rev.del";
  BSRevision::undelete_rev($cgi, $projid, $packid, "$projectsdir/$projid.pkg/$packid.mrev.del", "$projectsdir/$projid.pkg/$packid.mrev");
  if (-s "$projectsdir/$projid.pkg/$packid.rev.del") {
    BSRevision::undelete_rev($cgi, $projid, $packid, "$projectsdir/$projid.pkg/$packid.rev.del", "$projectsdir/$projid.pkg/$packid.rev");
  }
  if ($packid eq '_product') {
    my $rev = BSRevision::getrev_local($projid, $packid);
    if ($rev) {
      my $files = BSRevision::lsrev($rev);
      BSSrcServer::Product::expandproduct($projid, $packid, $rev, $files, $rev->{'user'});
    }
  }
  notify_repservers('package', $projid, $packid);
  notify("SRCSRV_UNDELETE_PACKAGE", { "project" => $projid, "package" => $packid, "sender" => ($cgi->{'user'} || "unknown"), "comment" => $cgi->{'comment'} });
  return $BSStdServer::return_ok;
}

sub getpackagehistory {
  my ($cgi, $projid, $packid) = @_;
  my @res;
  my $revfile;
  $packid = '_project' unless defined $packid;

  if (!$cgi->{'deleted'}) {
    my $proj = checkprojrepoarch($projid, undef, undef, 1);
    if ($proj->{'remoteurl'}) {
      my @args = BSRPC::args($cgi, 'rev', 'meta', 'limit');
      my $h = BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/source/$proj->{'remoteproject'}/$packid/_history", 'proxy' => $proxy}, $BSXML::revisionlist, @args);
      return ($h, $BSXML::revisionlist);
    }
    if ($packid ne '_project' && ! -e "$projectsdir/$projid.pkg/$packid.xml") {
      # check project links
      die("404 package '$packid' does not exist\n") unless $proj->{'link'};
      $cgi->{'_checked'} ||= {};
      $cgi->{'_checked'}->{$projid} = 1;
      for my $lprojid (map {$_->{'project'}} @{$proj->{'link'}}) {
	next if $cgi->{'_checked'}->{$lprojid};
	$cgi->{'_checked'}->{$lprojid} = 1;
	my $h;
	eval {
	  $h = (getpackagehistory($cgi, $lprojid, $packid))[0];
	};
	die($@) if $@ && $@ !~ /^404/;
	return ($h, $BSXML::revisionlist) if $h;
      }
      die("404 package '$packid' does not exist\n");
    }
  }

  $revfile = "$projectsdir/$projid.pkg";
  $revfile = "$projectsdir/_deleted/$projid.pkg" if $packid eq '_project' && $cgi->{'deleted'};
  $revfile .= $cgi->{'meta'} ? "/$packid.mrev" : "/$packid.rev";
  if ($packid ne '_project' && $cgi->{'deleted'}) {
    $revfile .= '.del';
    if (! -e $revfile && ! -e "$projectsdir/$projid.xml" && -e "$projectsdir/_deleted/$projid.pkg") {
      $revfile = "$projectsdir/_deleted/$projid.pkg/$packid.mrev";
    }
  }
  my $filter;
  if ($cgi->{'rev'}) {
    $filter = sub { return $cgi->{'rev'} eq $_[0]->{'rev'} || $cgi->{'rev'} eq $_[0]->{'srcmd5'} ? 1 : 0 };
  }
  for (BSFileDB::fdb_getall_reverse($revfile, $srcrevlay, $cgi->{'limit'}, $filter)) {
    $_->{'comment'} = str2utf8xml($_->{'comment'}) if $_->{'comment'};
    unshift @res, $_;
  }
  return ({'revision' => \@res}, $BSXML::revisionlist);
}

##########################################################################

sub expandsearchpath {
  my ($projid, $repoid, $remotemap, $base) = @_;
  my %done;
  my @ret;
  my @path = {project => $projid, repository => $repoid};
  while (@path) {
    my $t = shift @path;
    my $prp = "$t->{'project'}/$t->{'repository'}";
    push @ret, $prp unless $done{$prp};
    $done{$prp} = 1;
    if (!@path) {
      ($base->{'project'}, $base->{'repository'}) = ($t->{'project'}, $t->{'repository'}) if $base;
      last if $done{"/$prp"};
      my ($pid, $tid) = ($t->{'project'}, $t->{'repository'});
      my $proj = BSRevision::readproj_local($pid, 1);
      if (!$proj || $proj->{'remoteurl'}) {
	undef $proj;
	$proj = $remotemap->{$pid} if $remotemap && $remotemap->{$pid};
	if (!$proj || $proj->{'proto'}) {
          $proj = BSSrcServer::Remote::remoteprojid($pid);
          $proj = BSSrcServer::Remote::fetchremoteproj($proj, $pid, $remotemap);
          die("404 project '$pid' does not exist\n") unless $proj;
        }
      }
      BSSrcServer::Partition::checkpartition($remotemap, $pid, $proj) if $remotemap && $remotemap->{':partitions'} && !$remotemap->{':partitions'}->{$pid};
      $done{"/$prp"} = 1;       # mark expanded
      my @repo = grep {$_->{'name'} eq $tid} @{$proj->{'repository'} || []};
      push @path, @{$repo[0]->{'path'}} if @repo && $repo[0]->{'path'};
    } elsif ($remotemap) {
      my $pid = $t->{'project'};
      my $proj = BSRevision::readproj_local($pid, 1);
      if ((!$proj || $proj->{'remoteurl'}) && !$remotemap->{$pid}) {
        my $r = BSSrcServer::Remote::remoteprojid($pid);
        $remotemap->{$pid} = {%$r, 'proto' => 1} if $r;
      }
      BSSrcServer::Partition::checkpartition($remotemap, $pid, $proj) if $remotemap && $remotemap->{':partitions'} && !$remotemap->{':partitions'}->{$pid};
    }
  }
  return @ret;
}

sub concatconfigs {
  my ($projid, $repoid, $remotemap, @path) = @_;

  my $config = "%define _project $projid\n";
  my $macros = '';

  #$macros .= "%vendor Open Build Service\n";

  # find the sign project, this is what we use as vendor
  my $vprojid = $projid;
  while ($vprojid ne '') {
    last if -s "$projectsdir/$vprojid.pkg/_signkey";
    $vprojid =~ s/[^:]*$//;
    $vprojid =~ s/:$//;
  }
  $vprojid = $projid if $vprojid eq '';
  my $obsname = $BSConfig::obsname || 'build.opensuse.org';
  $macros .= "%vendor obs://$obsname/$vprojid\n";
  $macros .= "%_download_url $BSConfig::repodownload\n" if $BSConfig::repodownload;

  $macros .= "%_project $projid\n";
  my $lastr = '';

  my $distinfo = "$projid / $repoid";
  if ($repoid eq 'standard') {
    $distinfo = $projid;
  } 

  for my $prp (reverse @path) {
    if ($prp eq "$projid/$repoid") {
      $macros .= "\n%distribution $distinfo\n";
      $macros .= "%_project $projid\n";
    }
    my ($p, $r) = split('/', $prp, 2);
    my $c;
    if (-s "$projectsdir/$p.conf") {
      $c = readstr("$projectsdir/$p.conf");
    } elsif (!-e "$projectsdir/$p.xml") {
      my $proj = BSSrcServer::Remote::remoteprojid($p);
      $c = BSSrcServer::Remote::fetchremoteconfig($proj, $p, $remotemap);
    }
    if ($remotemap && $remotemap->{':partitions'}) {
      BSSrcServer::Partition::checkpartition($remotemap, $p) if !$remotemap->{':partitions'}->{$p};
      $remotemap->{$p}->{'config'} = defined($c) ? $c : '' if ($remotemap->{$p} || {})->{'partition'};
    }
    next unless defined $c;
    $config .= "\n### from $p\n";
    $config .= "%define _repository $r\n";

    if ($c =~ /^\s*:macros\s*$/im) {
      # probably some multiple macro sections with %if statements
      # flush out macros
      $macros .= "\n### from $p\n";
      $macros .= "\n%_repository $r\n";
      $config .= "\nMacros:\n$macros:Macros\n\n";
      $macros = '';
      $lastr = $r;
      my $s1 = '\A(.*^\s*:macros\s*$)(.*?)\Z';	# should always match
      if ($c =~ /$s1/msi) {
        $config .= $1;
	$c = $2;
      } else {
        $config .= $c;
	$c = '';
      }
    }
    if ($c =~ /^(.*\n)?\s*macros:[^\n]*\n(.*)/si) {
      # has single macro section at end. cumulate
      $c = defined($1) ? $1 : '';
      $macros .= "\n### from $p\n";
      $macros .= "%_repository $r\n";
      $macros .= $2;
      $lastr = $r;
    }
    $config .= $c;
  }
  if ($lastr ne $repoid) {
    $macros .= "\n### from $projid\n";
    $macros .= "%_repository $repoid\n";
  }
  if (!@path || $path[0] ne "$projid/$repoid") {
    $macros .= "\n%distribution $distinfo\n";
    $macros .= "%_project $projid\n";
  }
  if ($BSConfig::extramacros) {
    for (sort keys %{$BSConfig::extramacros}) {
      $macros .= $BSConfig::extramacros->{$_} if $projid =~ /$_/;
    }
  }
  if ($BSConfig::extraconfig) {
    my $extraconfig = '';
    for (sort keys %{$BSConfig::extraconfig}) {
      $extraconfig .= $BSConfig::extraconfig->{$_} if $projid =~ /$_/;
    }
    $config .= "\n$extraconfig" if $extraconfig;
  }
  $config .= "\nMacros:\n$macros" if $macros ne '';
  return $config;
}

sub getbuildconfig {
  my ($cgi, $projid, $repoid) = @_;
  my @path;
  if ($cgi->{'path'}) {
    @path = @{$cgi->{'path'}};
  } else {
    @path = expandsearchpath($projid, $repoid);
  }
  my $config = concatconfigs($projid, $repoid, undef, @path);
  return ($config, 'Content-Type: text/plain');
}

sub getprojectconfig {
  my ($cgi, $projid) = @_;
  my $proj = checkprojrepoarch($projid, undef, undef, 1);
  if ($proj->{'remoteurl'}) {
    my $config = BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/source/$proj->{'remoteproject'}/_config", 'proxy' => $proxy}, undef);
    return ($config, 'Content-Type: text/plain');
  }
  my $config;
  if ($cgi->{'rev'}) {
    my $rev = getrev($projid, '_project', $cgi->{'rev'});
    my $files = $rev ? BSRevision::lsrev($rev) : {};
    $config = BSRevision::revreadstr($rev, '_config', $files->{'_config'}, 1) if $files->{'_config'};
  } else {
    $config = readstr("$projectsdir/$projid.conf", 1);
  }
  $config = '' unless defined $config;
  return ($config, 'Content-Type: text/plain');
}

sub putprojectconfig {
  my ($cgi, $projid) = @_;
  my $proj = BSRevision::readproj_local($projid);
  mkdir_p($uploaddir);
  my $uploadfile = "$uploaddir/$$";
  die("upload failed\n") unless BSServer::read_file($uploadfile);
  if (! -s $uploadfile) {
    unlink($uploadfile);
    $uploadfile = undef;
  }
  BSRevision::addrev_local_replace($cgi, $projid, undef, [ $uploadfile, "$projectsdir/$projid.conf", '_config' ]);
  notify_repservers('project', $projid);
  notify("SRCSRV_UPDATE_PROJECT_CONFIG", { "project" => $projid, "sender" => ($cgi->{'user'} || "unknown") });
  return $BSStdServer::return_ok;
}

sub delprojectconfig {
  my ($cgi, $projid) = @_;
  BSRevision::addrev_local_replace($cgi, $projid, undef, [ undef, "$projectsdir/$projid.conf", '_config' ]);
  notify_repservers('project', $projid);
  notify("SRCSRV_UPDATE_PROJECT_CONFIG", { "project" => $projid, "sender" => ($cgi->{'user'} || "unknown") });
  return $BSStdServer::return_ok;
}

##########################################################################

sub getsources {
  my ($cgi, $projid, $packid, $srcmd5) = @_;
  $packid =~ s/(?<!^_product)(?<!^_patchinfo):.+//;
  my $rev = {'project' => $projid, 'package' => $packid, 'srcmd5' => $srcmd5};
  my $files = BSRevision::lsrev($rev);
  my @files = map {BSRevision::revcpiofile($rev, $_, $files->{$_})} sort keys %$files;
  BSServer::reply_cpio(\@files);
  return undef;
}

my %getfilelist_ajax_inprogress;

sub getfilelist_ajax {
  my ($cgi, $projid, $packid) = @_;

  my $srcmd5 = $cgi->{'rev'};
  if ($cgi->{'view'} eq 'cpio') {
    my $rev = {'project' => $projid, 'package' => $packid, 'srcmd5' => $srcmd5};
    my $files = BSRevision::lsrev($rev);
    my @files = map {BSRevision::revcpiofile($rev, $_, $files->{$_})} sort keys %$files;
    BSWatcher::reply_cpio(\@files);
    return undef;
  }
  if ($cgi->{'view'} eq 'notify') {
    my $jev = $BSServerEvents::gev;
    my $filelist = $jev->{'filelist'};
    my $idstring = "$projid/$packid/$srcmd5";
    if (!$filelist) {
      return $BSStdServer::return_ok if $getfilelist_ajax_inprogress{$idstring};
      $filelist = BSSrcServer::Remote::remote_getrev_getfilelist($projid, $packid, $srcmd5);
      return undef unless defined $filelist;
      # ok, got the filelist, we're the master. get the missing files in the background.
      $jev = BSWatcher::background($BSStdServer::return_ok);
      $getfilelist_ajax_inprogress{$idstring} = $jev;
      $jev->{'filelist'} = $filelist;
      $jev->{'idstring'} = $idstring;
      $jev->{'handler'} = sub {delete $getfilelist_ajax_inprogress{$idstring}};
    }

    # here we're the backgrounded master. get missing files and notify
    my $ret;
    eval {
      $ret = BSSrcServer::Remote::remote_getrev_getfiles($projid, $packid, $srcmd5, $filelist);
    };
    if ($@) {
      # hmm, what to do here? for now we just notify. this will lead to a retry loop, though.
      notify_all_repservers('package', $projid, $packid);
      die($@);
    }
    return undef unless defined $ret;
    delete $getfilelist_ajax_inprogress{$idstring};
    notify_all_repservers('package', $projid, $packid);
    return $ret;
  }
  die("unknown view '$cgi->{'view'}'\n");
}

sub getproductrepositories {
  my ($xml) = @_;

  my @res;
  for my $product (@{$xml->{'products'}->{'product'}}) {
    my @pr;
    for my $repo (@{$product->{'register'}->{'updates'}->{'repository'}}) {
      my ($path) = published_path({}, $repo->{'project'}, $repo->{'name'});
      $path = { 'path' => $path->{'path'}, 'update' => undef };
      $path->{'arch'} = $repo->{'arch'} if $repo->{'arch'};
      $path->{'zypp'} = $repo->{'zypp'} if $repo->{'zypp'};
      $path->{'debug'} = undef if $repo->{'name'} =~ m/_debug$/;
      push @pr, $path;
    }
    for my $repo (@{$product->{'register'}->{'pool'}->{'repository'}}) {
      die("getproductrepositories: path AND url is set!\n") if defined ($repo->{'project'}) && defined($repo->{'url'});
      my $path;
      if (defined($repo->{'url'})) {
        $path = { 'url' => $repo->{'url'} };
      } else {
        ($path) = published_path({ 'medium' => $repo->{'medium'} }, $repo->{'project'}, $repo->{'name'});
        $path = { 'path' => $path->{'path'} };
      }
      $path->{'arch'} = $repo->{'arch'} if $repo->{'arch'};
      $path->{'zypp'} = $repo->{'zypp'} if $repo->{'zypp'};
      $path->{'debug'} = undef if $repo->{'medium'} =~ m/_debug$/;
      push @pr, $path;
    }
    my $prod = { 'name' => $product->{'name'}, 'repository' => \@pr };
    $prod->{'distrotarget'} = $product->{'register'}->{'updates'}->{'distrotarget'} if $product->{'register'}->{'updates'}->{'distrotarget'};
    push @res, $prod;
  }
  return @res;
}

sub getfilelist {
  my ($cgi, $projid, $packid) = @_;

  my $view = $cgi->{'view'};
  my $rev;
  my $linked;
  $linked = [] if $cgi->{'withlinked'};
  my $islatestrev;
  if ($cgi->{'meta'}) {
    $rev = BSRevision::getrev_meta($projid, $packid, $cgi->{'rev'}, $cgi->{'deleted'});
  } elsif ($cgi->{'deleted'}) {
    $rev = BSRevision::getrev_local($projid, $packid, $cgi->{'rev'}, $cgi->{'deleted'});
  } else {
    $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload', $linked);
    $islatestrev = 1 if !defined($cgi->{'rev'}) && $rev->{'srcmd5'} && $rev->{'srcmd5'} ne 'pattern' && $rev->{'srcmd5'} ne 'upload';
  }
  my $li = {};
  my $files = BSRevision::lsrev($rev, $li);

  # show state of current source service run, if defined
  my $serviceinfo;
  if ($li->{'xservicemd5'} || $li->{'lservicemd5'}) {
    # new style
    $serviceinfo = {};
    $serviceinfo->{'lsrcmd5'} = $li->{'lservicemd5'} if $li->{'lservicemd5'};
    if ($li->{'xservicemd5'}) {
      if ($cgi->{'expand'}) {
	$serviceinfo->{'lsrcmd5'} = $rev->{'srcmd5'};
	$files = BSSrcServer::Service::handleservice($rev, $files, $li->{'xservicemd5'});
	$serviceinfo->{'code'} = 'succeeded';	# otherwise it already died...
      } else {
        eval { BSSrcServer::Service::handleservice({ %$rev }, $files, $li->{'xservicemd5'}) };
	my $error = $@;
	chomp $error if $error;
	if (!$error) {
	  $serviceinfo->{'code'} = 'succeeded';
	  $serviceinfo->{'xsrcmd5'} = $li->{'xservicemd5'};
	} elsif ($error eq 'service in progress') {
	  $serviceinfo->{'code'} = 'running';
	} else {
	  $serviceinfo->{'code'} = 'failed';
	  $serviceinfo->{'xsrcmd5'} = $li->{'xservicemd5'};
	  $serviceinfo->{'error'} = $error;
	}
      }
    }
    delete $li->{'xservicemd5'};
    delete $li->{'lservicemd5'};
  } elsif ($files->{'_service'} && $packid ne '_project' && !$cgi->{'meta'} && !defined($cgi->{'rev'})) {
    # check error/in progress
    $serviceinfo = {};
    my $lockfile = "$eventdir/service/${projid}::$packid";
    if (-e $lockfile) {
      $serviceinfo->{'code'} = 'running';
    } elsif ($files->{'_service_error'}) {
      $serviceinfo->{'code'} = 'failed';
      $serviceinfo->{'error'} = BSRevision::revreadstr($rev, '_service_error', $files->{'_service_error'});
    } else {
      $serviceinfo->{'code'} = 'succeeded';
    }
  } elsif ($files->{'_service_error'}) {
    $serviceinfo = {'code' => 'failed'};
    $serviceinfo->{'error'} = BSRevision::revreadstr($rev, '_service_error', $files->{'_service_error'});
  }
  undef $islatestrev if $serviceinfo && !$cgi->{'expand'};

  if ($files->{'_link'}) {
    if ($cgi->{'emptylink'}) {
      my $l = BSRevision::revreadxml($rev, '_link', $files->{'_link'}, $BSXML::link);
      delete $l->{'patches'};
      mkdir_p($uploaddir);
      writexml("$uploaddir/$$", undef, $l, $BSXML::link);
      $files = {};
      $files->{'_link'} = BSSrcrep::addfile($projid, $packid, "$uploaddir/$$", '_link');
      $rev = addrev({}, $projid, $packid, $files, '');
      undef $islatestrev;
    }
    my %lrev = %$rev;
    $lrev{'linkrev'} = $cgi->{'linkrev'} if $cgi->{'linkrev'};
    $li->{'linked'} = $linked if $linked;
    my $lfiles = BSSrcServer::Link::handlelinks(\%lrev, $files, $li);
    BSSrcServer::Multibuild::updatemultibuild($projid, $packid, $lfiles) if $islatestrev && !$cgi->{'linkrev'} && ref($lfiles);
    if ($cgi->{'expand'}) {
      if (!ref($lfiles)) {
	if ($cgi->{'withlinked'} && !$view) {
	  my $ret = {};
	  $ret->{'name'} = $packid;
	  $ret->{'error'} = $lfiles || 'internal error';
	  $ret->{'linkinfo'} = $li;
	  return ($ret, $BSXML::dir);
	}
	die("$lfiles\n");
      }
      $files = $lfiles;
      %$rev = %lrev;
      $rev->{'rev'} = $rev->{'srcmd5'};
    } else {
      if (ref $lfiles) {
        $li->{'xsrcmd5'} = $lrev{'srcmd5'};
      } else {
	# link is broken
	$li->{'error'} = $lfiles;
	# set xsrcmd5 if we have a link error file
	$li->{'xsrcmd5'} = $lrev{'srcmd5'} if $lrev{'srcmd5'} && BSSrcrep::havelinkerror($lrev{'project'}, $lrev{'package'}, $lrev{'srcmd5'});
	if ($cgi->{'lastworking'}) {
	  my $lastworking = BSSrcServer::Link::findlastworkinglink($rev);
	  $li->{'lastworking'} = $lastworking if $lastworking;
	}
      }
    }
  } else {
    BSSrcServer::Multibuild::updatemultibuild($projid, $packid, $files) if $islatestrev;
  }
  if ($islatestrev && $rev->{'originpackage'}) {
    my $mb = BSSrcServer::Multibuild::getmultibuild($projid, $rev->{'package'}) || {};
    die("404 package '$packid' does not exist\n") unless grep {"$rev->{'package'}:$_" eq $packid} @{$mb->{'flavor'} || $mb->{'package'} || []};
  }

  if ($cgi->{'extension'}) {
    for (keys %$files) {
      delete $files->{$_} unless /\.\Q$cgi->{'extension'}\E$/;
    }
  }

  if ($view && $view eq 'cpio') {
    if (!$cgi->{'extension'} && $rev->{'srcmd5'} && $rev->{'srcmd5'} ne 'upload' && $rev->{'srcmd5'} ne 'pattern' && $rev->{'srcmd5'} ne 'empty' && $rev->{'srcmd5'} ne $BSSrcrep::emptysrcmd5) {
      # hack: we identify remote source downloads by looking at the user agent
      my $useragent = $BSServer::request->{'headers'}->{'user-agent'} || '';
      if ($useragent =~ /BSRPC/) {
	BSHandoff::handoff("/source/$projid/$rev->{'package'}", undef, "rev=$rev->{'srcmd5'}", 'view=cpio');
      }
    }
    my @files = map {BSRevision::revcpiofile($rev, $_, $files->{$_})} sort keys %$files;
    BSServer::reply_cpio(\@files);
    return undef;
  }

  if ($view && ($view eq 'products' || $view eq 'productrepositories') ) {
    my @res;
    my $reader = sub { return BSRevision::revreadstr($rev, $_[0], $files->{$_[0]}) };
    for my $filename (sort keys %$files) {
      next unless $filename =~ /\.product$/s;
      next if $cgi->{'product'} && $filename ne "$cgi->{'product'}.product";
      my $xml = BSProductXML::readproductxml([$reader, $filename], 1);
      die("400 Unable to parse $filename\n") unless $xml;
      push @res, $xml;
    }
    if ($view eq 'productrepositories') {
      @res = map {getproductrepositories($_)} @res;
      return ({"product" => \@res}, $BSProductXML::productlistrepositories);
    }
    return ({'productdefinition' => \@res}, $BSProductXML::products);
  }

  my $ret = {};
  $ret->{'name'} = $packid;
  $ret->{'srcmd5'} = $rev->{'srcmd5'} if $rev->{'srcmd5'} ne 'empty';
  $ret->{'rev'} = $rev->{'rev'} if exists $rev->{'rev'};
  $ret->{'vrev'} = $rev->{'vrev'} if exists $rev->{'vrev'};
  $ret->{'serviceinfo'} = $serviceinfo if $serviceinfo;
  my @res;
  for my $filename (sort keys %$files) {
    my @s = BSRevision::revstat($rev, $filename, $files->{$filename});
    if (@s) {
      push @res, {'name' => $filename, 'md5' => $files->{$filename}, 'size' => $s[7], 'mtime' => $s[9]};
    } else {
      push @res, {'name' => $filename, 'md5' => $files->{$filename}, 'error' => "$!"};
    }
  }
  if (%$li) {
    BSSrcServer::Link::linkinfo_addtarget($rev, $li);
    $ret->{'linkinfo'} = $li;
  }

  # fake linkinfo element for project links. see comment in remote_getrev
  if ($linked && @$linked && !$ret->{'linkinfo'}) {
    $li->{'linked'} = $linked;
    $ret->{'linkinfo'} = $li;
  }

  $ret->{'entry'} = \@res;
  return ($ret, $BSXML::dir);
}

sub getfile {
  my ($cgi, $projid, $packid, $filename) = @_;
  die("no filename\n") unless defined($filename) && $filename ne '';
  die("bad filename\n") if $filename =~ /\// || $filename =~ /^\./;
  my $rev;
  if ($cgi->{'meta'}) {
    $rev = BSRevision::getrev_meta($projid, $packid, $cgi->{'rev'}, $cgi->{'deleted'});
  } elsif ($cgi->{'deleted'}) {
    $rev = BSRevision::getrev_local($projid, $packid, $cgi->{'rev'}, $cgi->{'deleted'});
  } else {
    $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  }
  my $view = $cgi->{'view'} || '';
  if ($view eq 'blame') {
    die("cannot blame deleted packages\n") if $cgi->{'deleted'};
    my $b = BSSrcServer::Blame::blame($rev, $filename, $cgi->{'expand'}, $cgi->{'meta'});
    my $ret = '';
    my %links;
    my $linkno = 1;
    $links{"$projid/$packid"} = 0;
    for my $rev (@$b) {
      next unless ref($rev);
      next if exists $links{"$rev->{'project'}/$rev->{'package'}"};
      $ret .= sprintf("%7s ", "$linkno:")."$rev->{'project'}/$rev->{'package'}\n";
      $links{"$rev->{'project'}/$rev->{'package'}"} = $linkno++;
    }
    $ret .= "\n" if $linkno > 1;
    my $li = 1;
    while (@$b) {
      my ($rev, $line) = splice(@$b, 0, 2);
      if ($linkno > 1) {
        my $lno = $links{"$rev->{'project'}/$rev->{'package'}"};
        $lno = $lno ? "$lno:" : '';
        $ret .= sprintf("%7s ", "$lno$rev->{'rev'}");
      } else {
        $ret .= sprintf("%4d ", $rev->{'rev'});
      }
      my $user = sprintf("%-12s", $rev->{'user'});
      my $date = BSUtil::isotime($rev->{'time'});
      $ret .= "($user $date ".sprintf("%5d", $li++).") $line\n";
    }
    return $ret;
  } elsif ($view) {
    die("unknown view '$view'\n");
  }

  my $files;
  if ($cgi->{'expand'}) {
    $files = lsrev_expanded($rev);
  } else {
    $files = BSRevision::lsrev($rev);
  }
  die("404 $filename: no such file\n") unless $files->{$filename};
  my @s = BSRevision::revstat($rev, $filename, $files->{$filename});
  die("$projid/$packid/$files->{$filename}-$filename: $!\n") unless @s;
  if (!$BSStdServer::isajax && $rev->{'srcmd5'} && $rev->{'srcmd5'} ne 'upload' && $rev->{'srcmd5'} ne 'pattern' && $rev->{'srcmd5'} ne 'empty' && $rev->{'srcmd5'} ne $BSSrcrep::emptysrcmd5) {
    # hack: we identify remote source downloads by looking at the user agent
    my $useragent = $BSServer::request->{'headers'}->{'user-agent'} || '';
    if ($useragent =~ /BSRPC/) {
      BSHandoff::handoff("/source/$projid/$packid/$filename", undef, "rev=$rev->{'srcmd5'}");
    }
  }
  my $fd = gensym;
  BSRevision::revopen($rev, $filename, $files->{$filename}, $fd) || die("$projid/$packid/$files->{$filename}-$filename: $!\n");
  BSWatcher::reply_file($fd);
  return undef;
}

sub putfile {
  my ($cgi, $projid, $packid, $filename) = @_;
  die("no filename\n") unless defined($filename) && $filename ne '';
  die("bad filename\n") if $filename =~ /\// || $filename =~ /^\./;
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$", 'withmd5' => 1);
  die("upload failed\n") unless $uploaded;
  if ($cgi->{'meta'}) {
    if ($filename eq '_attribute') {
      my $attribs = readxml("$uploaddir/$$", $BSXML::attributes);
      BSVerify::verify_attributes($attribs);
      writexml("$uploaddir/$$", undef, $attribs, $BSXML::attributes);
    } elsif ($filename eq '_frozenlinks') {
      my $frozenx = readxml("$uploaddir/$$", $BSXML::frozenlinks);
      BSVerify::verify_frozenlinks($frozenx);
      writexml("$uploaddir/$$", undef, $frozenx, $BSXML::frozenlinks);
    } else {
      die("unsupported meta operation\n");
    }
    my $rev = BSRevision::addrev_meta_replace($cgi, $projid, $packid, [ "$uploaddir/$$", undef, $filename ]);
    notify_repservers('package', $projid) if $cgi->{'meta'} && $filename eq '_frozenlinks';
    delete $rev->{'project'};
    delete $rev->{'package'};
    return ($rev, $BSXML::revision);
  }

  my $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  die("file '$filename' is read-only\n") if ($filename =~ /^_service:/) && !$cgi->{'force'};
  BSSrcrep::addfile($projid, $packid, "$uploaddir/$$", $filename, $uploaded->{'md5'});
  # create new meta file
  my $files;
  if ($cgi->{'keeplink'}) {
    $files = lsrev_expanded($rev);
  } else {
    $files = BSRevision::lsrev($rev);
  }
  $files->{$filename} = $uploaded->{'md5'};
  $files = BSSrcServer::Link::keeplink($cgi, $projid, $packid, $files) if $cgi->{'keeplink'};
  $rev = addrev($cgi, $projid, $packid, $files, $cgi->{'rev'});
  BSSrcServer::Service::runservice($cgi, $rev, $files);
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

sub getsourcediffcache {
  my ($cgi, $cacheid) = @_;

  my $view = $cgi->{'view'} || '';
  my $cn = "$diffcache/".substr($cacheid, 0, 2)."/$cacheid";
  BSWatcher::addfilewatcher($cn) if $BSStdServer::isajax;
  my $lockc = BSUtil::lockcheck('>>', "$cn.run");
  my $fd = gensym;
  if (open($fd, '<', $cn)) {
    unlink("$cn.run");
    utime(time, time, $cn);
    BSWatcher::reply_file($fd, $view eq 'xml' ? 'Content-Type: text/xml' : 'Content-Type: text/plain');
    return undef;
  }
  return undef if $BSStdServer::isajax && !$lockc;
  die("cache entry '$cacheid' does not exist\n");
}

sub sourcediff {
  my ($cgi, $projid, $packid) = @_;

  BSVerify::verify_linkrev($cgi->{'olinkrev'}) if defined($cgi->{'olinkrev'}) && $cgi->{'olinkrev'} ne 'linkrev';
  my $oprojid = exists($cgi->{'oproject'}) ? $cgi->{'oproject'} : $projid;
  my $opackid = exists($cgi->{'opackage'}) ? $cgi->{'opackage'} : $packid;

  my $fmax = 200;
  my $tmax = 16000;
  $fmax = $cgi->{'filelimit'} if defined $cgi->{'filelimit'};
  $tmax = $cgi->{'tarlimit'} if defined $cgi->{'tarlimit'};
  undef $fmax unless $fmax;
  undef $tmax unless $tmax;

  my $have0rev = (defined($cgi->{'rev'}) && $cgi->{'rev'} eq '0') || (defined($cgi->{'orev'}) && $cgi->{'orev'} eq '0');
  my $rev;
  if ($cgi->{'meta'}) {
    $rev = BSRevision::getrev_meta($projid, $packid, $cgi->{'rev'});
  } else {
    $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload', undef, $cgi->{'missingok'});
  }
  my $linkinfo = {};
  my $files = BSRevision::lsrev($rev, $linkinfo);
  $files = BSSrcServer::Service::handleservice($rev, $files, $linkinfo->{'xservicemd5'}) if $cgi->{'expand'} && $linkinfo->{'xservicemd5'};
  my $orev = $cgi->{'orev'};
  if (!defined($cgi->{'oproject'}) && !defined($cgi->{'opackage'}) && !defined($cgi->{'orev'}) && $rev->{'rev'}) {
    die("revision is not a simple commit\n") unless $rev->{'rev'} =~ /^\d+$/s;
    $orev = $rev->{'rev'} - 1;
    $have0rev = 1 if $orev == 0;
    $cgi->{'olinkrev'} = 'linkrev' if !defined($cgi->{'olinkrev'});
  }
  if ($cgi->{'meta'}) {
    $orev = BSRevision::getrev_meta($oprojid, $opackid, $orev);
  } else {
    $orev = getrev($oprojid, $opackid, defined($orev) ? $orev : 'latest', undef, $cgi->{'missingok'});
  }
  my $olinkinfo = {};
  my $ofiles = BSRevision::lsrev($orev, $olinkinfo);
  $ofiles = BSSrcServer::Service::handleservice($orev, $ofiles, $olinkinfo->{'xservicemd5'}) if $cgi->{'expand'} && $olinkinfo->{'xservicemd5'};
  if ($cgi->{'expand'} || (!$have0rev && $files->{'_link'} && !$ofiles->{'_link'}) || (!$have0rev && $ofiles->{'_link'} && !$files->{'_link'})) {
    # expand links
    if ($files->{'_link'}) {
      $rev->{'linkrev'} = $cgi->{'linkrev'} if $cgi->{'linkrev'};
      my %li;
      my $l = BSRevision::revreadxml($rev, '_link', $files->{'_link'}, $BSXML::link, 1);
      if ($l) {
        $l->{'project'} = $rev->{'project'} unless defined $l->{'project'};
        $l->{'package'} = $rev->{'package'} unless defined $l->{'package'};
      }
      $files = BSSrcServer::Link::handlelinks($rev, $files, \%li);
      die("bad link: $files\n") unless ref $files;

      # some nasty magic to improve diff usability
      if ($l && $cgi->{'linkrev'} && $l->{'project'} eq $oprojid && $l->{'package'} eq $opackid && !$l->{'rev'} && !$cgi->{'orev'}) {
        # we're diffing against the link target. As the user specified a baserev, we should use it
        # instead of the latest source
        $orev = getrev($oprojid, $opackid, $li{'srcmd5'});
        $ofiles = BSRevision::lsrev($orev);
      }
      # olinkrev=linkrev: reuse same linkrev if the link target matches
      if ($cgi->{'olinkrev'} && $cgi->{'olinkrev'} eq 'linkrev' && $ofiles->{'_link'}) {
	my $ol = BSRevision::revreadxml($orev, '_link', $ofiles->{'_link'}, $BSXML::link, 1);
	if ($ol) {
	  $ol->{'project'} = $orev->{'project'} unless defined $ol->{'project'};
	  $ol->{'package'} = $orev->{'package'} unless defined $ol->{'package'};
	}
	$cgi->{'olinkrev'} = $li{'srcmd5'} if $l && $ol && $l->{'project'} eq $ol->{'project'} && $l->{'package'} eq $ol->{'package'};
      }
    }
    if ($ofiles->{'_link'}) {
      $orev->{'linkrev'} = $cgi->{'olinkrev'} if $cgi->{'olinkrev'} && $cgi->{'olinkrev'} ne 'linkrev';
      $ofiles = BSSrcServer::Link::handlelinks($orev, $ofiles);
      die("bad link: $ofiles\n") unless ref $ofiles;
    }
  }
  my $view = $cgi->{'view'} || '';
  $view = 'unified' if $cgi->{'unified'};
  die("unsupported view '$view'\n") if $view && ($view ne 'xml' && $view ne 'unified');
  my $cacheid = "//cacheversion:2/";
  $cacheid .= "$orev->{'srcmd5'}/$rev->{'srcmd5'}";
  $cacheid .= "/unified:1" if $view && $view eq 'unified';
  $cacheid .= "/view:$cgi->{'view'}" if $view && $view ne 'unified';
  $cacheid .= "/fmax:$fmax" if defined $fmax;
  $cacheid .= "/tmax:$tmax" if defined $tmax;
  $cgi->{'withissues'} = 1 if $cgi->{'onlyissues'};
  if ($cgi->{'withissues'}) {
    my @s = stat("$BSConfig::bsdir/issuetrackers.xml");
    $cacheid .= "/withissues:$s[9]/$s[7]/$s[1]" if @s;
    $cacheid .= "/onlyissues" if $cgi->{'onlyissues'};
  }
  if ($cgi->{'file'}) {
    my %file = map {$_ => 1} @{$cgi->{'file'}};
    $cacheid .= "/file:$_" for sort keys %file;
    for (keys %$ofiles) {
      delete $ofiles->{$_} unless $file{$_};
    }
    for (keys %$files) {
      delete $files->{$_} unless $file{$_};
    }
  }
  $cacheid = Digest::MD5::md5_hex($cacheid);
  my $xmlret;
  if ($view eq 'xml') {
    $xmlret = {};
    $xmlret->{'key'} = $cacheid;
    $rev->{'rev'} ||= 0;
    $rev->{'srcmd5'} = $BSSrcrep::emptysrcmd5 if $rev->{'srcmd5'} eq 'empty';
    $orev->{'rev'} ||= 0;
    $orev->{'srcmd5'} = $BSSrcrep::emptysrcmd5 if $rev->{'srcmd5'} eq 'empty';
    $xmlret->{'old'} = { 'project' => $orev->{'project'}, 'package' => $orev->{'package'}, 'rev' => $orev->{'rev'}, 'srcmd5' => $orev->{'srcmd5'} };
    $xmlret->{'new'} = { 'project' => $rev->{'project'}, 'package' => $rev->{'package'}, 'rev' => $rev->{'rev'}, 'srcmd5' => $rev->{'srcmd5'} };
    $xmlret->{'files'} = {};
  }
  if (!grep {($ofiles->{$_} || '') ne ($files->{$_} || '')} (keys %$ofiles, keys %$files)) {
    # all files identical, don't bother
    return ($xmlret, $BSXML::sourcediff) if $view eq 'xml';
    return ('', 'Content-Type: text/plain');
  }
  local *F;
  my $cn = "$diffcache/".substr($cacheid, 0, 2)."/$cacheid";
  if (open(F, '<', $cn)) {
    utime(time, time, $cn);
    BSServer::reply_file(\*F, $view eq 'xml' ? 'Content-Type: text/xml' : 'Content-Type: text/plain');
    return undef;
  }
  local *LF;
  mkdir_p("$diffcache/".substr($cacheid, 0, 2));
  if (!BSUtil::lockcheck('>>', "$cn.run")) {
    my @args;
    push @args, "view=$view" if $view;
    BSHandoff::handoff("/sourcediffcache/$cacheid", undef, @args);
  }
  BSUtil::lockopen(\*LF, '>>', "$cn.run");
  # retry open, maybe somebody else has created the diff meanwhile
  if (open(F, '<', $cn)) {
    unlink("$cn.run");
    close LF;
    utime(time, time, $cn);
    BSServer::reply_file(\*F, $view eq 'xml' ? 'Content-Type: text/xml' : 'Content-Type: text/plain');
    return undef;
  }
  my $tmpdir = "$uploaddir/srcdiff$$";
  my $d;
  my %xobscpio;
  mkdir_p($uploaddir);
  my $xobscpio = sub {
    return BSRevision::revfilename($_[0], $_[1], $_[2]) if $_[1] !~ /\.obscpio$/;
    my $tmp = "$uploaddir/sourcediff.obscpio.$$.$_[2]-$_[1]";
    BSSrcrep::copyonefile_tmp($_[0]->{'project'}, $_[0]->{'package'}, $_[1], $_[2], $tmp) unless $xobscpio{$tmp};
    $xobscpio{$tmp} = 1;
    return $tmp;
  };
  my $ofn = sub { $xobscpio->($orev, $_[0], $ofiles->{$_[0]}) };
  my $fn  = sub { $xobscpio->($rev, $_[0], $files->{$_[0]}) };
  my %opts = ('edir' => $tmpdir, 'fmax' => $fmax, 'tmax' => $tmax, 'tfmax' => $fmax, 'doarchive' => 1, 'similar' => 1);
  if ($view eq 'xml') {
    if (!$cgi->{'onlyissues'}) {
      $xmlret->{'files'} = { 'file' => BSSrcdiff::datadiff($ofn, $ofiles, $fn, $files, %opts) };
    }
    if ($cgi->{'withissues'}) {
      my $trackers = readxml("$BSConfig::bsdir/issuetrackers.xml", $BSXML::issue_trackers, 1) || {};
      $opts{'trackers'} = $trackers->{'issue-tracker'} || [];
      $xmlret->{'issues'} = { 'issue' => BSSrcdiff::issuediff($ofn, $ofiles, $fn, $files, %opts) };
    }
    BSUtil::data2utf8xml($xmlret);
    $d = XMLout($BSXML::sourcediff, $xmlret);
  } elsif ($view eq 'unified') {
    delete $opts{'doarchive'};
    delete $opts{'similar'};
    $opts{'nodecomp'} = 1;
    $opts{'oldrevision'} = $orev->{'rev'} if defined $orev->{'rev'};
    $opts{'newrevision'} = $rev->{'rev'} if defined $rev->{'rev'};
    $d = BSSrcdiff::unifieddiff($ofn, $ofiles, $fn, $files, %opts);
  } else {
    $d = BSSrcdiff::srcdiff($ofn, $ofiles, $fn, $files, %opts);
  }
  unlink($_) for keys %xobscpio;
  mkdir_p("$diffcache/".substr($cacheid, 0, 2));
  writestr("$diffcache/.new$$", $cn, $d);
  unlink("$cn.run");
  close LF;
  return ($d, $view eq 'xml' ? 'Content-Type: text/xml' : 'Content-Type: text/plain');
}

sub linkdiff {
  my ($cgi, $projid, $packid) = @_;
  my $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  $rev->{'linkrev'} = $cgi->{'linkrev'} if $cgi->{'linkrev'};
  my $linkinfo = {};
  my $files = lsrev_expanded($rev, $linkinfo);
  die("not a link\n") unless $linkinfo->{'srcmd5'};
  BSSrcServer::Link::linkinfo_addtarget($rev, $linkinfo);
  return sourcediff({
    %$cgi, 'expand' => 0,
    'oproject' => $linkinfo->{'project'},
    'opackage' => $linkinfo->{'package'},
    'orev' => $linkinfo->{'srcmd5'},
    'missingok' => $linkinfo->{'missingok'},
    'rev' => $rev->{'srcmd5'},
  }, $projid, $packid);
}

sub servicediff {
  my ($cgi, $projid, $packid) = @_;
  die("servicediff only works for new style services\n") if $BSConfig::old_style_services;
  my $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  my $linkinfo = {};
  my $files = BSRevision::lsrev($rev, $linkinfo);
  if ($linkinfo->{'xservicemd5'}) {
    return sourcediff({%$cgi, 'expand' => 0, 'orev' => $rev->{'srcmd5'}, 'rev' => $linkinfo->{'xservicemd5'}}, $projid, $packid);
  } elsif ($linkinfo->{'lservicemd5'}) {
    return sourcediff({%$cgi, 'expand' => 0, 'orev' => $linkinfo->{'lservicemd5'}, 'rev' => $rev->{'srcmd5'}}, $projid, $packid);
  } else {
    die("no service was run for this revision\n");
  }
}

sub sourcecommit {
  my ($cgi, $projid, $packid) = @_;
  my $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  my $files = BSRevision::lsrev($rev);
  $files = BSSrcServer::Link::keeplink($cgi, $projid, $packid, $files) if $cgi->{'keeplink'};
  $rev = addrev($cgi, $projid, $packid, $files);
  BSSrcServer::Service::runservice($cgi, $rev, $files) unless $cgi->{'noservice'};
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

sub sourcecommitfilelist {
  my ($cgi, $projid, $packid) = @_;
  BSVerify::verify_md5($cgi->{'servicemark'}) if $cgi->{'servicemark'};
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$");
  die("upload failed\n") unless $uploaded;
  my $fl = readxml("$uploaddir/$$", $BSXML::dir);
  unlink("$uploaddir/$$");
  # make sure we know every file
  my @missing;
  my $files = {};
  my $ofiles = {};
  my $ofiles_expanded = {};
  my $orev = {'project' => $projid, 'package' => $packid};
  if ($cgi->{'withvalidate'}) {
    eval {
      my $rev_old = getrev($projid, $packid);
      $ofiles = BSRevision::lsrev($rev_old);
      $ofiles_expanded = lsrev_expanded($rev_old);
    };
  }
  for my $entry (@{$fl->{'entry'} || []}) {
    BSVerify::verify_filename($entry->{'name'});
    BSVerify::verify_md5($entry->{'md5'});
    if (! -e BSRevision::revfilename($orev, $entry->{'name'}, $entry->{'md5'})) {
      $entry->{'hash'} = 'new' if $cgi->{'withvalidate'};
      push @missing, $entry;
    } else {
      die("duplicate file: $entry->{'name'}\n") if exists $files->{$entry->{'name'}};
      if ($entry->{'hash'}) {
        my $fd = gensym;
        BSRevision::revopen($orev, $entry->{'name'}, $entry->{'md5'}, $fd);
        my $sha256 = Digest::SHA->new(256);
        my $hash_to_check = "sha256:" . $sha256->addfile($fd)->hexdigest;
        if ($hash_to_check ne $entry->{'hash'}) {
          die("SHA mismatch for same md5sum in $packid for file $entry->{'name'} with sum $entry->{'md5'}\n");
        }
      } elsif ($cgi->{'withvalidate'}) {
        if ((!$ofiles->{$entry->{'name'}} || $ofiles->{$entry->{'name'}} ne $entry->{'md5'}) &&
            (!$ofiles_expanded->{$entry->{'name'}} || $ofiles_expanded->{$entry->{'name'}} ne $entry->{'md5'})) {
          $entry->{'hash'} = 'missing';
          push @missing, $entry;
        }
      }
      $files->{$entry->{'name'}} = $entry->{'md5'};
    }
  }
  if (@missing) {
    my $res = {'name' => $packid, 'error' => 'missing', 'entry' => \@missing};
    return ($res, $BSXML::dir);
  }
  $files = BSSrcServer::Link::keeplink($cgi, $projid, $packid, $files) if $cgi->{'keeplink'};
  if (-e "$projectsdir/$projid.pkg/$packid.upload-MD5SUMS") {
    # autocommit old update revision so that it doesn't get lost
    my $uploadrev = {'project' => $projid, 'package' => $packid, 'srcmd5' => 'upload'};
    my $uploadfiles = BSRevision::lsrev($uploadrev);
    addrev({ %$cgi, 'comment' => 'autocommit update revision'}, $projid, $packid, $uploadfiles);
  }
  my $rev = addrev($cgi, $projid, $packid, $files);
  BSSrcServer::Service::runservice($cgi, $rev, $files) unless $cgi->{'noservice'};
  $cgi->{'rev'} = $rev->{'rev'};
  return getfilelist($cgi, $projid, $packid);
}

# admin only, move entire project
sub moveproject {
  my ($cgi, $projid) = @_;
  my $oprojid = $cgi->{'oproject'};
  return $BSStdServer::return_ok if $oprojid eq $projid;

  my $oproj = BSRevision::readproj_local($oprojid);

  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $oreposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($oprojid) : $BSConfig::reposerver;
  if ($reposerver ne $oreposerver) {
    die("cannot copy binaries between different reposiory servers yet\n");
  }
  if (-e "$projectsdir/$projid.pkg" || -e "$projectsdir/$projid.conf" || -e "$projectsdir/$projid.xml") {
    die("target project already exists\n");
  }

  rename("$projectsdir/$oprojid.xml", "$projectsdir/$projid.xml");
  rename("$projectsdir/$oprojid.pkg", "$projectsdir/$projid.pkg") if -e "$projectsdir/$oprojid.pkg";
  rename("$projectsdir/$oprojid.conf", "$projectsdir/$projid.conf") if -e "$projectsdir/$oprojid.conf";
  rename("$treesdir/$oprojid", "$treesdir/$projid") if $BSConfig::nosharedtrees && -e "$treesdir/$oprojid";

  # move entries in linkinfo database
  BSRevision::movelinkinfos($projid, $oprojid);

  # move in the backend as well
  my @args;
  push @args, "cmd=move";
  push @args, "oproject=$oprojid";
  my $param = {
    'uri' => "$reposerver/build/$projid",
    'request' => 'POST',
  };
  eval {
    # ignore failures for now
    BSWatcher::rpc($param, undef, @args);
  };
  warn($@) if $@;

  # check all packages in project
  notify_repservers('package', $projid);
  notify_repservers('package', $oprojid);
  return $BSStdServer::return_ok;
}

# copy sources of entire project, project exists ensured by api.
sub copyproject {
  my ($cgi, $projid) = @_;
  my $oprojid = $cgi->{'oproject'};
  return $BSStdServer::return_ok if $oprojid eq $projid;

  die("copyproject can not have both makeolder and makeoriginolder\n") if $cgi->{'makeolder'} && $cgi->{'makeoriginolder'};
  my $proj = BSRevision::readproj_local($projid);
  my $oproj = BSRevision::readproj_local($oprojid);

  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $oreposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($oprojid) : $BSConfig::reposerver;
  if ($cgi->{'withbinaries'} && $reposerver ne $oreposerver) {
    die("cannot copy binaries between different repository servers yet\n");
  }

  my $user = defined($cgi->{'user'}) && $cgi->{'user'} ne '' ? $cgi->{'user'} : 'unknown';
  my $comment = defined($cgi->{'comment'}) ? $cgi->{'comment'} : '';
  $user = str2utf8xml($user);
  $comment = str2utf8xml($comment);

  # copy _project data
  if (-e "$projectsdir/$oprojid.pkg/_project.rev" || -e "$projectsdir/$oprojid.conf") {
    my $lastorev = getrev($oprojid, '_project');
    my $files = BSRevision::lsrev($lastorev);
    BSSrcrep::copyfiles($projid, '_project', $oprojid, '_project', $files);
    addrev($cgi, $projid, '_project', $files);
  }

  # signal start of project copy
  notify_repservers('suspendproject', $projid, undef, 'copyproject in progress');

  # local packages only
  my @pkgs = BSRevision::lspackages_local($oprojid);
  delete $cgi->{'servicemark'};		# just in case...
  for my $packid (@pkgs) {
    if (! -e "$projectsdir/$projid.pkg/$packid.xml") {
      # new package, create. hopefully the API can deal with this
      my $opack = BSRevision::readpack_local($oprojid, $packid);
      my $pack = {
	'project' => $projid,
	'name' => $packid,
      };
      # everything except person, group, devel and lock
      for (keys %$opack) {
        next if $_ eq 'project' || $_ eq 'name';
        next if $_ eq 'person' || $_ eq 'group' || $_ eq 'devel' || $_ eq 'lock';
        $pack->{$_} = $opack->{$_} if defined $opack->{$_};
      }
      mkdir_p($uploaddir);
      writexml("$uploaddir/copyproject$$", undef, $pack, $BSXML::pack);
      BSRevision::addrev_meta_replace($cgi, $projid, $packid, [ "$uploaddir/copyproject$$", "$projectsdir/$projid.pkg/$packid.xml", '_meta' ]);
      # need to do this now because the binary copy will fail otherwise
      notify_repservers('package', $projid, $packid) if $cgi->{'withbinaries'};
    }
    if ($cgi->{'makeolder'} || $cgi->{'makeoriginolder'} || -s "$projectsdir/$oprojid.pkg/$packid.rev") {
      my $lastorev;
      if ($cgi->{'withhistory'}) {
	# FIXME: races ahead
	# history copying is a bit tricky, as it renumbers the revisions
	my @allrevs = BSFileDB::fdb_getall("$projectsdir/$oprojid.pkg/$packid.rev", $srcrevlay);
	if (-e "$projectsdir/$projid.pkg/$packid.rev") {
	  my $lastrev = BSFileDB::fdb_getlast("$projectsdir/$projid.pkg/$packid.rev", $srcrevlay);
	  if ($lastrev && $lastrev->{'rev'}) {
	    for my $rev (@allrevs) {
	      $rev->{'rev'} += $lastrev->{'rev'};
	    }
	  }
	}
	# make trees available in new project
	for my $rev (@allrevs) {
	  BSSrcrep::copytree($projid, $packid, $oprojid, $packid, $rev->{'srcmd5'});
	}
	BSFileDB::fdb_add_multiple("$projectsdir/$projid.pkg/$packid.rev", $srcrevlay, @allrevs);
	$lastorev = $allrevs[-1];
      } else {
	$lastorev = BSFileDB::fdb_getlast("$projectsdir/$oprojid.pkg/$packid.rev", $srcrevlay);
      }
      if (!$lastorev || !$lastorev->{'rev'}) {
	next unless $cgi->{'makeolder'} || $cgi->{'makeoriginolder'};
	# fake empty commit
	$lastorev = { 'version' => 'unknown', 'rev' => 0, 'vrev' => 0, 'srcmd5' => $BSSrcrep::emptysrcmd5 };
      }
      # always do one new commit, we don't use addrev to have full control over vrev
      my $linkinfo = {};
      my $frev = { %$lastorev, 'project' => $oprojid, 'package' => $packid };
      my $files = BSRevision::lsrev($frev, $linkinfo);

      my $servicemark;
      if ($linkinfo->{'xservicemd5'}) {
        if ($cgi->{'noservice'}) {
	  eval {
	    $files = BSSrcServer::Service::handleservice($frev, $files, $linkinfo->{'xservicemd5'});
	  };
	  if ($@) {	
	    warn($@);	# hmm, could not expand service
	    $servicemark = BSSrcServer::Service::genservicemark($projid, $packid, $files, undef, 1);
	  } else {
            BSSrcrep::copyfiles($projid, $packid, $oprojid, $packid, $files);
	    ($servicemark, $files) = BSSrcServer::Service::servicemark_noservice($cgi, $projid, $packid, $files, undef, $linkinfo->{'xservicemd5'});
	  }
	} else {
	  $servicemark = BSSrcServer::Service::genservicemark($projid, $packid, $files, undef, 1);
	}
      }
      BSSrcrep::copyfiles($projid, $packid, $oprojid, $packid, $files);
      $files->{'/SERVICE'} = $servicemark if $servicemark;
      my $newrev = { %$lastorev };
      $newrev->{'srcmd5'} = BSSrcrep::addmeta($projid, $packid, $files);
      $newrev->{'user'} = $user;
      $newrev->{'comment'} = $comment;
      $newrev->{'requestid'} = $cgi->{'requestid'};
      $newrev->{'time'} = time();
      if ($cgi->{'makeolder'}) {
	$newrev->{'vrev'} =~ s/(\d+)$/($1+1).".1"/e;
      } elsif ($cgi->{'makeoriginolder'} && !$files->{'_link'}) {
	$newrev->{'vrev'} =~ s/(\d+)$/$1+2/e;
      } else {
	$newrev->{'vrev'} =~ s/(\d+)$/$1+1/e;
      }
      delete $newrev->{'rev'};
      $newrev = BSRevision::addrev_local({'vrev' => 1}, $projid, $packid, $newrev, $files);
      if ($cgi->{'makeolder'} || ($cgi->{'makeoriginolder'} && !$files->{'_link'})) {
	$lastorev->{'user'} = $user;
	$lastorev->{'comment'} = $comment;
	$lastorev->{'requestid'} = $cgi->{'requestid'};
	$lastorev->{'time'} = time();
	$lastorev->{'user'} = $user;
	if ($cgi->{'makeoriginolder'}) {
	  $lastorev->{'vrev'} =~ s/(\d+)$/($1+1).".1"/e;
	} else {
	  $lastorev->{'vrev'} =~ s/(\d+)$/$1+2/e;
	}
	delete $lastorev->{'rev'};
	$lastorev = BSFileDB::fdb_add_i("$projectsdir/$oprojid.pkg/$packid.rev", $srcrevlay, $lastorev);
      }
      if ($packid ne '_product') {
	my $omb = BSSrcServer::Multibuild::getmultibuild($oprojid, $packid);
	my $mb = BSSrcServer::Multibuild::getmultibuild($projid, $packid);
	if ($mb || $omb) {
          BSSrcServer::Multibuild::setmultibuild($projid, $packid, $omb);
          notify_repservers('package', $projid, $packid) if $cgi->{'withbinaries'};
	}
      }
    }
    # XXX: does this make any sense?
    if ($cgi->{'withbinaries'}) {
      for my $repo (@{$proj->{'repository'} || []}) {
	my $orepo = (grep {$_->{'name'} eq $repo->{'name'}} @{$oproj->{'repository'} || []})[0];
	next unless $orepo;
	for my $arch (@{$repo->{'arch'} || []}) {
	  next unless grep {$_ eq $arch} @{$orepo->{'arch'} || []};

	  # same source and target repo/arch in both projects exists
	  for my $mpackid (BSSrcServer::Multibuild::addmultibuildpackages($projid, undef, $packid)) {
	    my @args;
	    push @args, "cmd=copy";
	    push @args, "oproject=$oprojid";
	    push @args, "opackage=$mpackid"; # same package name
	    push @args, "orepository=$repo->{'name'}"; # same repo name
	    push @args, 'resign=1' if $cgi->{'resign'};
	    my $param = {
	      'uri' => "$reposerver/build/$projid/$repo->{'name'}/$arch/$mpackid",
	      'request' => 'POST',
	    };
	    eval {
	      # ignore failures for now
	      BSWatcher::rpc($param, undef, @args);
	    };
	    warn($@) if $@;
	  }
	}
      }
    }
  }
  # check all packages in project
  notify_repservers('package', $projid);	# also resumes the project
  if ($cgi->{'makeolder'} || $cgi->{'makeoriginolder'}) {
    notify_repservers('package', $oprojid);
  }
  return $BSStdServer::return_ok;
}

sub freezeprojectlink {
  my ($cgi, $projid) = @_;
  my %origins;
  my $proj = BSRevision::readproj_local($projid);
  my @frozen;
  for my $lprojid (map {$_->{'project'}} @{$proj->{'link'} || []}) {
    my @frozenp;
    my @lpackids = findpackages($projid, undef, -1);
    for my $packid (sort(@lpackids)) {
      my $rev = getrev($projid, $packid);
      lsrev_expanded($rev);
      push @frozenp, { name => $packid, srcmd5 => $rev->{'srcmd5'}, 'vrev' => $rev->{'vrev'} };
    }
    push @frozen, { 'project' => $lprojid, 'package' => \@frozenp };
  }
  my $frozenlinks = { 'frozenlink' => \@frozen };
  mkdir_p($uploaddir);
  writexml("$uploaddir/freezeproject.$$", undef, $frozenlinks, $BSXML::frozenlinks);
  $cgi->{'comment'} ||= 'freeze project';
  BSRevision::addrev_meta_replace($cgi, $projid, undef, [ "$uploaddir/freezeproject.$$", undef, '_frozenlinks' ]);
  return $BSStdServer::return_ok;
}

# we're going to auto-update a link. this means we must also
# auto-update the corresponding service result
sub update_link_in_service {
  my ($rev, $files, $xservicemd5, $isbranch) = @_;

  return undef unless defined $xservicemd5;
  return $xservicemd5 if $BSConfig::old_style_services;
  return $xservicemd5 unless $files->{'_link'};
  my $sfiles;
  eval {
    $sfiles = BSRevision::lsrev({%$rev, 'srcmd5' => $xservicemd5});
  };
  return $xservicemd5 unless $sfiles && $sfiles->{'_link'};
  return $xservicemd5 if $sfiles->{'_link'} && $sfiles->{'_link'} eq $files->{'_link'};	# nothing changed
  # okay, we need to generate a new service commit
  my $servicemark = BSSrcServer::Service::genservicemark($rev->{'project'}, $rev->{'package'}, $files, undef, 1);
  return undef unless $servicemark;
  # delete all non-service files unless it's a branch
  if (!$isbranch) {
    delete $sfiles->{$_} for grep {!/^_service[_:]/} keys %$sfiles;
  }
  # copy new link
  $sfiles->{'_link'} = $files->{'_link'};
  # write back new service result
  BSSrcServer::Service::fake_service_run($rev->{'project'}, $rev->{'package'}, $files, $sfiles, $servicemark);
  return $servicemark;
}

sub sourcecopy {
  my ($cgi, $projid, $packid) = @_;
  die("illegal rev parameter\n") if $cgi->{'rev'} && $cgi->{'rev'} ne 'upload';
  my $oprojid = exists($cgi->{'oproject'}) ? $cgi->{'oproject'} : $projid;
  my $opackid = exists($cgi->{'opackage'}) ? $cgi->{'opackage'} : $packid;
  die("makeoriginolder only makes sense with withvrev\n") if $cgi->{'makeoriginolder'} && !$cgi->{'withvrev'};
  die("vrevbump only makes sense with withvrev\n") if $cgi->{'vrevbump'} && !$cgi->{'withvrev'};
  my $orev = $cgi->{'orev'};
  $orev = getrev($oprojid, $opackid, defined($orev) ? $orev : 'latest');
  if ($cgi->{'instantiate'}) {
    my $proj = BSRevision::readproj_local($projid, 1);
    if ($proj && $proj->{'link'}) {
      my $irev = BSSrcServer::Projlink::getrev_projlink($projid, $proj, $packid, undef, undef, 1);
      if ($irev) {
	$orev->{'vrev'} = $irev->{'vrev'} if $irev;	# hack: overwrite vrev with instantiated vrev
	$cgi->{'withvrev'} = 1;
      }
    }
  }
  die("origin must not be virtual for makeoriginolder\n") if $cgi->{'makeoriginolder'} && $orev->{'originproject'};
  $orev->{'linkrev'} = $cgi->{'olinkrev'} if $cgi->{'olinkrev'};
  my $orev_srcmd5 = $orev->{'srcmd5'};  # so that we can restore it later
  my $linkinfo = {};
  my $files = $cgi->{'noservice'} || $cgi->{'expand'} ? lsrev_service($orev, $linkinfo) : BSRevision::lsrev($orev, $linkinfo);
  die("need a revision to copy\n") if !$cgi->{'rev'} && !$cgi->{'orev'} && $oprojid eq $projid && $opackid eq $packid && !$cgi->{'instantiate'} && !($files->{'_link'} && $cgi->{'expand'});

  die("makeoriginolder currently does not work on links\n") if $cgi->{'makeoriginolder'} && $files->{'_link'};

  my $autosimplifylink;
  my $autosimplifylink_lrev;
  my $freezelink;
  my $freezelinkfiles;

  if ($files->{'_link'} && !$cgi->{'dontupdatesource'} && !$cgi->{'rev'}) {
    # fix me: do this in a more generic way
    my $olink = BSRevision::revreadxml($orev, '_link', $files->{'_link'}, $BSXML::link, 1);
    if ($olink) {
      my $lprojid = $oprojid;
      my $lpackid = $opackid;
      my $lrev = $olink->{'rev'};
      $lprojid = $olink->{'project'} if exists $olink->{'project'};
      $lpackid = $olink->{'package'} if exists $olink->{'package'};
      if ($cgi->{'freezelink'}) {
	# we're going to freeze the link in the source
	die("400 freezelink needs expand or noservice\n") unless $cgi->{'noservice'} || $cgi->{'expand'};
	$lrev = getrev($lprojid, $lpackid, $lrev, undef, $olink->{'missingok'});
	my %lrev = %$lrev;
	lsrev_expanded(\%lrev);
	die("400 freezelink refusing to change rev from $olink->{'rev'} to $lrev{'srcmd5'}\n") if $olink->{'rev'} && $lrev{'srcmd5'} ne $olink->{'rev'};
	if (!$olink->{'rev'} || $lrev{'srcmd5'} ne $olink->{'rev'}) {
	  # halt, freeze!
	  $olink->{'rev'} = $lrev{'srcmd5'};
	  $olink->{'vrev'} = $lrev{'vrev'} if defined $lrev{'vrev'};
	  if ($lprojid eq $projid && $lpackid eq $packid) {
	    eval {
	      BSSrcServer::Access::checksourceaccess($projid, $packid);
	      delete $olink->{'missingok'};
	    };
	  }
	  $freezelink = $olink;
	}
	$freezelinkfiles = { %$files };
      } elsif ($lprojid eq $projid && $lpackid eq $packid) {
	# copy destination is target of link
	# we're integrating this link
	$lrev = getrev($lprojid, $lpackid, $lrev);
	$autosimplifylink_lrev = { %$lrev };
	my $lfiles = $cgi->{'noservice'} && !$cgi->{'expand'} ? lsrev_service({ %$lrev }) : BSRevision::lsrev($lrev);
	if ($lfiles->{'_link'} && !$cgi->{'expand'}) {
	  # link to a link, join
	  $files = BSSrcServer::Link::integratelink($lfiles, $lprojid, $lpackid, $lrev, $files, $oprojid, $opackid, $olink, $orev);
	} else {
	  # auto expand
	  $cgi->{'expand'} = 1;
	}
	$autosimplifylink = $olink;
      }
    }
  }

  die("400 freezelink: origin provides no link\n") if $cgi->{'freezelink'} && !$freezelinkfiles;

  my $oldvrev = $orev->{'vrev'};
  if ($files->{'_link'} && $cgi->{'expand'}) {
    my %olrev = %$orev;		# copy so that orev still points to unexpanded sources
    $files = BSSrcServer::Link::handlelinks(\%olrev, $files);
    die("broken link in $oprojid/$opackid: $files\n") unless ref $files;
    $oldvrev = $olrev{'vrev'};
  }

  BSSrcrep::copyfiles($projid, $packid, $oprojid, $opackid, $files);

  # copy multibuild data
  if ($cgi->{'expand'} && $cgi->{'noservice'} && $packid ne '_product') {
    my $omb = BSSrcServer::Multibuild::getmultibuild($oprojid, $opackid);
    BSSrcServer::Multibuild::setmultibuild($projid, $packid, $omb);
  }

  if ($cgi->{'withvrev'} && !$cgi->{'vrev'} && defined($oldvrev)) {
    $cgi->{'vrev'} = $oldvrev;
    my $vrevbump = $cgi->{'vrevbump'} || 1;
    $vrevbump = 2 if $cgi->{'makeoriginolder'} && $vrevbump < 2;
    # bump vrev so that new builds will have a bigger release number
    # (just like in copyproject)
    $cgi->{'vrev'} =~ s/(\d+)$/$1+$vrevbump/e;
  }
  $files = BSSrcServer::Link::keeplink($cgi, $projid, $packid, $files) if $cgi->{'keeplink'};
  my $rev = addrev($cgi, $projid, $packid, $files, $cgi->{'rev'});
  delete $cgi->{'vrev'};

  if ($cgi->{'makeoriginolder'}) {
    # add dummy commit
    my $lastline = BSFileDB::fdb_getlast("$projectsdir/$oprojid.pkg/$opackid.rev", $srcrevlay);
    die("makeoriginolder: $oprojid/$opackid does not exists?\n") unless defined $lastline;
    delete $lastline->{'requestid'};
    delete $lastline->{'rev'};
    $lastline->{'user'} = 'buildservice-autocommit';
    $lastline->{'comment'} = "makeoriginolder vrev update for $projid/$packid";
    $lastline->{'requestid'} = $cgi->{'requestid'} if $cgi->{'requestid'};
    $lastline->{'vrev'} =~ s/(\d+)$/($1+1).".1"/e;
    BSFileDB::fdb_add_i("$projectsdir/$oprojid.pkg/$opackid.rev", $srcrevlay, $lastline);
    notify_repservers('package', $oprojid, $opackid);
  }

  if ($freezelink) {
    mkdir_p($uploaddir);
    writexml("$uploaddir/$$", undef, $freezelink, $BSXML::link);
    $freezelinkfiles->{'_link'} = BSSrcrep::addfile($oprojid, $opackid, "$uploaddir/$$", '_link');
    my $isbranch = grep {(keys %$_)[0] eq 'branch'} @{$freezelink->{'patches'}->{''} || []};
    my $servicemark = update_link_in_service($orev, $freezelinkfiles, $linkinfo->{'xservicemd5'}, $isbranch);
    addrev({ %$cgi, 'user' => 'buildservice-autocommit', 'comment' => 'freeze link', 'servicemark' => $servicemark }, $oprojid, $opackid, $freezelinkfiles);
  } elsif ($autosimplifylink && !defined($autosimplifylink->{'rev'})) {
    $orev->{'srcmd5'} = $orev_srcmd5;	# back to unexpanded

    # make sure that vrev doesn't decrease when copying to the
    # link target
    my $vrevbump = 0;
    if ($rev && $autosimplifylink_lrev && $rev->{'version'} ne $autosimplifylink_lrev->{'version'}) {
      # version change, check if vrev went down
      my $vrev1 = $rev->{'vrev'} || '0';
      my $vrev2 = $autosimplifylink_lrev->{'vrev'} || '0';
      $vrev1 =~ s/.*?(\d+)$/$1/;
      $vrev2 =~ s/.*?(\d+)$/$1/;
      $vrevbump = $vrev2 > $vrev1 ? $vrev2 - $vrev1 : 0;
    }

    my $isbranch = grep {(keys %$_)[0] eq 'branch'} @{$autosimplifylink->{'patches'}->{''} || []};
    if ($isbranch) {
      # update base rev so that there are no changes
      # FIXME: this is a gross hack...
      # we should not need to update the baserev, instead we should change
      # the way branches get applied

      my $ofiles = BSRevision::lsrev($orev);
      delete $ofiles->{'_link'};
      BSSrcrep::copyfiles($projid, $packid, $oprojid, $opackid, $ofiles);
      my $newbase = BSSrcrep::addmeta($projid, $packid, $ofiles);
      if ($autosimplifylink->{'baserev'} ne $newbase) {
	eval {
          my $latestorev = getrev($oprojid, $opackid);
	  my $latestlinkinfo = {};
          my $latestfiles = BSRevision::lsrev($latestorev, $latestlinkinfo);
          if ($latestfiles->{'_link'}) {
	    my $latestl = BSRevision::revreadxml($latestorev, '_link', $latestfiles->{'_link'}, $BSXML::link, 1);
	    my $latestisbranch = grep {(keys %$_)[0] eq 'branch'} @{$latestl->{'patches'}->{''} || []};
	    if ($latestisbranch && $latestl->{'baserev'} eq $autosimplifylink->{'baserev'}) {
	      $latestl->{'baserev'} = $newbase;
	      $latestl->{'patches'}->{''} = [ { 'branch' => undef} ]; # work around xml problem
	      if ($latestl->{'missingok'} &&
		(defined($latestl->{'project'}) ? $latestl->{'project'} : $oprojid) eq $projid &&
		(defined($latestl->{'package'}) ? $latestl->{'package'} : $opackid) eq $packid) {
		eval {
	          BSSrcServer::Access::checksourceaccess($projid, $packid);
		  delete $latestl->{'missingok'};
		};
	      }
	      mkdir_p($uploaddir);
	      writexml("$uploaddir/$$", undef, $latestl, $BSXML::link);
              $latestfiles->{'_link'} = BSSrcrep::addfile($oprojid, $opackid, "$uploaddir/$$", '_link');
              my $servicemark = update_link_in_service($latestorev, $latestfiles, $latestlinkinfo->{'xservicemd5'}, 1);
	      if ($vrevbump) {
		$cgi->{'vrev'} = $latestorev->{'vrev'};
		$cgi->{'vrev'} =~ s/(\d+)$/$1 + $vrevbump/e;
	      }
              addrev({ %$cgi, 'user' => 'buildservice-autocommit', 'comment' => 'baserev update by copy to link target', 'servicemark' => $servicemark }, $oprojid, $opackid, $latestfiles);
	    }
	  }
	};
        warn($@) if $@;
      }
    } else {
      eval {
        my $latestorev = getrev($oprojid, $opackid);
        if ($latestorev->{'srcmd5'} eq $orev->{'srcmd5'}) {
          # simplify link
	  my $latestlinkinfo = {};
          my $latestfiles = BSRevision::lsrev($latestorev, $latestlinkinfo);
          my $nl = { %$autosimplifylink };
          delete $nl->{'patches'};
          delete $nl->{'baserev'};
	  mkdir_p($uploaddir);
          writexml("$uploaddir/$$", undef, $nl, $BSXML::link);
          my $ofiles = {};
          $ofiles->{'_link'} = BSSrcrep::addfile($oprojid, $opackid, "$uploaddir/$$", '_link');
          my $servicemark = update_link_in_service($latestorev, $ofiles, $latestlinkinfo->{'xservicemd5'}, 0);
	  if ($vrevbump) {
	    $cgi->{'vrev'} = $latestorev->{'vrev'};
	    $cgi->{'vrev'} =~ s/(\d+)$/$1 + $vrevbump/e;
	  }
          addrev({ %$cgi, 'user' => 'buildservice-autocommit', 'comment' => 'auto commit by copy to link target', 'servicemark' => $servicemark }, $oprojid, $opackid, $ofiles);
        }
      };
      warn($@) if $@;
    }
    delete $cgi->{'vrev'} if $vrevbump;
  }

  BSSrcServer::Service::runservice($cgi, $rev, $files) unless $cgi->{'noservice'};

  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision_acceptinfo);
}

sub sourcebranch {
  my ($cgi, $projid, $packid) = @_;

  my $usebranch = 1;
  my $oprojid = exists($cgi->{'oproject'}) ? $cgi->{'oproject'} : $projid;
  my $opackid = exists($cgi->{'opackage'}) ? $cgi->{'opackage'} : $packid;
  my $orev = $cgi->{'orev'};
  die("cannot branch myself\n") if $oprojid eq $projid && $opackid eq $packid;
  $orev = getrev($oprojid, $opackid, defined($orev) ? $orev : 'latest', undef, $cgi->{'missingok'});
  $opackid = $orev->{'package'} if $opackid =~ /(?<!^_product)(?<!^_patchinfo):./;
  $orev->{'linkrev'} = $cgi->{'olinkrev'} if $cgi->{'olinkrev'};
  my $files = lsrev_expanded($orev);	# modifies srcmd5, thus also needed for keepcontent case
  if ($cgi->{'keepcontent'}) {
    die("keepcontent is only supported for branches\n") unless $usebranch;
    my $nrev = getrev($projid, $packid, 'latest');
    $files = lsrev_expanded($nrev);
  }
  my $l = {};
  $l->{'project'} = $oprojid if $oprojid ne $projid;
  $l->{'package'} = $opackid if $opackid ne $packid;
  # a missing package entry is bad if the project has sourceaccess
  # disabled, so check if that's the case
  eval {
    BSSrcServer::Access::checksourceaccess($oprojid, $opackid) if $opackid eq $packid && $oprojid ne $projid;
  };
  $l->{'package'} = $opackid if $@;
  $l->{'missingok'} = 'true' if defined $cgi->{'missingok'} && !exists($orev->{'rev'}) && $orev->{'srcmd5'} eq $BSSrcrep::emptysrcmd5;
  $l->{'rev'} = $cgi->{'orev'} if defined $cgi->{'orev'};
  $l->{'baserev'} = $orev->{'srcmd5'};
  if ($cgi->{'extendvrev'}) {
    $l->{'vrev'} = $orev->{'vrev'};
    $l->{'vrev'} ||= '0';
    die("extendvrev error for $l->{'vrev'}\n") unless $l->{'vrev'} =~ s/^(\d+).*?$/($1+1).'.1'/e;
  }
  my $lfiles = {};
  if ($usebranch) {
    $l->{'patches'}->{''} = [ { 'branch' => undef} ];
    BSSrcrep::copyfiles($projid, $packid, $oprojid, $opackid, $files) unless $cgi->{'keepcontent'};
    $lfiles->{$_} = $files->{$_} for keys %$files;
  }
  mkdir_p($uploaddir);
  writexml("$uploaddir/$$", undef, $l, $BSXML::link);
  $lfiles->{'_link'} = BSSrcrep::addfile($projid, $packid, "$uploaddir/$$", '_link');
  my $rev = addrev($cgi, $projid, $packid, $lfiles);
  BSSrcServer::Service::runservice($cgi, $rev, $lfiles) unless $cgi->{'noservice'};
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision_acceptinfo);
}

sub linktobranch {
  my ($cgi, $projid, $packid) = @_;
  my $rev = getrev($projid, $packid);
  $rev->{'linkrev'} = $cgi->{'linkrev'} if $cgi->{'linkrev'};
  my $files = BSRevision::lsrev($rev);
  die("package is not a link\n") unless $files->{'_link'};
  my $l = BSRevision::revreadxml($rev, '_link', $files->{'_link'}, $BSXML::link);
  die("package is already a branch\n") if $l->{'patches'} && grep {(keys %$_)[0] eq 'branch'} @{$l->{'patches'}->{''} || []};
  my $linkinfo = {};
  $files = lsrev_expanded($rev, $linkinfo);
  $l->{'baserev'} = $linkinfo->{'srcmd5'};
  $l->{'patches'}->{''} = [ { 'branch' => undef} ];
  mkdir_p($uploaddir);
  writexml("$uploaddir/$$", undef, $l, $BSXML::link);
  $files->{'_link'} = BSSrcrep::addfile($projid, $packid, "$uploaddir/$$", '_link');
  $cgi->{'comment'} ||= 'converted link to branch';
  $rev = addrev($cgi, $projid, $packid, $files);
  BSSrcServer::Service::runservice($cgi, $rev, $files);
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

sub sourcecollectbuildenv {
  my ($cgi, $projid, $packid) = @_;
  my $oprojid = $cgi->{'oproject'} || $projid;
  my $opackid = $cgi->{'opackage'} || $packid;
  die("cannot collect from myself\n") if $oprojid eq $projid && $opackid eq $packid;
  my $proj = checkprojrepoarch($projid);
  my $oproj = checkprojrepoarch($oprojid);
  my %orepoids;
  for (@{$oproj->{'repository'} || []}) {
    $orepoids{"$oprojid/$_->{'name'}"} = $_;
  }
  for (@{$oproj->{'repository'} || []}) {
    for my $rt (@{$_->{'releasetarget'} || []}) {
      $orepoids{"$rt->{'project'}/$rt->{'repository'}"} ||= $_;
    }
  }
  my %buildenvs;
  for my $repo (@{$proj->{'repository'} || []}) {
    next unless @{$repo->{'arch'} || []};
    my $repoid = $repo->{'name'};
    my @xpath = expandsearchpath($projid, $repoid);
    my $orepo;
    for my $xr (@xpath) {
      $orepo = $orepoids{$xr};
      last if $orepo;
    }
    if ($orepo) {
      my $orepoid = $orepo->{'name'};
      my %oarchs = map {$_ => 1} @{$orepo->{'arch'} || []};
      for my $arch (@{$repo->{'arch'}}) {
	my $be;
	if (!$oarchs{$arch}) {
	  # arch not included, use error buildenv
	  $be = { 'error', "$arch missing in $oprojid/$orepoid" };
	} else {
	  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($oprojid) : $BSConfig::reposerver;
	  eval {
	    $be = BSRPC::rpc("$reposerver/build/$oprojid/$orepoid/$arch/$opackid/_buildenv", $BSXML::buildinfo);
	  };
	  if ($@) {
	    die($@) if $@ !~ /^404/;
	    $be = { 'error', "_buildenv missing in $oprojid/$orepoid" };
	  }
	  $be ||= { 'error', "could not get _buildenv in $oprojid/$orepoid" };
	}
        $buildenvs{"_buildenv.$repoid.$arch"} = BSUtil::toxml($be, $BSXML::buildinfo);
      }
    }
  }
  die("could not get any buildenv, something is wrong\n") unless %buildenvs;
  # add master buildenv, in our case a "fallback error" buildenv
  my $be = { 'error', "no buildenv for this repo/arch" };
  $buildenvs{'_buildenv'} = BSUtil::toxml($be, $BSXML::buildinfo);
  # now add all the buildenvs to the last commit (unexpanded is enough for us)
  my $rev = getrev($projid, $packid);
  my $files = BSRevision::lsrev($rev);
  delete $files->{$_} for grep {/^_buildenv/} keys %$files;
  mkdir_p($uploaddir);
  for my $file (sort keys %buildenvs) {
    writestr("$uploaddir/_be$$", undef, $buildenvs{$file});
    $files->{$file} = BSSrcrep::addfile($projid, $packid, "$uploaddir/_be$$", $file);
  }
  $rev = addrev($cgi, $projid, $packid, $files);
  BSSrcServer::Service::runservice($cgi, $rev, $files);
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

sub deleteuploadrev {
  my ($cgi, $projid, $packid) = @_;
  unlink("$projectsdir/$projid.pkg/$packid.upload-MD5SUMS");
  return $BSStdServer::return_ok;
}

sub unknowncmd {
  my ($cgi, $projid, $packid) = @_;
  die("unknown command \"$cgi->{'cmd'}\"\n");
}

sub delfile {
  my ($cgi, $projid, $packid, $filename) = @_;
  die("no filename\n") unless defined($filename) && $filename ne '';
  die("bad filename\n") if $filename =~ /\// || $filename =~ /^\./;
  if ($cgi->{'meta'}) {
    if ($filename ne '_attribute' && $filename ne '_frozenlinks') {
      die("unsupported meta operation\n");
    }
    my $rev = BSRevision::addrev_meta_replace($cgi, $projid, $packid, [ undef, undef, $filename ]);
    notify_repservers('package', $projid) if $filename eq '_frozenlinks';
    delete $rev->{'project'};
    delete $rev->{'package'};
    return ($rev, $BSXML::revision);
  }
  die("file '$filename' is read-only\n") if ($filename =~ /^_service:/) && !$cgi->{'force'};
  my $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'upload');
  my $files;
  if ($cgi->{'keeplink'}) {
    $files = lsrev_expanded($rev);
  } else {
    $files = BSRevision::lsrev($rev);
  }
  die("404 file '$filename' does not exist\n") unless $files->{$filename};
  delete $files->{$filename};
  $files = BSSrcServer::Link::keeplink($projid, $packid, $files) if $cgi->{'keeplink'};
  $rev = addrev($cgi, $projid, $packid, $files, $cgi->{'rev'});
  BSSrcServer::Service::runservice($cgi, $rev, $files);
  delete $rev->{'project'};
  delete $rev->{'package'};
  return ($rev, $BSXML::revision);
}

sub getrepositorylist {
  my ($cgi, $projid) = @_;
  my $proj = checkprojrepoarch($projid, undef, undef, 1);
  if ($proj->{'remoteurl'}) {
    return (BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}", 'proxy' => $proxy}, $BSXML::dir), $BSXML::dir);
  }
  my @res = map {{'name' => $_->{'name'}}} @{$proj->{'repository'} || []};
  return ({'entry' => \@res}, $BSXML::dir);
}

sub getrepository {
  my ($cgi, $projid, $repoid) = @_;
  my $proj = BSRevision::readproj_local($projid);
  my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
  die("404 $repoid: no such repository\n") unless $repo;
  return ($repo, $BSXML::repo);
}

sub getarchlist {
  my ($cgi, $projid, $repoid) = @_;
  my $proj = checkprojrepoarch($projid, $repoid, undef, 1);
  if ($proj->{'remoteurl'}) {
    return (BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/$repoid", 'proxy' => $proxy}, $BSXML::dir), $BSXML::dir);
  }
  my @repo = grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []};
  die("404 $repoid: no such repository\n") unless @repo;
  my @res = map {{'name' => $_}} @{$repo[0]->{'arch'} || []};
  return ({'entry' => \@res}, $BSXML::dir);
}

sub getresult {
  my ($cgi, $projid) = @_;

  my $proj = checkprojrepoarch($projid, undef, undef, 1);
  if ($proj->{'remoteurl'}) {
    die("oldstate not supported for remote projects\n") if $cgi->{'oldstate'};
    my @args = BSRPC::args($cgi, 'lastbuild', 'view', 'repository', 'arch', 'package', 'code', 'multibuild', 'locallink');
    return (BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/_result", 'proxy' => $proxy}, $BSXML::resultlist, @args), $BSXML::resultlist);
  }
  if ($cgi->{'multibuild'} && $cgi->{'package'} && !$BSStdServer::isajax) {
    $cgi->{'package'} = [ BSSrcServer::Multibuild::addmultibuildpackages($projid, undef, @{$cgi->{'package'}}) ];
  }
  if ($cgi->{'locallink'} && $cgi->{'package'} && !$BSStdServer::isajax) {
    my @packages;
    # find all packages that are links
    my $db = BSDB::opendb($sourcedb, '');
    for my $packid (@{$cgi->{'package'}}) {
      push @packages, $packid;
      my @l = grep {s/^\Q$projid\///} BSDBIndex::getvalues($db, 'linkinfo/package', $packid);
      push @packages, @l;
    }
    @packages = BSUtil::unify(@packages);
    $cgi->{'package'} = \@packages;
  }
  if ($cgi->{'oldstate'} && !$BSStdServer::isajax) {
    my @args = BSRPC::args($cgi, 'oldstate', 'lastbuild', 'view', 'repository', 'arch', 'package', 'code');
    BSHandoff::handoff("/build/$projid/_result", undef, @args);
  }

  my %repoidfilter = map {$_ => 1} @{$cgi->{'repository'} || []};
  my %archfilter = map {$_ => 1} @{$cgi->{'arch'} || []};
  my %view = map {$_ => 1} @{$cgi->{'view'} || ['status']};
  $view{'status'} = 1 if $view{'versrel'};
  my %code = map {$_ => 1} @{$cgi->{'code'} || []};

  if ($cgi->{'repository'}) {
    my %knownrepoids = map {$_->{'name'} => 1} @{$proj->{'repository'} || []};
    for (@{$cgi->{'repository'}}) {
      die("404 unknown repository '$_'\n") if !$knownrepoids{$_};
    }
  }
  if ($cgi->{'package'}) {
    my %knownpackids = map {$_ => 1} findpackages($projid, $proj, 1);
    for (@{$cgi->{'package'}}) {
      die("404 unknown package '$_'\n") if !$knownpackids{$_};
    }
  }
  my @prpas;
  for my $repo (@{$proj->{'repository'} || []}) {
    next if %repoidfilter && !$repoidfilter{$repo->{'name'}};
    my @archs = @{$repo->{'arch'} || []};
    @archs = grep {$archfilter{$_}} @archs if %archfilter;
    push @prpas, map {"$projid/$repo->{'name'}/$_"} @archs;
  }

  BSWatcher::addfilewatcher("$projectsdir/$projid.xml") if $BSStdServer::isajax;

  if (!@prpas) {
    my $state = "00000000000000000000000000000000";
    return undef if $BSStdServer::isajax && $cgi->{'oldstate'} && $state eq $cgi->{'oldstate'};
    return ({'state' => $state}, $BSXML::resultlist);
  }

  my $ps = {};
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args;
  push @args, 'lastbuild' if $cgi->{'lastbuild'};
  push @args, "oldstate=$cgi->{'oldstate'}" if $cgi->{'oldstate'};
  push @args, map {"prpa=$_"} @prpas;
  push @args, map {"package=$_"} @{$cgi->{'package'} || []};
  push @args, map {"code=$_"} @{$cgi->{'code'} || []};
  push @args, 'withbinarylist' if $view{'binarylist'};
  push @args, 'withstats' if $view{'stats'};
  push @args, 'withversrel' if $view{'versrel'};
  push @args, 'summary' if $view{'summary'} && !$view{'status'};
  eval {
    $ps = BSWatcher::rpc("$reposerver/_result", $BSXML::resultlist, @args);
  };
  if ($@) {
    print "warning: $reposerver: $@";
    $ps = {};
  }
  return if $BSStdServer::isajax && !defined($ps);
  if ($view{'summary'} && $view{'status'}) {
    my @order = ('succeeded', 'failed', 'unresolvable', 'broken', 'scheduled');
    my %order = map {$_ => 1} @order;
    for my $p (@{$ps->{'result'} || []}) {
      my %sum;
      for my $pp (@{$p->{'status'} || []}) {
        $sum{$pp->{'code'}}++ if $pp->{'code'};
      }
      my @sum = grep {exists $sum{$_}} @order;
      push @sum, grep {!$order{$_}} sort keys %sum;
      $p->{'summary'} = {'statuscount' => [ map {{'code' => $_, 'count' => $sum{$_}}} @sum ] };
    }
  }
  if (!$view{'status'}) {
    for my $p (@{$ps->{'result'} || []}) {
      delete $p->{'status'};
    }
  }
  return ($ps, $BSXML::resultlist);
}

sub docommand {
  my ($cgi, $projid) = @_;

  my %repoidfilter = map {$_ => 1} @{$cgi->{'repository'} || []};
  my %archfilter = map {$_ => 1} @{$cgi->{'arch'} || []};

  my $proj = BSRevision::readproj_local($projid);
  if ($cgi->{'cmd'} eq 'suspendproject') {
    notify_repservers('suspendproject', $projid, undef, $cgi->{'comment'} || 'suspendproject');
    return $BSStdServer::return_ok;
  }
  if ($cgi->{'cmd'} eq 'resumeproject') {
    notify_repservers('resumeproject', $projid, undef, $cgi->{'comment'} || 'suspendproject');
    return $BSStdServer::return_ok;
  }
  my @prpas;
  for my $repo (@{$proj->{'repository'} || []}) {
    next if %repoidfilter && !$repoidfilter{$repo->{'name'}};
    my @archs = @{$repo->{'arch'} || []};
    @archs = grep {$archfilter{$_}} @archs if %archfilter;
    push @prpas, map {"$projid/$repo->{'name'}/$_"} @archs;
  }
  die("no repository defined\n") unless @prpas;
  my @packids = @{$cgi->{'package'} || []};
  if ($cgi->{'cmd'} eq 'wipepublishedlocked') {
    die("wipepublishedlocked can only wipe complete repos\n") if $cgi->{'arch'} || $cgi->{'code'} || @packids;
  } else {
    if (@packids) {
      my %packids = map {$_ => 1} findpackages($projid, $proj, 1);
      my @badpacks = grep {!$packids{$_}} @packids;
      die("404 unknown package: @badpacks\n") if @badpacks;
    } else {
      @packids = findpackages($projid, $proj);
    }
    die("no packages defined\n") unless @packids;
  }
  die("illegal wipe parameter\n") if $cgi->{'wipe'} && $cgi->{'cmd'} ne 'wipe';
  
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $res;
  my @args;
  push @args, map {"prpa=$_"} @prpas;
  push @args, map {"package=$_"} @packids;
  push @args, map {"code=$_"} @{$cgi->{'code'} || []};
  push @args, map {"wipe=$_"} @{$cgi->{'wipe'} || []};
  push @args, "cmd=$cgi->{'cmd'}";
  my $param = {
    'uri' => "$reposerver/_command",
    'request' => 'POST',
  };
  $res = BSWatcher::rpc($param, undef, @args);
  return $res;
}

sub checkprojrepoarch {
  my ($projid, $repoid, $arch, $remoteok) = @_;
  my $proj = BSRevision::readproj_local($projid, 1);
  $proj = BSSrcServer::Remote::remoteprojid($projid) if $remoteok && (!$proj || $proj->{'remoteurl'});
  die("404 project '$projid' does not exist\n") if !$proj;
  die("404 project '$projid' is remote\n") if $proj->{'remoteurl'} && !$remoteok;
  return $proj if $proj->{'remoteurl'};
  return $proj unless defined $repoid;
  my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
  die("404 project '$projid' has no repository '$repoid'\n") unless $repo;
  return $proj unless defined $arch;
  die("404 repository '$projid/$repoid' has no architecture '$arch'\n") unless grep {$_ eq $arch} @{$repo->{'arch'} || []};
  return $proj;
}

sub getbuilddepinfo {
  my ($cgi, $projid, $repoid, $arch) = @_;

  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'package', 'view');
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/_builddepinfo",
  };
  if (BSServer::have_content()) {
    $param->{'request'} = 'POST';
    $param->{'data'} = BSServer::read_data(10000000);
    $param->{'headers'} = [ 'Content-Type: application/octet-stream' ];
  }
  my $res = BSWatcher::rpc($param, $BSXML::builddepinfo, @args);
  return ($res, $BSXML::builddepinfo);
}

sub getjobhistory {
  my ($cgi, $projid, $repoid, $arch) = @_;

  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'limit', 'package', 'code');
  my $res = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/_jobhistory", $BSXML::jobhistlist, @args);
  return ($res, $BSXML::jobhistlist);
}

sub getpackagelist_build {
  my ($cgi, $projid, $repoid, $arch) = @_;
  if ($cgi->{'view'}) {
    die("unknown view '$cgi->{'view'}'\n") unless $cgi->{'view'} eq 'binaryversions' || $cgi->{'view'} eq 'binaryversionscode';
    my $proj = checkprojrepoarch($projid, $repoid, $arch, 1);
    my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
    my @args = BSRPC::args($cgi, 'view', 'package');
    my $param = {
      'uri' => "$reposerver/build/$projid/$repoid/$arch",
      'ignorestatus' => 1,
      'receiver' => \&BSServer::reply_receiver,
    };
    if ($proj->{'remoteurl'}) {
      if (!$BSStdServer::isajax) {
	BSHandoff::handoff("/build/$projid/$repoid/$arch", undef, @args);
      }
      $param->{'uri'} = "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/$repoid/$arch";
      $param->{'proxy'} = $proxy;
    }
    BSWatcher::rpc($param, undef, @args);
    return undef;
  }
  return getpackagelist({ %$cgi, 'expand' => 1, 'noorigins' => 1 }, $projid, $repoid, $arch);
}

sub getbinarylist {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;

  my $view = $cgi->{'view'};
  my $nosource = $cgi->{'nosource'};
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'view', 'nosource', 'withmd5', 'binary');
  if ($view && ($view eq 'cache' || $view eq 'cpio' || $view eq 'solv' || $view eq 'solvstate')) {
    # do not check arch in interconnect mode
    my $proj = checkprojrepoarch($projid, $repoid, undef, 1);
    if ($view eq 'cpio' && $packid eq '_repository' && !@{$cgi->{'binary'} || []}) {
      if (!$proj->{'remoteurl'} || $proj->{'partition'}) {
        my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
        my $doddata = (grep {($_->{'arch'} || '') eq $arch} @{($repo || {})->{'download'} || []})[0];
	die("will not get all dod packages\n") if $doddata;
      }
    }
    if (!$BSStdServer::isajax) {
      if ($proj->{'remoteurl'} && $view eq 'cpio' && $packid eq '_repository' && !$nosource && @{$cgi->{'binary'} || []}) {
	# hand over to worker_getbinaries to get the answer cached
	@args = ();
	push @args, "project=$projid";
	push @args, "repository=$repoid";
	push @args, "arch=$arch";
	push @args, "binaries=".join(',', @{$cgi->{'binary'} || []});
	BSHandoff::handoff('/getbinaries', undef, @args);
      }
      BSHandoff::handoff("/build/$projid/$repoid/$arch/$packid", undef, @args);
    }
    my $param = {
      'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid",
      'ignorestatus' => 1,
      'receiver' => \&BSServer::reply_receiver,
    };
    if ($proj->{'remoteurl'}) {
      $param->{'uri'} = "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/$repoid/$arch/$packid";
      $param->{'proxy'} = $proxy;
    }
    BSWatcher::rpc($param, undef, @args);
    return undef;
  }
  my $proj = checkprojrepoarch($projid, $repoid, $arch, 1);
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid",
  };
  if ($proj->{'remoteurl'}) {
    $param->{'uri'} = "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/$repoid/$arch/$packid";
    $param->{'proxy'} = $proxy;
  }
  if ($view && $view eq 'binaryversions') {
    push @args, 'nometa=1' if $cgi->{'nometa'};
    if (!$BSStdServer::isajax && $packid eq '_repository' && $proj->{'remoteurl'} && !$proj->{'partition'}) {
      # hand over to getbinaryversions for chunking
      @args = ();
      push @args, "project=$projid";
      push @args, "repository=$repoid";
      push @args, "arch=$arch";
      push @args, 'nometa=1' if $cgi->{'nometa'};
      push @args, "binaries=".join(',', @{$cgi->{'binary'} || []});
      BSHandoff::handoff('/getbinaryversions', undef, @args);
    }
    if (!$BSStdServer::isajax && $packid eq '_repository') {
      # this can take a while if we have dod configured, in that case handoff
      my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
      if ($repo) {
	my $doddata = (grep {($_->{'arch'} || '') eq $arch} @{$repo->{'download'} || []})[0];
	if ($doddata) {
	  die("will not get all dod packages\n") unless @{$cgi->{'binary'} || []};
	  BSHandoff::handoff("/build/$projid/$repoid/$arch/$packid", undef, @args);
	}
      }
    }
    my $bvl = BSWatcher::rpc($param, $BSXML::binaryversionlist, @args);
    return ($bvl, $BSXML::binaryversionlist);
  }
  if ($view && $view eq 'names' && $cgi->{'binary'} && !$BSStdServer::isajax && $packid eq '_repository') {
    # this can take a while if we have dod configured, in that case handoff
    my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
    if ($repo) {
      my $doddata = (grep {($_->{'arch'} || '') eq $arch} @{$repo->{'download'} || []})[0];
      if ($doddata) {
        BSHandoff::handoff("/build/$projid/$repoid/$arch/$packid", undef, @args);
      }
    }
  }
  if ($view && ($view eq 'cpioheaders' || $view eq 'cpioheaderchksums')) {
     $param->{'ignorestatus'} = 1;
     $param->{'receiver'} = \&BSServer::reply_receiver;
     BSWatcher::rpc($param, undef, @args);
     return undef;
  }
  my $bl = BSWatcher::rpc($param, $BSXML::binarylist, @args);
  return ($bl, $BSXML::binarylist);
}

sub getbinary {
  my ($cgi, $projid, $repoid, $arch, $packid, $filename) = @_;
  my $proj = checkprojrepoarch($projid, $repoid, $arch, 1);
  my $view = $cgi->{'view'} || '';
  if ($proj->{'remoteurl'} && $packid eq '_repository' && !$view) {
    # hack: reroute to /getbinaries so that our local cache is used
    die("need the raw package name as filename for remote repository access\n") if $filename =~ /\.(?:$binsufsre)$/;
    my @args;
    push @args, "project=$projid";
    push @args, "repository=$repoid";
    push @args, "arch=$arch";
    push @args, "binaries=$filename";
    push @args, "raw=1";
    BSHandoff::handoff('/getbinaries', undef, @args);
  }
  if ($view eq 'publishedpath') {
    die("publishedpath does not work for _repository\n") if $packid eq '_repository';
    return published_path($cgi, $projid, $repoid);
  }
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args;
  push @args, "view=$view" if $view;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid/$filename",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
  };
  if ($view && $view eq 'fileinfo_ext') {
    my $partition = BSSrcServer::Partition::projid2partition($projid);
    my $projpack = (getprojpack({'nopackages' => 1, 'withrepos' => 1, 'expandedrepos' => 1, 'withremotemap' => 1, 'withconfig' => 1, 'partition' => $partition}, [ $projid ], [ $repoid ], undef, $arch))[0];
    if ($projpack) {
      if ($projpack->{'project'} && $projpack->{'project'}->[0]->{'name'} eq $projid) {
        my $config = (getbuildconfig({}, $projid, $repoid))[0];
	$projpack->{'project'}->[0]->{'config'} = $config if $config;
      }
      $param->{'request'} = 'POST';
      $param->{'data'} = BSUtil::toxml($projpack, $BSXML::projpack);
      $param->{'headers'} = [ 'Content-Type: application/octet-stream' ];
    }
  }
  if ($proj->{'remoteurl'}) {
    $param->{'uri'} = "$proj->{'remoteurl'}/build/$proj->{'remoteproject'}/$repoid/$arch/$packid/$filename";
    $param->{'proxy'} = $proxy;
  }
  BSWatcher::rpc($param, undef, @args);
  return undef;
}

sub putbinary {
  my ($cgi, $projid, $repoid, $arch, $filename) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'ignoreolder', 'wipe');
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/_repository/$filename",
    'request' => 'PUT',
    'data' => \&BSServer::forward_sender,
    'chunked' => 1,
  };
  # XXX add return type checking
  return BSWatcher::rpc($param, undef, @args);
}

sub delbinary {
  my ($cgi, $projid, $repoid, $arch, $filename) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/_repository/$filename",
    'request' => 'DELETE',
  };
  return BSWatcher::rpc($param, undef);
}

sub copybuild {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  die("illegal package '$packid'\n") if $packid =~ /^_/ && !($packid =~ /^_product:/);
  checkprojrepoarch($projid, $repoid, $arch);
  my $oprojid = defined($cgi->{'oproject'}) ? $cgi->{'oproject'} : $projid;
  my $opackid = defined($cgi->{'opackage'}) ? $cgi->{'opackage'} : $packid;
  my $orepoid = defined($cgi->{'orepository'}) ? $cgi->{'orepository'} : $repoid;
  die("nothing to do\n") if "$oprojid/$opackid/$orepoid" eq "$projid/$packid/$repoid";
  checkprojrepoarch($oprojid, $orepoid, $arch);
  # make sure the packages exist. not cheap, but does everything we need
  getrev($projid, $packid);
  getrev($oprojid, $opackid);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $oreposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($oprojid) : $BSConfig::reposerver;
  if ($reposerver ne $oreposerver) {
    die("cannot copy binaries between different reposiory servers yet\n");
  }
  my %tocopy = ($opackid => $packid);
  if ($cgi->{'multibuild'}) {
    my $mb = BSSrcServer::Multibuild::getmultibuild($oprojid, $opackid) || {};
    $tocopy{"$opackid:$_"} = "$packid:$_" for @{$mb->{'flavor'} || $mb->{'package'} || []};
  }
  for $opackid (sort keys %tocopy) {
    my @args;
    push @args, "cmd=copy";
    push @args, "oproject=$oprojid";
    push @args, "opackage=$opackid";
    push @args, "orepository=$orepoid";
    push @args, "setupdateinfoid=$cgi->{'setupdateinfoid'}" if $cgi->{'setupdateinfoid'};
    push @args, "setrelease=$cgi->{'setrelease'}" if $cgi->{'setrelease'};
    push @args, 'resign=1' if $cgi->{'resign'};
    my $param = {
      'uri' => "$reposerver/build/$projid/$repoid/$arch/$tocopy{$opackid}",
      'request' => 'POST',
    };
    BSWatcher::rpc($param, undef, @args);
  }
  return $BSStdServer::return_ok;
}

sub uploadbuild {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  die("illegal package '$packid'\n") if $packid =~ /^_/ && !($packid =~ /^_product:/);
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid",
    'request' => 'POST',
    'data' => \&BSServer::forward_sender,
    'chunked' => 1,
  };
  # XXX add return type checking
  return BSWatcher::rpc($param, undef);
}


sub getlogfile {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch);

  if (!$cgi->{'start'}) {
    # check if the package is broken
    my $rev = getrev($projid, $packid, 'build');
    eval {
      lsrev_expanded($rev);
    };
    if ($@) {
      my $error = $@;
      if ($rev->{'srcmd5'}) {
        my $files = BSRevision::lsrev($rev);
	if ($files->{'_serviceerror'}) {
	  $error = BSRevision::revreadstr($rev, '_serviceerror', $files->{'_serviceerror'});
	} elsif ($files->{'_linkerror'}) {
	  $error = BSRevision::revreadstr($rev, '_linkerror', $files->{'_linkerror'});
	}
      }
      if ($cgi->{'view'} && $cgi->{'view'} eq 'entry') {
        my $entry = {'name' => '_log', 'size' => length($error)};
        return ({'entry' => [ $entry ]}, $BSXML::dir);
      }
      return $error;
    }
  }

  my @args = BSRPC::args($cgi, 'last', 'nostream', 'start', 'end', 'view');
  if (!$BSStdServer::isajax && !$cgi->{'view'}) {
    BSHandoff::handoff("/build/$projid/$repoid/$arch/$packid/_log", undef, @args);
  }
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid/_log",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
    'joinable' => 1,
  };
  BSWatcher::rpc($param, undef, @args);
  return undef; # always streams result
}

sub getjobstatus {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $jobstatus = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/$packid/_jobstatus", $BSXML::jobstatus);
  return ($jobstatus, $BSXML::jobstatus);
}

sub getbuildhistory {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'limit');
  my $buildhist = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/$packid/_history", $BSXML::buildhist, @args);
  return ($buildhist, $BSXML::buildhist);
}

sub getbuildinfo {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch, 1);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'internal', 'debug', 'add');
  my $buildinfo = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/$packid/_buildinfo", $BSXML::buildinfo, @args);
  return ($buildinfo, $BSXML::buildinfo);
}

sub getbuildinfo_post {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch, 1);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my @args = BSRPC::args($cgi, 'debug', 'add');
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid/_buildinfo",
    'request' => 'POST',
    'data' => \&BSServer::forward_sender,
    'chunked' => 1,
  };
  my $buildinfo = BSWatcher::rpc($param, $BSXML::buildinfo, @args);
  return ($buildinfo, $BSXML::buildinfo);
}

sub getbuildreason {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $reason = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/$packid/_reason", $BSXML::buildreason);
  return ($reason, $BSXML::buildreason);
}

sub getmultibuildpackages {
  my ($cgi, $projid, $packid) = @_;
  my $pack_hash = {};
  my $mb = BSSrcServer::Multibuild::getmultibuild($projid, $packid) || {};
  foreach my $pack (@{$mb->{'package'}}) {
    my $entry = {'name' => $pack};
    push @{$pack_hash->{'entry'}}, $entry;
  }
  return ($pack_hash, $BSXML::dir);
}

sub getbuildstatus {
  my ($cgi, $projid, $repoid, $arch, $packid) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $status = BSWatcher::rpc("$reposerver/build/$projid/$repoid/$arch/$packid/_status", $BSXML::buildstatus);
  return ($status, $BSXML::buildstatus);
}

sub add_daemondata {
  my ($ws, $daemondata) = @_;
  my $part = (grep {!defined($_->{'name'})} @{$ws->{'partition'} || []})[0];
  if (!$part) {
    $part = {};
    $ws->{'partition'} ||= [];
    unshift @{$ws->{'partition'}}, $part;
  }
  $part->{'daemon'} ||= [];
  unshift(@{$part->{'daemon'}}, $daemondata);
  return $ws;
}

sub add_localworkerstatus {
  my ($cgi, $ws, $type, $lock) = @_;
  return $ws if $cgi->{'type'} && !grep {$_ eq $type} @{$cgi->{'type'}};
  return $ws if $cgi->{'arch'} && !grep {$_ eq $type} @{$cgi->{'arch'}};
  return $ws if $type eq 'servicedispatch' && !$BSConfig::servicedispatch;
  return $ws if $type ne 'srcserver' && ! -e $lock;
  my $daemondata = {'state' => 'dead', 'type' => $type};
  if ($type eq 'srcserver') {
    my $req = $BSServer::request;
    $daemondata->{'starttime'} = $req->{'server'}->{'starttime'} if $req && $req->{'server'};
    if ($req && $req->{'conf'} && $req->{'conf'}->{'handoffpath'}) {
      $lock = "$req->{'conf'}->{'handoffpath'}.lock";
    }
    $daemondata->{'state'} = 'running' unless $lock;
  }
  local *F;
  if ($lock && open(F, '<', $lock)) {
    if (!flock(F, LOCK_EX | LOCK_NB)) {
      my @s = stat(F);
      $daemondata->{'state'} = 'running';
      $daemondata->{'starttime'} ||= $s[9] if @s;
    }
    close F;
  }
  return add_daemondata($ws, $daemondata);
}

sub add_serviceserverstatus {
  my ($cgi, $ws) = @_;
  return $ws unless $BSConfig::serviceserver;
  return $ws if $cgi->{'type'} && !grep {$_ eq 'service'} @{$cgi->{'type'}};
  return $ws if $cgi->{'arch'} && !grep {$_ eq 'service'} @{$cgi->{'arch'}};
  my $daemondata = {'state' => 'dead', 'type' => 'service'};
  my $param = {
    'uri' => "$BSConfig::serviceserver/serverstatus",
    'timeout' => 60,
  };
  eval {
    my $sta = BSWatcher::rpc($param, $BSXML::serverstatus);
    $daemondata->{'state'} = 'running';
    $daemondata->{'starttime'} = $sta->{'starttime'};
  };
  warn($@) if $@;
  return add_daemondata($ws, $daemondata);
}

sub add_clouduploadserverstatus {
  my ($cgi, $ws) = @_;
  return $ws unless $BSConfig::clouduploadserver;
  return $ws if $cgi->{'type'} && !grep {$_ eq 'clouduploadserver' || $_ eq 'clouduploadworker'} @{$cgi->{'type'}};
  return $ws if $cgi->{'arch'} && !grep {$_ eq 'clouduploadserver' || $_ eq 'clouduploadworker'} @{$cgi->{'arch'}};
  my $daemondatas = [ {'state' => 'dead', 'type' => 'clouduploadserver'} ];
  my $param = {
    'uri' => "$BSConfig::clouduploadserver/workerstatus",
    'timeout' => 60,
  };
  eval {
    my $sws = BSWatcher::rpc($param, $BSXML::workerstatus);
    $daemondatas = $sws->{'partition'}->[0]->{'daemon'}
  };
  warn($@) if $@;
  for my $daemondata (reverse(@$daemondatas)) {
    $ws = add_daemondata($ws, $daemondata);
  }
  return $ws;
}

sub getworkerstatus {
  my ($cgi) = @_;
  # compat
  if ($cgi->{'scheduleronly'} && !$cgi->{'daemonsonly'}) {
    $cgi->{'daemonsonly'} = delete $cgi->{'scheduleronly'};
  }
  my @args = BSRPC::args($cgi, 'daemonsonly', 'arch');

  if (!$BSConfig::partitioning || !$BSConfig::partitionservers) {
    my $reposerver = $BSConfig::reposerver;
    my $param = {
      'uri' => "$reposerver/workerstatus",
      'timeout' => 60,
    };
    my $ws = { 'partition' => [ { 'daemon' => [ { 'state' => 'dead', 'type' => 'repserver' } ] } ] };
    eval {
      $ws = BSWatcher::rpc($param, $BSXML::workerstatus, @args);
    };
    warn($@) if $@;
    delete $_->{'uri'} for @{$ws->{'idle'} || []};
    delete $_->{'uri'} for @{$ws->{'building'} || []};
    add_clouduploadserverstatus($cgi, $ws);
    add_serviceserverstatus($cgi, $ws);
    add_localworkerstatus($cgi, $ws, 'deltastore', "$rundir/bs_deltastore.lock");
    add_localworkerstatus($cgi, $ws, 'servicedispatch', "$rundir/bs_servicedispatch.lock");
    add_localworkerstatus($cgi, $ws, 'srcserver');
    return ($ws, $BSXML::workerstatus);
  }

  # cummulated worker status
  my $cws;
  if (!$cgi->{'daemonsonly'}) {
    $cws->{'clients'} = 0;
    $cws->{'building'} = [];
    $cws->{'waiting'} = [];
    $cws->{'blocked'} = [];
    $cws->{'buildavg'} = [];
    $cws->{'idle'} = [];
  }
  $cws->{'partition'} = [];

  my %reposervers = map {$_ => 1} values(%$BSConfig::partitionservers);
  for my $reposerver (sort keys %reposervers) {
    my $param = {
      'uri' => "$reposerver/workerstatus",
      'timeout' => 60,
    };
    my $ws;
    eval {
      $ws = BSWatcher::rpc("$reposerver/workerstatus", $BSXML::workerstatus, @args);
    };
    warn($@) if $@;
    if (!$ws) {
      for my $par (grep {$BSConfig::partitionservers->{$_} eq $reposerver} sort(keys(%$BSConfig::partitionservers))) {
        push @{$ws->{'partition'}}, { 'name' => $par, 'daemon' => [ { 'state' => 'dead', 'type' => 'repserver' } ] };
      }
    }

    push @{$cws->{'partition'}}, @{$ws->{'partition'}};
    next if $cgi->{'daemonsonly'};

    delete $_->{'uri'} for @{$ws->{'idle'} || []};
    delete $_->{'uri'} for @{$ws->{'building'} || []};
    for my $workerstate (qw{idle building down dead away}) {
      push @{$cws->{$workerstate}}, @{$ws->{$workerstate}} if $ws->{$workerstate};
    }
    for my $b (@{$ws->{'waiting'} || []}) {
      my $ob = (grep {$_->{'arch'} eq $b->{'arch'}} @{$cws->{'waiting'} || []})[0];
      if (!$ob) {
        $ob = {'arch' => $b->{'arch'}, 'jobs' => 0};
        push @{$cws->{'waiting'}}, $ob;
      }
      $ob->{'jobs'} += $b->{'jobs'};
    }
    for my $b (@{$ws->{'blocked'} || []}) {
      my $ob = (grep {$_->{'arch'} eq $b->{'arch'}} @{$cws->{'blocked'} || []})[0];
      if (!$ob) {
        $ob = {'arch' => $b->{'arch'}, 'jobs' => 0};
        push @{$cws->{'blocked'}}, $ob;
      }
      $ob->{'jobs'} += $b->{'jobs'};
    }
    for my $b (@{$ws->{'buildavg'} || []}) {
      my $ob = (grep {$_->{'arch'} eq $b->{'arch'}} @{$cws->{'buildavg'} || []})[0];
      if (!$ob) {
        $ob = {'arch' => $b->{'arch'}, 'buildavg' => 0, 'count' => 0};
        push @{$cws->{'buildavg'}}, $ob;
      }
      $ob->{'buildavg'} += $b->{'buildavg'};
      $ob->{'count'} += 1;
    }
    $cws->{'clients'} += $ws->{'clients'} if $ws->{'clients'};
  }
  for my $b (@{$cws->{'buildavg'} || []}) {
    $b->{'buildavg'} /= delete $b->{'count'};
  }

  # sort
  if (!$cgi->{'daemonsonly'}) {
    $cws->{'idle'} = [ sort {$a->{'workerid'} cmp $b->{'workerid'} || $a->{'uri'} cmp $b->{'uri'} || $a cmp $b} @{$cws->{'idle'}} ];
    $cws->{'building'} = [ sort {$a->{'workerid'} cmp $b->{'workerid'} || $a->{'uri'} cmp $b->{'uri'} || $a cmp $b} @{$cws->{'building'}} ];
    $cws->{'waiting'} = [ sort {$a->{'arch'} cmp $b->{'arch'} || $a cmp $b}  @{$cws->{'waiting'}} ];
    $cws->{'blocked'} = [ sort {$a->{'arch'} cmp $b->{'arch'} || $a cmp $b}  @{$cws->{'blocked'}} ];
    $cws->{'buildavg'} = [ sort {$a->{'arch'} cmp $b->{'arch'} || $a cmp $b}  @{$cws->{'buildavg'}} ];
  }
  
  add_clouduploadserverstatus($cgi, $cws);
  add_serviceserverstatus($cgi, $cws);
  add_localworkerstatus($cgi, $cws, 'deltastore', "$rundir/bs_deltastore.lock");
  add_localworkerstatus($cgi, $cws, 'servicedispatch', "$rundir/bs_servicedispatch.lock");
  add_localworkerstatus($cgi, $cws, 'srcserver');
  return ($cws, $BSXML::workerstatus);
}

sub getajaxstatus {
  my ($cgi) = @_;
  BSHandoff::handoff('/ajaxstatus') if !$BSStdServer::isajax;
  my $r = BSWatcher::getstatus();
  return ($r, $BSXML::ajaxstatus);
}

####################################################################

sub search_proj {
  my ($cgi, $match, $id) = @_;
  $match =~ s/^\[(.*)\]$/$1/s;
  my $data = [];
  for my $projid (findprojects()) {
    my $proj = BSRevision::readproj_local($projid);
    push @$data, $proj;
  }
  $data = BSXPath::match($data, $match);
  if ($id) {
    for (@{$data || []}) {
      $_ = {'name' => $_->{'name'}};
    }
  }
  my $res = {'project' => $data};
  return ($res, $BSXML::collection);
}

sub pkgsearch_fetch {
  my ($db, $k) = @_;
  my ($projid, $packid) = split('/', $k, 2);
  my $pack = BSRevision::readpack_local($projid, $packid, 1) || {'name' => $packid};
  $pack->{'project'} = $projid;
  #my @linkinfo = BSDBIndex::getvalues($db, 'linkinfo', $k);
  #$pack->{'linkinfo'} = $linkinfo[0] if @linkinfo;
  return $pack;
}

sub pkgsearch_indexfunc {
  my ($db, $path, $value, $lkeys) = @_;
  if (!defined($path)) {
    return @{$db->{'_allkeys'}} if $db->{'_allkeys'};
    my @projids = findprojects();
    my @r;
    for my $projid (@projids) {
      push @r, map {"$projid/$_"} BSRevision::lspackages_local($projid);
    }
    $db->{'_allkeys'} = \@r;
    return @r;
  } elsif (!defined($value)) {
    return BSDBIndex::getkeys($db, "$db->{'index'}$path") if $path =~ /^linkinfo\//;
    return findprojects() if $path eq 'project';
    if ($path eq 'name') {
      $lkeys = [ pkgsearch_indexfunc($db) ] unless $lkeys;
      my %v = map {$_ => 1} grep {s/^.*\///} map {$_} @$lkeys;
      return sort keys %v;
    }
  } else {
    return BSDBIndex::getvalues($db, "$db->{'index'}$path", $value) if $path =~ /^linkinfo\//;
    return map {"$value/$_"} BSRevision::lspackages_local($value) if $path eq 'project';
    if ($path eq 'name') {
      $lkeys = [ pkgsearch_indexfunc($db) ] unless $lkeys;
      return grep {/\Q$value\E$/} @$lkeys;
    }
  }
  return ();
}

sub search_pack {
  my ($cgi, $match, $id) = @_;
  $match =~ s/^\[(.*)\]$/$1/s;
  # really ugly hack to speed up needed api call
  if ($match =~ /^\@project='(.+)' and starts-with\(\@name,'(.+)'\)$/) {
    my $projid = $1;
    my $startswith = $2;
    $projid =~ s/''/'/g;
    $startswith =~ s/''/'/g;
    my @packages = BSRevision::lspackages_local($projid);
    my $data = [];
    for my $packid (grep {/^\Q$startswith\E/} @packages) {
      my ($pack, undef) = getpackage($cgi, $projid, $packid);
      $pack->{'project'} = $projid;
      push @$data, $pack;
    }
    my $res = {'package' => $data};
    return ($res, $BSXML::collection);
  }
  my $db = BSDB::opendb($sourcedb, '');
  $db->{'indexfunc'} = {
    'project' => \&pkgsearch_indexfunc,
    'name' => \&pkgsearch_indexfunc,
    'linkinfo/project' => \&pkgsearch_indexfunc,
    'linkinfo/package' => \&pkgsearch_indexfunc,
    'linkinfo/rev' => \&pkgsearch_indexfunc,
  };
  $db->{'noindexatall'} = 1;
  $db->{'fetch'} = \&pkgsearch_fetch;
  my $data = BSXPathKeys::node($db, '');
  if ($id) {
    $data = $data->keymatch($match);
    for (@$data) {
      my @p = split('/', $_, 2);
      $_ = {'name' => $p[1], 'project' => $p[0]};
    }
  } else {
    $data = BSXPath::match($data, $match);
    delete $_->{'linkinfo'} for @$data;
  }
  my $res = {'package' => $data};
  return ($res, $BSXML::collection);
}

sub search_proj_id {
  return search_proj(@_, 1);
}

sub search_pack_id {
  return search_pack(@_, 1);
}

#############################################################################

sub search_published_updatedb {
  my ($cgi) = @_;
  die("unknown command '$cgi->{'cmd'}'\n") unless $cgi->{'cmd'} eq 'updatedb';
  my $data = BSServer::read_data();
  $data = Storable::thaw($data);
  die("no data\n") unless $data && @$data;
  my $patterndb;
  my $binarydb;
  my $repoinfodb;
  mkdir_p($extrepodb) unless -d $extrepodb;
  while (@$data) {
    my ($w, $k, $v) = splice(@$data, 0, 3);
    if ($w eq 'binary') {
      $binarydb = BSDB::opendb($extrepodb, 'binary') unless $binarydb;
      $binarydb->updateindex_rel($k || [], $v || []);
    } elsif ($w eq 'pattern') {
      $patterndb = BSDB::opendb($extrepodb, 'pattern') unless $patterndb;
      $patterndb->store($k, $v);
    } elsif ($w eq 'repoinfo') {
      if (!$repoinfodb) {
        $repoinfodb = BSDB::opendb($extrepodb, 'repoinfo');
        $repoinfodb->{'noindexatall'} = 1;
      }
      $repoinfodb->store($k, $v);
    } else {
      die("bad data type: '$w'\n");
    }
  }
  return $BSStdServer::return_ok;
}

#sub search_published_id {
#  my ($cgi, $what, $match) = @_;
#  my $res;
#  for my $rrserver ($BSConfig::reposerver) {
#    $res = BSRPC::rpc("$rrserver/search/published/$what/id", $BSXML::collection, "match=$match");
#    last if $res;
#  }
#  return ($res, $BSXML::collection);
#}
#
#sub search_published_binary_id {
#  return search_published_id($_[0], 'binary', $_[1]);
#}
#
#sub search_published_pattern_id {
#  return search_published_id($_[0], 'pattern', $_[1]);
#}

my %prp_to_repoinfo;

sub prp_to_repoinfo {
  my ($prp) = @_;

  my $repoinfo = $prp_to_repoinfo{$prp};
  if (!$repoinfo) {
    my $repoinfodb = BSDB::opendb($extrepodb, 'repoinfo');
    $repoinfo = $repoinfodb->fetch($prp);
    if ($repoinfo) {
      for (@{$repoinfo->{'prpsearchpath'} || []}) {
	next if ref($_);	# legacy
	my ($p, $r) = split('/', $_, 2);
	$_ = {'project' => $p, 'repository' => $r};
      }
    } else {
      $repoinfo = {'binaryorigins' => {}};
    }
    $prp_to_repoinfo{$prp} = $repoinfo;
  }
  return $repoinfo;
}

sub binary_key_to_data {
  my ($db, $key) = @_; 
  my @p = split('/', $key);
  my $binary = pop(@p);
  my $name = $binary;
  my $versrel = '';
  if ($name =~ s/-([^-]+-[^-]+)\.[^\.]+\.rpm$//) {
    $versrel = $1;
  } elsif ($name =~ s/_([^_]+)_[^_]+\.deb$//) {
    $versrel = $1;
  } elsif ($name =~ s/-([^-]+-[^-]+)-[^-]+\.pkg\.tar\..z$//) {
    $versrel = $1;
  }
  my ($version, $release) = ($versrel, undef);
  ($version, $release) = ($1, $2) if $version =~ /^(.*)-(.*?)$/;
  my $arch = pop(@p);
  $arch = $1 if ($arch eq 'RPMS' || $arch eq 'SRPMS') && $binary =~ /\.([^\.]+)\.rpm$/;
  while (@p > 1 && $p[0] =~ /:$/) {
    splice(@p, 0, 2, "$p[0]$p[1]");
  }
  my $project = shift(@p);
  while (@p > 1 && $p[0] =~ /:$/) {
    splice(@p, 0, 2, "$p[0]$p[1]");
  }
  my $repository = shift(@p);
  my $prp = "$project/$repository";
  my $repoinfo = $prp_to_repoinfo{$prp} || prp_to_repoinfo($prp);
  my $type;
  $type = 'rpm' if $binary =~ /\.rpm$/;
  $type = 'deb' if $binary =~ /\.deb$/;
  $type = 'arch' if $binary =~ /\.pkg\.tar\..z$/;
  my $res = {
    'name' => $name,
    'versrel' => $versrel,
    'version' => $version,
    'arch' => $arch,
    'type' => $type,
    'project' => $project,
    'repository' => $repository,
    'filename' => $binary,
    'filepath' => $key,
  };
  $res->{'release'} = $release if defined $release;
  $res->{'path'} = $repoinfo->{'prpsearchpath'} if $repoinfo->{'prpsearchpath'};
  my $location = join('/', @p, $arch, $binary);
  $res->{'package'} = $repoinfo->{'binaryorigins'}->{$location} if defined $repoinfo->{'binaryorigins'}->{$location};
  if ($repoinfo->{'base'}) {
    $res->{'baseproject'} = $repoinfo->{'base'}->{'project'};
  } elsif ($res->{'path'}) {
    $res->{'baseproject'} = $res->{'path'}->[-1]->{'project'};
  }
  return $res;
}

sub binary_key_to_project {
  my ($db, $key) = @_;  
  my @p = split('/', $key);
  while (@p > 1 && $p[0] =~ /:$/) {
    splice(@p, 0, 2, "$p[0]$p[1]");
  }
  return shift @p;
}

sub pattern_key_to_data {
  my ($db, $key) = @_; 
  my @p = split('/', $key);
  my $filename = pop(@p);
  while (@p > 1 && $p[0] =~ /:$/) {
    splice(@p, 0, 2, "$p[0]$p[1]");
  }
  my $project = shift(@p);
  while (@p > 1 && $p[0] =~ /:$/) {
    splice(@p, 0, 2, "$p[0]$p[1]");
  }
  my $repository = shift(@p);
  my @v = BSDBIndex::getvalues($db, $db->{'table'}, $key);
  return {} unless @v;
  my $res = $v[0];
  $res->{'baseproject'} = $res->{'path'}->[-1]->{'project'} if $res->{'path'};
  $res->{'project'} = $project;
  $res->{'repository'} = $repository;
  $res->{'filename'} = $filename;
  $res->{'filepath'} = $key;
  return $res;
}

sub published_projectindexfunc {
  my ($db, $path, $value) = @_;
  return findprojects() unless defined $value;
  my $proj = BSRevision::readproj_local($value);
  return () unless $proj;
  my @repoids = map {$_->{'name'}} @{$proj->{'repository'} || []};
  my %bins;
  for my $repoid (@repoids) {
    my $prp = "$value/$repoid";
    my $prp_ext = $prp;
    $prp_ext =~ s/:/:\//g;
    my $repoinfo = $prp_to_repoinfo{$prp} || prp_to_repoinfo($prp);
    for (keys %{$repoinfo->{'binaryorigins'} || {}}) {
      next unless /\//;
      # keep in sync with updatebinaryindex in bs_publish
      next unless /\.(?:rpm|deb|pkg\.tar\..z)$/;
      $bins{"$prp_ext/$_"} = 1;
    }
  }
  return sort keys %bins;
}

sub search_add_downloadurl {
  my ($data) = @_;
  for my $d (@$data) {
    my @p = split('/', $d->{'filepath'});
    my $downloadurl = BSUrlmapper::get_downloadurl("$d->{'project'}/$d->{'repository'}");
    next unless $downloadurl;
    $downloadurl =~ s/\/$//;
    $d->{'downloadurl'} = "$downloadurl/".join('/', splice(@p, -2));
  }
}

sub search_published_binary_id {
  my ($cgi, $match) = @_;
  my $binarydb = BSDB::opendb($extrepodb, 'binary');
  $binarydb->{'allkeyspath'} = 'name';
  $binarydb->{'noindex'} = {'version' => 1, 'release' => 1, 'versrel' => 1, 'arch' => 1, 'project' => 1, 'repository' => 1, 'package' => 1, 'type' => 1, 'path/project' => 1, 'path/repository' => 1, 'baseproject' => 1};
  $binarydb->{'indexfunc'} = {'project' => \&published_projectindexfunc };
  $binarydb->{'fetch'} = \&binary_key_to_data;
  $binarydb->{'fetch_project'} = \&binary_key_to_project;
  $binarydb->{'cheapfetch'} = 1;
  my $limit = defined($cgi->{'limit'}) ? $cgi->{'limit'} : 1000;
  my $rootnode = BSXPathKeys::node($binarydb, '', $limit && $limit < 10 ? 1000 : $limit * 100);
  my $data = BSXPath::match($rootnode, $match) || [];
  # epoch?
  @$data = sort {Build::Rpm::verscmp($b->{'version'}, $a->{'version'}) || $a->{'name'} cmp $b->{'name'} || $a->{'arch'} cmp $b->{'arch'}} @$data;
  delete $_->{'versrel'} for @$data;
  my $res = {};
  $res->{'matches'} = @$data;
  $res->{'limited'} = 'true' if $limit && @$data > $limit;
  splice(@$data, $limit) if $limit && @$data > $limit;
  delete $_->{'path'} for @$data;
  search_add_downloadurl($data) if $cgi->{'withdownloadurl'};
  $res->{'binary'} = $data;
  return ($res, $BSXML::collection);
}

sub search_published_pattern_id {
  my ($cgi, $match) = @_;
  my $patterndb = BSDB::opendb($extrepodb, 'pattern');
  $patterndb->{'noindex'} = {'project' => 1, 'repository' => 1};
  $patterndb->{'fetch'} = \&pattern_key_to_data;
  my $limit = defined($cgi->{'limit'}) ? $cgi->{'limit'} : 1000;
  my $rootnode = BSXPathKeys::node($patterndb, '', $limit && $limit < 10 ? 1000 : $limit * 100);
  my $data = BSXPath::match($rootnode, $match) || [];
  my $res = {};
  $res->{'matches'} = @$data;
  $res->{'limited'} = 'true' if $limit && @$data > $limit;
  splice(@$data, $limit) if $limit && @$data > $limit;
  for (@$data) {
    delete $_->{'path'};
    delete $_->{'description'};
    delete $_->{'summary'};
  }
  search_add_downloadurl($data) if $cgi->{'withdownloadurl'};
  $res->{'pattern'} = $data;
  return ($res, $BSXML::collection);
}

#############################################################################

sub search {
  my ($cgi, $in, $match) = @_;
  # gather all data
  my $data = [];
  if ($in eq 'projects') {
    for my $projid (findprojects()) {
      my $proj = BSRevision::readproj_local($projid);
      push @$data, $proj;
    }
  } elsif ($in eq 'packages') {
    for my $projid (findprojects()) {
      my @packages = BSRevision::lspackages_local($projid);
      for my $packid (@packages) {
        my ($pack, undef) = getpackage($cgi, $projid, $packid);
	$pack->{'project'} = $projid;
        push @$data, $pack;
      }
    }
  } else {
    die("'in' parameter needs to be either 'projects' or 'packages'\n");
  }
  my $res;
  if ($cgi->{'values'}) {
    $data = BSXPath::valuematch($data, $match);
    $res = {'value' => $data};
  } else {
    $data = BSXPath::match($data, $match);
    if (exists $cgi->{'return'}) {
      $data = BSXPath::valuematch($data, $cgi->{'return'});
      $res = {'value' => $data};
    } elsif ($in eq 'projects') {
      $res = {'project' => $data};
    } else {
      $res = {'package' => $data};
    }
  }
  return ($res, $BSXML::collection);
}

sub postrepo {
  my ($cgi, $projid, $repoid, $arch) = @_;
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/_repository",
    'request' => 'POST',
  };
  my $res = BSWatcher::rpc($param, $BSXML::collection, "match=$cgi->{'match'}");
  return ($res, $BSXML::collection);
}

####################################################################

sub service {
  my ($cgi, $service) = @_;
  die("404 no such service '$service'\n") unless $BSConfig::serviceserver;
  return BSWatcher::rpc("$BSConfig::serviceserver/service/$service", undef);
}

sub listservices {
  my ($cgi) = @_;
  return "<servicelist/>\n" unless $BSConfig::serviceserver;
  return BSWatcher::rpc("$BSConfig::serviceserver/service", undef);
}

####################################################################

sub published {
  my ($cgi, $projid, $repoid, $arch, $filename, $subfilename) = @_;
  my $projpack;
  die("unknown view '$cgi->{'view'}'\n") if $cgi->{'view'} && $cgi->{'view'} ne 'ymp' && $cgi->{'view'} ne 'fileinfo';
  if (defined($projid) && defined($repoid) && $cgi->{'view'} && $cgi->{'view'} eq 'ymp') {
    # attach projpack data so that the repo server does not need to
    # reconnect us
    $projpack = (getprojpack({'nopackages' => 1, 'withrepos' => 1, 'expandedrepos' => 1}, [ $projid ], [ $repoid ], undef, 'noarch'))[0];
    my $proj = $projpack->{'project'}->[0];
    die("no such project\n") unless $proj && $proj->{'name'} eq $projid;
    my $repo = $proj->{'repository'}->[0];
    die("no such repository\n") unless $repo && $repo->{'name'} eq $repoid;
    $projpack->{'project'} = [ $proj ];
    my @nprojids = grep {$_ ne $projid} map {$_->{'project'}} @{$repo->{'path'} || []};
    @nprojids = BSUtil::unify(@nprojids);
    for my $nprojid (@nprojids) {
      my $nproj = (getproject({}, $nprojid))[0];
      push @{$projpack->{'project'}}, {
	'name' => $nprojid,
	'title' => $nproj->{'title'} || '',
	'description' => $nproj->{'description'} || '',
      };
    }
  }
  my @args;
  push @args, "view=$cgi->{'view'}" if $cgi->{'view'};
  my $p = "/published";
  $p .= "/$projid" if defined $projid;
  $p .= "/$repoid" if defined $repoid;
  $p .= "/$arch" if defined $arch;
  $p .= "/$filename" if defined $filename;
  $p .= "/$subfilename" if defined $subfilename;
  if (defined($projid) || !$BSConfig::partitioning || !$BSConfig::partitionservers) {
    my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
    my $param = {
      'uri' => "$reposerver$p",
      'ignorestatus' => 1,
      'receiver' => \&BSServer::reply_receiver,
    };
    if ($projpack) {
      $param->{'request'} = 'POST';
      $param->{'data'} = BSUtil::toxml($projpack, $BSXML::projpack);
      $param->{'headers'} = [ 'Content-Type: application/octet-stream' ];
    }
    BSWatcher::rpc($param, undef, @args);
    return undef;
  }
  
  my %pubprojids;
  my @reposervers = BSSrcServer::Partition::allreposervers();
  for my $reposerver (@reposervers) {
    my $res;
    eval {
      $res = BSWatcher::rpc("$reposerver/published", $BSXML::dir, @args);
    };
    warn($@) if $@;
    next unless $res;
    $pubprojids{$_->{'name'}} = 1 for @{$res->{'entry'} || []};
  }
  my @res = sort(keys %pubprojids);
  @res = map {{'name' => $_}} @res;
  return ({'entry' => \@res}, $BSXML::dir);
}

sub published_path {
  my ($cgi, $projid, $repoid) = @_;
  my $medium = $cgi->{'medium'};
  my $prp = "$projid/$repoid";
  my $ret = {'project' => $projid, 'repository' => $repoid};
  $ret->{'medium'} = $medium if $medium;

  # update to get fresh version of repodownload
  BSConfiguration::check_configuration_once();
  my ($path, $url) = BSUrlmapper::get_path_downloadurl($prp);

  if ($cgi->{'filename'}) {
    # called from getbinary
    my $bin = $cgi->{'filename'};
    my $p;
    if ($bin =~ /^.+-[^-]+-[^-]+\.([a-zA-Z][^\/\.\-]*)\.d?rpm$/) {
      $p = "$1/$bin";
    } elsif ($bin =~ /^.+_[^_]+_([^_\.]+)\.deb$/) {
      $p = "$1/$bin";
    } elsif ($bin =~ /\.exe$/) {
      $p = $bin;
    } elsif ($bin =~ /\.(?:pkg\.tar\.gz|pkg\.tar\.xz)$/) {
      $p = ($cgi->{'arch'} eq 'i586' ? 'i686' : $cgi->{'arch'})."/$bin";
    } elsif ($bin =~ /\.iso(?:\.report)$/) {
      $p = "iso/$bin";
    } elsif ($bin =~ /-Media\d+$/) {
      $medium = $bin;
    }
    if (defined($p) && !$medium) {
      $path .= "/$p" if defined $path;
      $url .= BSRPC::urlencode($p) if defined $url;
    }
  }

  if ($medium && $medium =~ /\.iso$/) {
    $medium = "iso/$medium";
  } elsif ($medium) {
    my @path = expandsearchpath($projid, $repoid);
    my $c = concatconfigs($projid, $cgi->{'repository'}, undef, @path);
    my $bconf = Build::read_config('noarch', [ split("\n", $c) ]);
    my %repotype;
    for (@{$bconf->{'repotype'} || []}) {
      if (/^(.*?):(.*)$/) {
        $repotype{$1} = [ split(':', $2) ];
      } else {
        $repotype{$_} = [];
      }    
    }
    if ($repotype{'slepool'}) {
      my $name = $repotype{'slepool'}->[0] || 'product';
      if ($medium =~ /-Media1$/) {
	$medium = $name;
      } elsif ($medium =~ /-Media3$/) {
	$medium = "${name}_debug";
      } elsif ($medium =~ /-Media2$/) {
        my $repoinfo;
	eval {
	  $repoinfo = $prp_to_repoinfo{$prp};
	};
	my $binaryorigins = ($repoinfo || {})->{'binaryorigins'};
	$medium = $binaryorigins->{"${name}_source"} ? "${name}_source" : "${name}_debug";
      }
    } else {
      $medium = "repo/$medium";
    }
  }
  if ($medium) {
    $path .= "/$medium" if defined $path;
    $url .= BSRPC::urlencode($medium) if defined $url;
  }
  $url =~ s/([^\/])\/$/$1/ if defined $url;		# compat
  $ret->{'path'} = $path if defined $path;
  $ret->{'url'} = $url if defined $url;
  return ($ret, $BSXML::publishedpath);
}
  
####################################################################

sub autoextend_check {
  my ($projid, $pk) = @_;
  return $pk unless $pk;
  my $ex = 0;
  eval { $ex = BSPGP::pk2expire(BSPGP::unarmor($pk)) };
  if ($ex && $ex < time() + 14 * 24 * 3600) {
    extendkey({'comment' => 'auto-extend public key expiry date'}, $projid);
    $pk = readstr("$projectsdir/$projid.pkg/_pubkey", 1);
  }
  return $pk;
}

sub getsignkey {
  my ($cgi, $projid) = @_;

  while ($projid ne '') {
    my $sk = readstr("$projectsdir/$projid.pkg/_signkey", 1);
    if ($sk) {
      if ($cgi->{'withpubkey'} || $cgi->{'withalgo'}) {
        my $pk = readstr("$projectsdir/$projid.pkg/_pubkey", 1);
	$pk = autoextend_check($projid, $pk) if $cgi->{'withpubkey'} && $cgi->{'autoextend'};
	if ($cgi->{'withalgo'} && $sk !~ /^\S+:/) {
	  my $algo = '?';
	  if ($pk) {
	    eval { $algo = BSPGP::pk2algo(BSPGP::unarmor($pk)) };
	  }
	  $sk = "$algo:$sk";
	}
	if ($cgi->{'withpubkey'}) {
          $sk .= "\n" unless $sk =~ /\n$/s;
          $sk .= $pk if defined $pk;
	}
      }
      return ($sk, 'Content-Type: text/plain') if $sk;
    }
    $projid =~ s/[^:]*$//;
    $projid =~ s/:$//;
  }
  return ('', 'Content-Type: text/plain');
}

sub projid2sslcert {
  my ($projid, $origprojid, $sk, $nocreate) = @_;
  my $rev = BSRevision::getrev_meta($projid, undef);
  my $files = BSRevision::lsrev($rev);
  if (!$files->{'_sslcert'}) {
    return undef if $nocreate;
    # length(signkey) <= 2 means special handling, don't commit it
    if (!$sk || length($sk) <= 2) {
      return pubkey2sslcert($origprojid || $projid, "$projectsdir/$projid.pkg/_pubkey");
    }
    my $cert = pubkey2sslcert($projid);
    mkdir_p($uploaddir);
    writestr("$uploaddir/sslcert.$$", undef, $cert);
    BSRevision::addrev_meta_replace({'comment' => 'automatic cert creation'}, $projid, undef, [ "$uploaddir/sslcert.$$", undef, '_sslcert']);
    return $cert;
  }
  return BSRevision::revreadstr($rev, '_sslcert', $files->{'_sslcert'});
}

sub getsslcert {
  my ($cgi, $projid) = @_;

  my $origprojid = $projid;
  while ($projid ne '') {
    my $sk = readstr("$projectsdir/$projid.pkg/_signkey", 1);
    if (!$sk) {
      $projid =~ s/[^:]*$//;
      $projid =~ s/:$//;
      next;
    }
    my $pk = readstr("$projectsdir/$projid.pkg/_pubkey", 1);
    $pk = autoextend_check($projid, $pk) if $cgi->{'autoextend'};
    my $cert = projid2sslcert($projid, $origprojid, $sk);
    return ($cert, 'Content-Type: text/plain');
  }
  if ($BSConfig::sign_project && $BSConfig::sign) {
    # request default cert
    my $cert = '';
    local *F;
    open(F, '-|', $BSConfig::sign, '--project', $origprojid, '-C') || die("$BSConfig::sign: $!\n");
    1 while sysread(F, $cert, 4096, length($cert));
    close(F) || die("$BSConfig::sign: $?\n");
    return ($cert, 'Content-Type: text/plain');
  }
  return ('', 'Content-Type: text/plain');
}

sub pubkeyinfo {
  my ($pk) = @_;

  my $algo;
  my $keysize;
  my $fingerprint;
  my $expire;
  eval {
    my $pku = BSPGP::unarmor($pk);
    eval { $algo = BSPGP::pk2algo($pku) };
    eval { $keysize = BSPGP::pk2keysize($pku) };
    eval { $fingerprint = BSPGP::pk2fingerprint($pku) };
    eval { $expire = BSPGP::pk2expire($pku) };
  };
  my $pubkey = { '_content' => $pk };
  $pubkey->{'algo'} = $algo if $algo;
  $pubkey->{'keysize'} = $keysize if $keysize;
  if ($fingerprint) {
    $pubkey->{'keyid'} = substr($fingerprint, -8, 8);
    $fingerprint =~ s/(....)/$1 /g;
    $fingerprint =~ s/ $//;
    $pubkey->{'fingerprint'} = $fingerprint;
  }
  $pubkey->{'expires'} = $expire if $expire;
  return $pubkey;
}

sub getkeyinfo {
  my ($cgi, $projid) = @_;

  my $origprojid = $projid;
  my ($sk, $pk, $cert);
  while ($projid ne '') {
    $sk = readstr("$projectsdir/$projid.pkg/_signkey", 1);
    last if $sk;
    $projid =~ s/[^:]*$//;
    $projid =~ s/:$//;
  }
  if ($projid) {
    $pk = readstr("$projectsdir/$projid.pkg/_pubkey", 1);
    undef $pk if $pk && length($pk) <= 2;
    $pk = autoextend_check($projid, $pk) if $cgi->{'autoextend'};
  }
  if ($cgi->{'withsslcert'} && $projid && $pk) {
    $cert = projid2sslcert($projid, $origprojid, $sk, $cgi->{'donotcreatecert'});
  }
  if ($cgi->{'withsslcert'} && !$projid && $BSConfig::sign_project && $BSConfig::sign) {
    local *S;
    open(S, '-|', $BSConfig::sign, '--project', $origprojid, '-C') || die("$BSConfig::sign: $!\n");
    $cert = '';
    1 while sysread(S, $cert, 4096, length($cert));
    $cert = undef unless close(S);
  }
  if (!$pk && $BSConfig::sign_project && $BSConfig::sign) {
    local *S;
    open(S, '-|', $BSConfig::sign, '--project', $origprojid, '-p') || die("$BSConfig::sign: $!\n");
    $pk = '';
    1 while sysread(S, $pk, 4096, length($pk));
    $pk = undef unless close(S);
  } elsif (!$pk && $BSConfig::keyfile) {
    $pk = readstr($BSConfig::keyfile, 1);
  }
  my $keyinfo = {};
  $keyinfo->{'project'} = $projid if $projid;
  $keyinfo->{'pubkey'} = pubkeyinfo($pk) if $pk;
  $keyinfo->{'sslcert'} = $cert if $cert;
  return ($keyinfo, $BSXML::keyinfo);
}

####################################################################

# next two functions needed for migrations to 2.4
sub getlastidrequest {
  my $lastid = (readstr("$requestsdir/.nextid", 1) || 0) - 1;
  return ("$lastid\n", 'Content-Type: text/plain');
}

sub getrequest {
  my ($cgi, $id) = @_;
  local *F;
  if (!open(F, '<', "$requestsdir/$id") && !open(F, '<', "$oldrequestsdir/$id")) {
    die("404 no such request '$id'\n");
  }
  my $reqxml = '';
  1 while sysread(F, $reqxml, 8192, length($reqxml));
  close F;
  return ($reqxml, 'Content-Type: text/xml');
}

####################################################################

sub findremote {
  my ($projid) = @_;

  my $proj = BSRevision::readproj_local($projid, 1);
  $proj = BSSrcServer::Remote::remoteprojid($projid) if !$proj || $proj->{'remoteurl'};
  die("404 project '$projid' does not exist\n") unless $proj;
  if (!$proj->{'remoteurl'}) {
    $proj->{'remoteurl'} = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
    $proj->{'remoteproject'} = $projid;
    $proj->{'remoteproxy'} = undef;
  }
  return $proj;
}

sub worker_getbinaries {
  my ($cgi, $projid, $repoid, $arch) = @_;

  if (!$BSStdServer::isajax) {
    my @args;
    push @args, "project=$projid";
    push @args, "repository=$repoid";
    push @args, "arch=$arch";
    push @args, "binaries=$cgi->{'binaries'}";
    BSHandoff::handoff('/getbinaries', undef, @args);
  }
  my @binaries = split(',', $cgi->{'binaries'});
  my $proj = findremote($projid);
  my $binarylist = BSSrcServer::Remote::getremotebinarylist($proj, $projid, $repoid, $arch, \@binaries);
  return undef unless $binarylist;
  my $reply = BSSrcServer::Remote::getremotebinaries($proj, $projid, $repoid, $arch, \@binaries, $binarylist);
  return undef unless $reply;
  if ($cgi->{'raw'}) {
    die("can only transport one binary in raw mode\n") unless @$reply == 1;
    my $f = $reply->[0];
    die("$f->{'name'}: $f->{'error'}\n") if $f->{'error'};
    die("$f->{'name'}: not found\n") unless $f->{'filename'};
    BSWatcher::reply_file($f->{'filename'});
    return undef;
  }
  BSWatcher::reply_cpio($reply);
  return undef;
}

sub worker_getbinaryversions {
  my ($cgi, $projid, $repoid, $arch) = @_;

  if (!$BSStdServer::isajax) {
    my @args;
    push @args, "project=$projid";
    push @args, "repository=$repoid";
    push @args, "arch=$arch";
    push @args, "binaries=$cgi->{'binaries'}";
    push @args, "nometa=1" if $cgi->{'nometa'};
    BSHandoff::handoff('/getbinaryversions', undef, @args);
  }
  my @binaries = split(',', $cgi->{'binaries'});
  my $proj = findremote($projid);
  my $binaryversions = BSSrcServer::Remote::getremotebinaryversions($proj, $projid, $repoid, $arch, \@binaries);
  return undef unless $binaryversions;
  my $bvl = {};
  $bvl->{'binary'} = [ map {$binaryversions->{$_}} @binaries];
  return ($bvl, $BSXML::binaryversionlist);
}

####################################################################

# this is shared for AJAX requests
my @lastev_cache;
my @lastev_stat;

sub lastevents {
  my ($cgi, $filter) = @_;
  if (!$cgi->{'start'}) {
    # just fetch the current event number
    my $lastev = BSFileDB::fdb_getlast("$eventdir/lastevents", $eventlay);
    my $lastno = $lastev ? $lastev->{'number'} : 0;
    my $ret = {'next' => $lastno, 'sync' => 'lost'};
    return ($ret, $BSXML::events);
  }
  if (!$BSStdServer::isajax) {
    my @args;
    push @args, "obsname=$cgi->{'obsname'}" if $cgi->{'obsname'};
    push @args, map {"filter=$_"} @{$filter || []};
    push @args, "start=$cgi->{'start'}";
    BSHandoff::handoff('/lastevents', undef, @args);
  }
  BSWatcher::addfilewatcher("$eventdir/lastevents", 120);

  my @s = stat("$eventdir/lastevents");
  my @events;
  my ($firstno, $nextno);
  if (@s && @lastev_stat && "$s[9]/$s[7]/$s[1]" eq "$lastev_stat[9]/$lastev_stat[7]/$lastev_stat[1]") {
    @events = @lastev_cache;
  } else {
    my $lastev = BSFileDB::fdb_getlast("$eventdir/lastevents", $eventlay);
    push @events, $lastev if $lastev;
    @lastev_cache = @events;
    @lastev_stat = @s;
  }
  $firstno = @events ? $events[0]->{'number'} : 0;
  $nextno = @events ? $events[-1]->{'number'} + 1 : 1;

  if ($cgi->{'start'} < $firstno) {
    # get last 5
    @events = BSFileDB::fdb_getall_reverse("$eventdir/lastevents", $eventlay, 5);
    @events = reverse @events;
    @lastev_cache = @events;
    @lastev_stat = @s;
    $firstno = @events ? $events[0]->{'number'} : 0;
    $nextno = @events ? $events[-1]->{'number'} + 1 : 1;
  }

  if ($cgi->{'start'} < $firstno) {
    my $cnt = $nextno - $cgi->{'start'};
    if ($cnt > 5) {
      @events = BSFileDB::fdb_getall_reverse("$eventdir/lastevents", $eventlay, $cnt);
      @events = reverse @events;
      if (@events < 20) {
        @lastev_cache = @events;
        @lastev_stat = @s;
      }
      $firstno = @events ? $events[0]->{'number'} : 0;
      $nextno = @events ? $events[-1]->{'number'} + 1 : 1;
    }
  }

  if ($cgi->{'start'} < $firstno) {
    # we have to get them all
    @events = BSFileDB::fdb_getall("$eventdir/lastevents", $eventlay);
    # re-calculate in case something has changed
    $firstno = @events ? $events[0]->{'number'} : 0;
    $nextno = @events ? $events[-1]->{'number'} + 1 : 1;
    if ($firstno > $cgi->{'start'}) {
      # out of sync!
      return ({'next' => $nextno, 'sync' => 'lost'}, $BSXML::events);
    }
  }

  # filter
  @events = grep {$_->{'number'} >= $cgi->{'start'}} @events;
  if ($filter && @events) {
    my %filter = map {$_ => 1} @$filter;
    for my $ev (splice @events) {
      if ($ev->{'type'} eq 'package') {
        next unless defined $ev->{'package'};
        next unless $filter{"package/$ev->{'project'}/$ev->{'package'}"} || $filter{"package/$ev->{'project'}"};
      } elsif ($ev->{'type'} eq 'project') {
        next unless $filter{"project/$ev->{'project'}"};
      } elsif ($ev->{'type'} eq 'repository' || $ev->{'type'} eq 'repoinfo') {
        next unless $filter{"repository/$ev->{'project'}/$ev->{'repository'}/$ev->{'arch'}"} || $filter{"repository/$ev->{'project'}/$ev->{'repository'}"};
      } else {
	next;
      }
      push @events, $ev;
    }
  }
  # return a sync reply every 100 events / 5 minutes for two reasons
  # - get rid of old peers
  # - survive history truncation
  $cgi->{'start_orig'} ||= $cgi->{'start'};
  $cgi->{'req_time'} ||= time();
  if ($BSStdServer::isajax && !@events && $nextno < $cgi->{'start_orig'} + 100 && time() < $cgi->{'req_time'} + 300) {
    # small hack: update cgi to the next event number
    $cgi->{'start'} = $nextno if $cgi->{'start'} < $nextno;
    return undef;
  }
  for (@events) {
    $_ = { %$_ };	# clone em
    # delete unwanted fields
    delete $_->{'time'};
    delete $_->{'number'};
    # clean up a bit
    delete $_->{'package'} unless defined($_->{'package'}) && $_->{'package'} ne '';
  }
  my $ret = {'next' => $nextno};
  $ret->{'event'} = \@events if @events;
  return ($ret, $BSXML::events);
}

#
# add an event to the "lastevents" queue used in the build service
# interconnect implementation
#
sub addevent {
  my ($ev) = @_;

  # check the "access" flag. if the project has access turned
  # off, do not add it to lastevents.
  # XXX: maybe better to add a "noaccess" marker to the event
  # and filter in the request
  if (defined($ev->{'project'})) {
    my $access = 1;
    my $proj = BSRevision::readproj_local($ev->{'project'}, 1);
    if ($proj && $proj->{'access'}) {
      $access = BSUtil::enabled('', $proj->{'access'}, $access, '');
    }
    # XXX: may also check packages in the future
    return unless $access;
  }
  $ev->{'time'} = time();
  mkdir_p($eventdir);
  my $size = 262144;	#keep at least 256k of data
  if (-s "$eventdir/lastevents" && -s _ >= $size * 2) {
    local *F;
    BSUtil::lockopen(\*F, '+>>', "$eventdir/lastevents");
    my $events = readstr("$eventdir/lastevents");
    if (length($events) >= $size * 2) {
      $events = substr($events, -$size);
      $events =~ s/^[^\n]*\n//s;
      writestr("$eventdir/.lastevents", "$eventdir/lastevents", $events);
    }
    close F;
  }
  BSFileDB::fdb_add_i("$eventdir/lastevents", $eventlay, $ev);
}

sub newevent {
  my ($cgi) = @_;
  my $ev = {};
  for ('type', 'project', 'package', 'repository', 'arch', 'job') {
    $ev->{$_} = $cgi->{$_} if defined $cgi->{$_};
  }
  addevent($ev);
  return $BSStdServer::return_ok;
}

####################################################################

sub getrelsync {
  my ($cgi, $projid, $repoid, $arch) = @_;
  checkprojrepoarch($projid, $repoid, $arch);
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/_relsync",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
  };
  BSWatcher::rpc($param, undef);
  return undef;
}

sub postrelsync {
  my ($cgi, $projid, $repoid, $arch) = @_;

  my $proj = checkprojrepoarch($projid, $repoid, $arch);
  my $repo = (grep {$_->{'name'} eq $repoid} @{$proj->{'repository'} || []})[0];
  my $relsyncdata = BSServer::read_data(10000000);
  for my $a (@{$repo->{'arch'} || []}) {
    next if $a eq $arch;
    next if $BSConfig::relsync_pool && ($BSConfig::relsync_pool->{$arch} || '') ne ($BSConfig::relsync_pool->{$a} || '');
    my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
    my $param = {
      'uri' => "$reposerver/build/$projid/$repoid/$a/_relsync",
      'request' => 'POST',
      'data' => $relsyncdata,
    };
    eval {
      BSRPC::rpc($param);
    };
    if ($@) {
      warn($@);
    }
  }
  return $BSStdServer::return_ok;
}

####################################################################

# XXX: support multiple dispatchers
sub putdispatchprios {
  my ($cgi) = @_;
  my $dispatcher = $BSConfig::masterdispatcher || $BSConfig::reposerver;
  my $param = {
    'uri' => "$dispatcher/build/_dispatchprios",
    'request' => 'PUT',
    'data' => \&BSServer::forward_sender,
    'chunked' => 1,
  };
  return BSWatcher::rpc($param, undef);
}

sub getdispatchprios {
  my ($cgi) = @_;
  my $dispatcher = $BSConfig::masterdispatcher || $BSConfig::reposerver;
  my $param = {
    'uri' => "$dispatcher/build/_dispatchprios",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
  };
  BSWatcher::rpc($param, undef);
  return undef;
}

####################################################################

sub sourceinfo {
  my ($cgi, $projid, $packid, $bconf) = @_;
  my $r = {'package' => $packid};
  my $linked = [];
  my $rev;
  my $files;
  eval {
    $rev = getrev($projid, $packid, defined($cgi->{'rev'}) ? $cgi->{'rev'} : 'build', $linked);
    $r->{'srcmd5'} = $rev->{'srcmd5'} if $rev->{'srcmd5'} ne 'empty';
    $r->{'rev'} = $rev->{'rev'} if $rev->{'rev'};
    $r->{'vrev'} = $rev->{'vrev'} if $rev->{'vrev'};
    if (!$rev || $rev->{'srcmd5'} eq 'empty' || $rev->{'srcmd5'} eq $BSSrcrep::emptysrcmd5) {
      die("no source uploaded\n") unless $cgi->{'nofilename'};
      $rev = {'srcmd5' => $BSSrcrep::emptysrcmd5, 'project' => $projid, 'package' => $packid };
    }
    my $linkinfo = {};
    $files = BSRevision::lsrev($rev, $linkinfo);
    if ($linkinfo->{'xservicemd5'}) {
      $files = BSSrcServer::Service::handleservice($rev, $files, $linkinfo->{'xservicemd5'});
      $r->{'srcmd5'} = $rev->{'srcmd5'};
    }
    my $meta = '';
    $meta .= "$files->{$_}  $_\n" for sort keys %$files;
    $r->{'verifymd5'} = Digest::MD5::md5_hex($meta);
    die("source update running\n") if $files->{'_service'} && -e "$eventdir/service/${projid}::$packid";
    die("source update failed\n") if $files->{'_service_error'};
  };
  $r->{'originproject'} = $rev->{'originproject'} if $rev && $rev->{'originproject'};
  $r->{'originpackage'} = $rev->{'package'} if $rev && $rev->{'originpackage'};
  $r->{'linked'} = $linked if @$linked;
  if ($@) {
    $r->{'error'} = $@;
    $r->{'error'} =~ s/\n$//s;
    return $r;
  }
  if ($files->{'_link'}) {
    $rev->{'linkrev'} = $cgi->{'linkrev'} if $cgi->{'linkrev'};
    eval {
      $files = BSSrcServer::Link::handlelinks($rev, $files, {'linked' => $linked});
    };
    if ($@) {
      $files = "$@";
      $files =~ s/\n$//;
    }
    $r->{'linked'} = $linked if @$linked;
    $r->{'vrev'} = $rev->{'vrev'} if $rev->{'vrev'};
    if (!ref $files) {
      $r->{'error'} = $files || 'could not get file list';
      return $r;
    }
    $r->{'lsrcmd5'} = $r->{'srcmd5'};
    $r->{'srcmd5'} = $rev->{'srcmd5'};
    my $meta = '';
    $meta .= "$files->{$_}  $_\n" for sort keys %$files;
    $r->{'verifymd5'} = Digest::MD5::md5_hex($meta);
  }
  if ($cgi->{'withchangesmd5'}) {
    $r->{'revtime'} = $rev->{'time'};
    $r->{'changesmd5'} = $files->{"$packid.changes"} if $files->{"$packid.changes"};
  }
  if (!$cgi->{'rev'} && !$cgi->{'linkrev'}) {
    BSSrcServer::Multibuild::updatemultibuild($projid, $packid, $files);
    if ($rev->{'originpackage'}) {
      my $mb = BSSrcServer::Multibuild::getmultibuild($projid, $rev->{'package'}) || {};
      if (!grep {"$rev->{'package'}:$_" eq $packid} @{$mb->{'flavor'} || $mb->{'package'} || []}) {
        $r->{'error'} = "package '$packid' does not exist";
        return $r;
      }
    }
  }
  return $r if $cgi->{'nofilename'};
  return $r if $packid eq '_pattern';
  if ($files->{'_aggregate'}) {
    $r->{'filename'} = '_aggregate';
    return $r;
  } elsif ($files->{'_patchinfo'}) {
    $r->{'filename'} = '_patchinfo';
    return $r;
  }
  my $type = $bconf->{'type'};
  my $file;
  if (!$type || $type eq 'UNDEFINED') {
    undef $type;
    for my $t ('spec', 'dsc', 'kiwi') {
      $file = findfile($rev, $cgi->{'repository'}, $t, $files);
      next unless defined $file;
      $type = $t;
      last;
    }
  } else {
    $file = findfile($rev, $cgi->{'repository'}, $type, $files);
  }
  if (!$type) {
    $r->{'error'} = 'bad build configuration, no build type defined or detected';
    return $r;
  }
  if (!$file) {
    $r->{'error'} = "no file found for build type '$type'";
    return $r;
  }
  $r->{'filename'} = $file;
  return $r unless $cgi->{'parse'};
  my $buildtype = Build::recipe2buildtype($file);
  if (!$buildtype) {
    $r->{'error'} = "don't know how to build $file";
    return $r;
  }
  local $bconf->{'obspackage'} = $packid;
  local $bconf->{'buildflavor'};
  if ($packid =~ /(?<!^_product)(?<!^_patchinfo):./) {
    $packid =~ /^(.*):(.*?)$/;
    $bconf->{'obspackage'} = $1;
    $bconf->{'buildflavor'} = $2;
  }
  
  my $d;
  eval {
    $d = Build::parse_typed($bconf, BSRevision::revfilename($rev, $file, $files->{$file}), $buildtype);
  };
  if ($@) {
    $r->{'error'} = "parse error: $@";
    $r->{'error'} =~ s/\n.*//s;
    return $r;
  }
  if (!$d) {
    $r->{'error'} = 'parse error';
    return $r;
  }
  for (qw{name version release subpacks deps prereqs exclarch badarch}) {
    $r->{$_} = $d->{$_} if defined $d->{$_};
  }
  return $r;
}

sub getprojectsourceinfo {
  my ($cgi, $projid) = @_;
  my $proj = checkprojrepoarch($projid, $cgi->{'repository'}, $cgi->{'arch'}, 1);
  my @packages = @{$cgi->{'package'} || []};
  BSSrcServer::Projlink::enable_frozenlinks_cache();
  @packages = findpackages($projid, $proj) unless @packages;
  my $bconf;
  if (!$cgi->{'nofilename'}) {
    if (!$cgi->{'repository'}) {
      my $cfile;
      $cfile = "$projectsdir/$projid.conf" if -e "$projectsdir/$projid.conf";
      $bconf = Build::read_config($cgi->{'arch'} || 'noarch', $cfile);
    } else {
      my @path = expandsearchpath($projid, $cgi->{'repository'});
      my $c = concatconfigs($projid, $cgi->{'repository'}, undef, @path);
      $bconf = Build::read_config($cgi->{'arch'} || 'noarch', [ split("\n", $c) ]);
    }
  }
  my @res;
  if (@packages > 1) {
    $BSSrcServer::Remote::collect_remote_getrev = 1;
    for my $packid (splice @packages) {
      my $r = sourceinfo($cgi, $projid, $packid, $bconf);
      if ($r->{'error'} && $r->{'error'} =~ /collect_remote_getrev$/) {
	push @packages, $packid;
	next;
      }
      push @res, $r;
    }
    $BSSrcServer::Remote::collect_remote_getrev = 0;
    BSSrcServer::Remote::fill_remote_getrev_cache();
  }
  for my $packid (@packages) {
    push @res, sourceinfo($cgi, $projid, $packid, $bconf);
  }
  BSSrcServer::Projlink::disable_frozenlinks_cache();
  return ({'sourceinfo' => \@res}, $BSXML::sourceinfolist);
}

sub getpackagesourceinfo {
  my ($cgi, $projid, $packid) = @_;
  checkprojrepoarch($projid, $cgi->{'repository'}, $cgi->{'arch'}, 1); #remoteok
  my $bconf;
  if (!$cgi->{'nofilename'}) {
    my $cfile;
    if (!$cgi->{'repository'}) {
      $cfile = "$projectsdir/$projid.conf" if -e "$projectsdir/$projid.conf";
    } else {
      print "expandsearchpath $projid $cgi->{'repository'}...\n";
      my @path = expandsearchpath($projid, $cgi->{'repository'});
      my $c = concatconfigs($projid, $cgi->{'repository'}, undef, @path);
      $cfile = [ split("\n", $c) ];
    }
    $bconf = Build::read_config($cgi->{'arch'} || 'noarch', $cfile);
  }
  my $res = sourceinfo($cgi, $projid, $packid, $bconf);
  return ($res, $BSXML::sourceinfo);
}

####################################################################

sub putconfiguration {
  my ($cgi) = @_;
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$");
  die("upload failed\n") unless $uploaded;
  my $configuration = readxml("$uploaddir/$$", $BSXML::configuration);
  unlink("$uploaddir/$$");
  my $configurationxml = BSUtil::toxml($configuration, $BSXML::configuration);
  writestr("$BSConfig::bsdir/.configuration.xml", "$BSConfig::bsdir/configuration.xml", $configurationxml);

  # distribute to repo servers
  my @servers = BSSrcServer::Partition::allreposervers();
  push @servers, $BSConfig::serviceserver if $BSConfig::serviceserver;
  for my $server (@servers) {
    my $param = {
      'uri' => "$server/configuration",
      'request' => 'PUT',
      'data' => $configurationxml,
    };
    eval {
      BSRPC::rpc($param, undef);
    };
    warn($@) if $@;	# XXX: what now?
  }
  return $BSStdServer::return_ok;
}

sub getconfiguration {
  my $configuration = readxml("$BSConfig::bsdir/configuration.xml", $BSXML::configuration, 1) || {};
  return ($configuration, $BSXML::configuration);
}

####################################################################

sub putissuetrackers {
  my ($cgi) = @_;
  mkdir_p($uploaddir);
  my $uploaded = BSServer::read_file("$uploaddir/$$");
  die("upload failed\n") unless $uploaded;
  my $trackers = readxml("$uploaddir/$$", $BSXML::issue_trackers);
  unlink("$uploaddir/$$");
  writexml("$BSConfig::bsdir/.issuetrackers.xml", "$BSConfig::bsdir/issuetrackers.xml", $trackers, $BSXML::issue_trackers);
  return $BSStdServer::return_ok;
}

sub getissuetrackers {
  my $trackers = readxml("$BSConfig::bsdir/issuetrackers.xml", $BSXML::issue_trackers, 1) || {};
  return ($trackers, $BSXML::issue_trackers);
}

####################################################################

sub orderkiwirepos_for_prio {
  my (@repos) = @_;
  my @prps;
  my %prps;
  for my $repo (@repos) {
    my $prp;
    my $url = ($repo->{'source'} || {})->{'path'};
    if ($url =~ /^obs:\/\/\/?([^\/]+)\/([^\/]+)\/?$/) {
      $prp = "$1/$2";
    } else {
      $prp = BSUrlmapper::urlmapper($url);
    }
    $prp ||= ':unknown/unknown';
    push @prps, $prp;
    push @{$prps{$prp}}, $repo;
  }
  @prps = BSUtil::unify(@prps);
  my %deps;
  for my $prp (@prps) {
    my ($projid, $repoid) = split('/', $prp, 2);
    my @path;
    eval {
      @path = expandsearchpath($projid, $repoid);
    };
    if ($@) {
      die($@) unless $@ =~ /^404/;
      warn($@);
    }
    my $oldrprp;
    for my $rprp (@path) {
      unshift @{$deps{$rprp}}, $oldrprp if $oldrprp && $rprp ne $oldrprp;
      $oldrprp = $rprp;
      if (!$prps{$rprp}) {
	$prps{$rprp} = [];
        push @prps, $rprp;
      }
    }
  }
  $deps{$_} = [ BSUtil::unify(@{$deps{$_}}) ]  for keys %deps;
  # hmm, depsort is not really stable...
  my @cycs;
  @prps = BSSolv::depsort(\%deps, undef, \@cycs, @prps);
  print "orderkiwirepo_for_prio cycle: ".join(' -> ', @$_)."\n" for @cycs;
  return map {@{$prps{$_}}} @prps;
}

sub orderkiwirepos {
  my ($cgi) = @_;
  mkdir_p($uploaddir);
  die("content read failed\n") unless BSServer::read_file("$uploaddir/$$");
  my $kiwi = readxml("$uploaddir/$$", $BSKiwiXML::kiwidesc);
  unlink("$uploaddir/$$");
  my %prios;
  my $pkgmanager;
  $pkgmanager = $kiwi->{'preferences'}->{'packagemanager'} if $kiwi->{'preferences'};
  $pkgmanager ||= 'zypper';
  for (@{$kiwi->{'repository'} || []}) {
    my $prio = $_->{'priority'};
    $prio = $pkgmanager eq 'smart' ? 0 : 99 unless defined $prio;
    $prio = 0 + $prio;
    $prio = -$prio if $pkgmanager eq 'smart';
    push @{$prios{$prio}}, $_;
  }
  if (%prios) {
    $kiwi->{'repository'} = [];
    for my $prio (sort {$a <=> $b} keys %prios) {
      push @{$kiwi->{'repository'}}, orderkiwirepos_for_prio(@{$prios{$prio}});
    }
  }
  return ($kiwi, $BSKiwiXML::kiwidesc);
}

####################################################################

sub external_notification {
  my ($cgi, $type) = @_;
  my $param = {};
  for (keys %$cgi) {
    $param->{$_} = $cgi->{$_} unless $_ eq '_type' || /^\./;
  }
  notify($type, $param);
  return $BSStdServer::return_ok;
}

sub notify_plugins {
  my ($cgi, $type) = @_;
  my $param = {};
  if (BSServer::have_content()) {
    my $jsonbody = BSServer::read_data(10000000);
    $param = JSON::XS::decode_json($jsonbody);
  } else {
    for (keys %$cgi) {
      $param->{$_} = $cgi->{$_} unless $_ eq '_type' || /^\./;
    }
  }

  BSNotify::notify_plugins($type, $param);
  return $BSStdServer::return_ok;
}


####################################################################

sub listjobs {
  my ($cgi, $arch) = @_;
  die("not masterdispatching\n") unless $BSConfig::masterdispatcher;
  my $dir = BSRPC::rpc({
    'uri'     => "$BSConfig::masterdispatcher/jobs/$arch",
    'timeout' => 60,
  }, $BSXML::dir);
  my @jobs = sort(map {$_->{'name'}} @{$dir->{'entry'} || []});
  if ($cgi->{'partition'}) {
    my %good = map {$_ => 1} @{$cgi->{'partition'}};
    my %projid2partition;
    for my $job (splice @jobs) {
      my $jn = $job;
      $jn =~ s/-[0-9a-f]{32}$//s;
      my ($projid, $repoid, $packid) = split('::', $jn);
      next unless defined $repoid;
      my $part = $projid2partition{$projid};
      $part = $projid2partition{$projid} = BSSrcServer::Partition::projid2partition($projid) unless defined $part;
      push @jobs, $job if $good{$part};
    }
  }
  @jobs = map {{'name' => $_}} @jobs;
  return return ({'entry' => \@jobs}, $BSXML::dir);
}

####################################################################

sub hello {
  my ($cgi) = @_;
  return "<hello name=\"Source Repository Ajax Server\" repoid=\"$datarepoid\" />\n" if $BSStdServer::isajax;
  return "<hello name=\"Source Repository Server\" repoid=\"$datarepoid\" />\n";
}

####################################################################

sub getworkercap {
  my ($cgi, $workerid) = @_;
  my $reposerver = $BSConfig::masterdispatcher || $BSConfig::reposerver; # no need to look for partitioning here
  my $param = {
    'uri' => "$reposerver/worker/$workerid",
    'timeout' => 60,
  };

  my $worker_cap = BSWatcher::rpc($param, $BSXML::worker);

  return($worker_cap, $BSXML::worker);
}

sub checkconstraints {
  my ($cgi) = @_;
  checkprojrepoarch($cgi->{'project'}, $cgi->{'repository'}, $cgi->{'arch'});

  my $reposerver = $BSConfig::masterdispatcher;
  $reposerver ||= $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($cgi->{'project'}) : $BSConfig::reposerver;

  my @args = BSRPC::args($cgi, 'project', 'repository', 'arch', 'package');

  my $param = {
    'uri' => "$reposerver/worker",
    'request' => 'POST',
  };
  if (BSServer::have_content()) {
    $param->{'data'} = \&BSServer::forward_sender;
    $param->{'chunked'} = 1;
  }

  my $ret = BSWatcher::rpc($param, $BSXML::dir, "cmd=checkconstraints", @args);
  return ($ret, $BSXML::dir);
}

sub getavailable {
  my ($in, $available) = @_;
  for my $i (@{$in || []}) {
    for my $arch (@{$i->{'arch'} || []}) {
      $available->{$_}->{$arch} = 1 for @{$i->{'name'} || []};
    }
  }
}

sub processavailable {
  my ($available) = @_;
  my %archlist;
  my @res;
  for my $bin (sort keys %$available) {
    my $archlist = join(',', sort keys %{$available->{$bin}});
    $archlist{$archlist}->{$bin} = 1;
  }
  for my $archlist (sort keys %archlist) {
    my @archs = split(',', $archlist);
    push @res, {'arch' => \@archs, 'name' => [ sort keys %{$archlist{$archlist}} ]};
  }
  return \@res;
}

sub getavailablebinaries {
  my ($cgi, $projid) = @_;
  my @path = @{$cgi->{'path'} || []};
  for my $url (@{$cgi->{'url'} || []}) {
    if ($url =~ /^obs:\/\/\/?([^\/]+)\/([^\/]+)\/?$/) {
      push @path, "$1/$2";
    } else {
      my $prp = BSUrlmapper::urlmapper($url);
      push @path, $prp if $prp;
    }
  }
  my @prpa;
  my @arch = @{$cgi->{'arch'} || []};
  my %archfilter = map {$_ => 1} @arch;
  # hack ahead
  my $proj = BSRevision::readproj_local($projid, 1) || {};
  for my $repo (@{$proj->{'repository'} || []}) {
    my @repoarch = @{$repo->{'arch'} || []};
    next unless @repoarch;
    if (@{$proj->{'repository'} || []} > 1) {
      next unless $repo->{'name'} && $repo->{'name'} =~ /^images/;
    }
    push @arch, @repoarch unless %archfilter;
    if (!@path) {
      for my $prp (expandsearchpath($projid, $repo->{'name'})) {
        push @prpa, map {"$prp/$_"} @repoarch;
      }
    }
  }
  if (@path) {
    @arch = BSUtil::unify(@arch);
    for my $prp (@path) {
      push @prpa, map {"$prp/$_"} @arch;
    }
  }
  @prpa = BSUtil::unify(@prpa);

  # now partition the prpas
  my %projid2reposerver;
  my %prpas_by_reposerver;
  my %remoteprojects;
  for my $prpa (@prpa) {
    my ($aprojid, $arepoid, $aarch) = split('/', $prpa, 3);
    my $reposerver = $projid2reposerver{$aprojid};
    if (!$reposerver) {
      # check if it's a remote repo
      my $proj = BSRevision::readproj_local($aprojid, 1);
      $proj = BSSrcServer::Remote::remoteprojid($aprojid) if !$proj || $proj->{'remoteurl'};
      if ($proj->{'remoteurl'}) {
	$reposerver = "remote://$proj->{'remoteurl'}";
	$remoteprojects{$aprojid} = $proj;
      } else {
        $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($aprojid) : $BSConfig::reposerver;
      }
      $projid2reposerver{$aprojid} = $reposerver;
    }
    push @{$prpas_by_reposerver{$reposerver}}, $prpa;
  }

  # go get em
  my (%available, %available_pattern, %available_product);
  for my $reposerver (sort keys %prpas_by_reposerver) {
    my @args;
    push @args, map {"prpa=$_"} @{$prpas_by_reposerver{$reposerver}};
    push @args, "cmd=availablebinaries";
    my $param = {
      'uri' => "$reposerver/_command",
      'timeout' => 30,
      'request' => 'POST',
    };
    if ($reposerver =~ /^remote:\/\//) {
      my $firstproj;
      my @remotepath;
      my @remotearch;
      for my $prpa (@{$prpas_by_reposerver{$reposerver}}) {
	my ($aprojid, $arepoid, $aarch) = split('/', $prpa, 3);
	my $proj = $remoteprojects{$aprojid};
	next unless $proj;
	$firstproj ||= $proj;
	push @remotepath, "$proj->{'remoteproject'}/$arepoid";
	push @remotearch, $aarch;
      }
      next unless $firstproj && @remotepath && @remotearch;
      $param = {
	'uri' => "$firstproj->{'remoteurl'}/build/$firstproj->{'remoteproject'}/_availablebinaries",
	'timeout' => 30,
	'request' => 'GET',
        'proxy' => $firstproj->{'remoteproxy'},
      };
      @args = ();
      push @args, map {"path=$_"} BSUtil::unify(@remotepath);
      push @args, map {"arch=$_"} BSUtil::unify(@remotearch);
    }
    my $res;
    eval {
      $res = BSWatcher::rpc($param, $BSXML::availablebinaries, @args);
    };
    warn($@) if $@;
    next unless $res;
    #return ($res, $BSXML::availablebinaries) if keys(%prpas_by_reposerver) == 1;
    getavailable($res->{'packages'}, \%available) if $res->{'packages'};
    getavailable($res->{'patterns'}, \%available_pattern) if $res->{'patterns'};
    getavailable($res->{'products'}, \%available_product) if $res->{'products'};
  }
  my %res;
  $res{'packages'} = processavailable(\%available) if %available;
  $res{'patterns'} = processavailable(\%available_pattern) if %available_pattern;
  $res{'products'} = processavailable(\%available_product) if %available_product;
  return (\%res, $BSXML::availablebinaries);
}

####################################################################


sub cloudupload_create_receiver {
  my ($req, $param) = @_;
  my $hdr = $req->{'headers'};
  die("cannot do chunked uploads\n") if $hdr->{'transfer-encoding'} && lc($hdr->{'transfer-encoding'}) eq 'chunked';
  my $cl = $hdr->{'content-length'};
  die("need a content length\n") unless defined $cl;
  die("upload image is empty\n") unless $cl;

  local *S = $req->{'__socket'};	# XXX: fix BSRPC
  $req->{'__socket'} = \*S;

  # create a job
  $param->{'uploadjob'}->{'size'} = $cl;	# we now know the size
  my $createparam = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload",
    'request' => 'POST',
    'timeout' => 60,
    'data' => BSUtil::toxml($param->{'uploadjob'}, $BSXML::clouduploadjob),
  };
  my $job = BSWatcher::rpc($createparam, $BSXML::clouduploadjob);

  # reply the job right away
  BSStdServer::stdreply($job, $BSXML::clouduploadjob);
  BSServer::done(1);

  # do the upload in the background
  my $uploadparam = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload/$job->{'name'}",
    'request' => 'PUT',
    'timeout' => 600,
    'headers' => [ "Content-Length: $cl" ],
    'data' => \&BSHTTP::reply_sender,
    'reply_req' => $req,
  };
  BSWatcher::rpc($uploadparam);

  # nothing else to reply
  return undef;
}

sub cloudupload_create {
  my ($cgi, $projid, $repoid, $arch, $packid, $filename) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  my $targetdata;
  $targetdata = BSServer::read_data(10000000) if BSServer::have_content();
  $targetdata = '' unless defined $targetdata;
  my $reposerver = $BSConfig::partitioning ? BSSrcServer::Partition::projid2reposerver($projid) : $BSConfig::reposerver;
  my $uploadjob = {
    'project' => $projid,
    'repository' => $repoid,
    'arch' => $arch,
    'package' => $packid,
    'filename' => $filename,
    'user' => $cgi->{'user'},
    'target' => $cgi->{'target'},
    'details' => unpack('H*', $targetdata),
  };
  my $param = {
    'uri' => "$reposerver/build/$projid/$repoid/$arch/$packid/$filename",
    'receiver' => \&cloudupload_create_receiver,
    'timeout' => 600,
    'uploadjob' => $uploadjob,
  };
  return BSWatcher::rpc($param, undef);
}

sub cloudupload_status {
  my ($cgi, $jobid) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  my $param = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload/$jobid",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
  };
  BSWatcher::rpc($param, undef);
  return undef;
}

sub cloudupload_pubkey {
  my ($cgi) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  return cloudupload_status($cgi, '_pubkey') unless $cgi->{'view'};
  die("unsupported view '$cgi->{'view'}'\n") if $cgi->{'view'} ne 'info';
  my $pk = BSWatcher::rpc("$BSConfig::clouduploadserver/cloudupload/_pubkey");
  die("no pubkey configured\n") unless $pk;
  my $pubkey = pubkeyinfo($pk);
  return ($pubkey, $BSXML::pubkeyinfo);
}

sub cloudupload_kill {
  my ($cgi, $jobid) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  my $param = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload/$jobid",
    'request' => 'POST',
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
  };
  BSWatcher::rpc($param, undef, 'cmd=kill');
  return undef;
}

sub cloudupload_joblist {
  my ($cgi) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  my $param = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload",
  };
  return BSRPC::rpc($param, undef, BSRPC::args($cgi, 'name'));
}

sub cloudupload_log {
  my ($cgi, $jobid) = @_;
  die("no cloud upload server configurated\n") unless $BSConfig::clouduploadserver;
  my @args = BSRPC::args($cgi, 'nostream', 'start', 'end', 'view');
  if (!$BSStdServer::isajax && !$cgi->{'nostream'} && !$cgi->{'view'}) {
    BSHandoff::handoff("/cloudupload/$jobid/_log", undef, @args);
  }
  my $param = {
    'uri' => "$BSConfig::clouduploadserver/cloudupload/$jobid/_log",
    'ignorestatus' => 1,
    'receiver' => \&BSServer::reply_receiver,
    'joinable' => 1,
  };
  BSWatcher::rpc($param, undef, @args);
  return undef; # always streams result
}

####################################################################

my $dispatches = [
  '/' => \&hello,

  '!rw :' => undef,
  '!- GET:' => undef,
  '!- HEAD:' => undef,

  'POST:/source cmd=orderkiwirepos' => \&orderkiwirepos,
  'POST:/source cmd: *:*' => \&unknowncmd,

  # /source name space: manage project and package data
  '/source deleted:bool?' => \&getprojectlist,

  'POST:/source/$project cmd=createkey user:? comment:?' => \&createkey,
  'POST:/source/$project cmd=extendkey user:? comment:?' => \&extendkey,
  'POST:/source/$project cmd=undelete user:? comment:?' => \&undeleteproject,
  'POST:/source/$project cmd=copy user:? comment:? oproject:project withbinaries:bool? withhistory:bool? makeolder:bool? makeoriginolder:bool? resign:bool? noservice:bool?' => \&copyproject,
  'POST:/source/$project cmd=move oproject:project' => \&moveproject,
  'POST:/source/$project cmd=freezelink user:? comment:? requestid:num?' => \&freezeprojectlink,
  'POST:/source/$project cmd=notifypackagechange' => \&notifypackagechange,
  'POST:/source/$project cmd: *:*' => \&unknowncmd,
  '/source/$project view=info parse:bool? nofilename:bool? repository? arch? package* withchangesmd5:bool?' => \&getprojectsourceinfo,
  '/source/$project deleted:bool? expand:bool? noorigins:bool?' => \&getpackagelist,

  'DELETE:/source/$project user:? comment:? requestid:num?' => \&delproject,
  '/source/$project/_meta rev?' => \&getproject,
  'PUT:/source/$project/_meta user:? comment:? requestid:num? lowprio:bool?' => \&putproject,

  '/source/$project/_pubkey rev?' => \&getpubkey,
  'DELETE:/source/$project/_pubkey user:? comment:?' => \&deletekey,

  '/source/$project/_config rev?' => \&getprojectconfig,
  'PUT:/source/$project/_config user:? comment:?' => \&putprojectconfig,
  'DELETE:/source/$project/_config user:? comment:?' => \&delprojectconfig,

  '/source/$project/_history rev? meta:bool? deleted:bool? limit:num?' => \&getpackagehistory,
  '/source/$project/_keyinfo withsslcert:bool? autoextend:bool? donotcreatecert:bool?' => \&getkeyinfo,

  'POST:/source/$project/$package cmd=diff rev? orev:rev? oproject:project? opackage:package? expand:bool? linkrev? olinkrev:? unified:bool? missingok:bool? meta:bool? file:filename* filelimit:num? tarlimit:num? view:? withissues:bool? onlyissues:bool?' => \&sourcediff,
  'POST:/source/$project/$package cmd=linkdiff rev? linkrev? unified:bool? file:filename* filelimit:num? tarlimit:num? view:? withissues:bool? onlyissues:bool?' => \&linkdiff,
  'POST:/source/$project/$package cmd=servicediff rev? unified:bool? file:filename* filelimit:num? tarlimit:num? view:? withissues:bool? onlyissues:bool?' => \&servicediff,
  'POST:/source/$project/$package cmd=commit rev? user:? comment:? keeplink:bool? repairlink:bool? linkrev? setrev:bool? requestid:num? noservice:bool?' => \&sourcecommit,
  'POST:/source/$project/$package cmd=commitfilelist rev? user:? comment:? keeplink:bool? repairlink:bool? linkrev? setrev:bool? requestid:num? time:num? version:? vrev:? noservice:bool? servicemark:? withvalidate:?' => \&sourcecommitfilelist,
  'POST:/source/$project/$package cmd=copy rev? user:? comment:? orev:rev? oproject:project? opackage:package? expand:bool? keeplink:bool? repairlink:bool? linkrev? setrev:linkrev? olinkrev:linkrev? requestid:num? dontupdatesource:bool? noservice:bool? withvrev:bool? withacceptinfo:bool? makeoriginolder:bool? freezelink:bool? vrevbump:num? instantiate:bool?' => \&sourcecopy,
  'POST:/source/$project/$package cmd=collectbuildenv user:? comment:? orev:rev? oproject:project? opackage:package?' => \&sourcecollectbuildenv,
  'POST:/source/$project/$package cmd=branch rev? user:? comment:? orev:rev? oproject:project? opackage:package? olinkrev:linkrev? requestid:num? force:bool? keepcontent:bool? missingok:bool? noservice:bool? withacceptinfo:bool? time:num? extendvrev:bool?' => \&sourcebranch,
  'POST:/source/$project/$package cmd=linktobranch rev? user:? comment:? linkrev?' => \&linktobranch,
  'POST:/source/$project/$package cmd=deleteuploadrev' => \&deleteuploadrev,
  'POST:/source/$project/$package cmd=undelete user:? comment:? time:num?' => \&undeletepackage,
  'POST:/source/$project/$package cmd=runservice user:? comment:?' => \&triggerservicerun,
  'POST:/source/$project/$package cmd=waitservice' => \&waitservicerun,
  'POST:/source/$project/$package cmd=mergeservice user:? comment:?' => \&mergeservicerun,
  'POST:/source/$project/$package cmd=getprojectservices' => \&getprojectservices,
  'POST:/source/$project/$package cmd=notifypackagechange' => \&notifypackagechange,
  'POST:/source/$project/$package cmd: *:*' => \&unknowncmd,

  'PUT:/source/$project/$package cmd: rev? user:? comment:?' => \&sourcecommitfilelist,	# obsolete

  '/source/$project/$package view=getmultibuild' => \&getmultibuildpackages,
  '/source/$project/$package view=info rev? linkrev? parse:bool? nofilename:bool? repository? arch? withchangesmd5:bool?' => \&getpackagesourceinfo,
  '/source/$project/$package rev? linkrev? emptylink:bool? deleted:bool? expand:bool? view:? extension:? lastworking:bool? withlinked:bool? meta:bool? product:?' => \&getfilelist,
  '/source/$project/$package/_history rev? meta:bool? deleted:bool? limit:num?' => \&getpackagehistory,
  '/source/$project/$package/_meta rev? expand:bool? meta:bool? deleted:bool? view:?' => \&getpackage,
  'PUT:/source/$project/$package/_meta user:? comment:? requestid:num?' => \&putpackage,
  'DELETE:/source/$project/$package user:? comment:? requestid:num?' => \&delpackage,
  '/source/$project/$package/$filename rev? expand:bool? meta:bool? deleted:bool? view:?' => \&getfile,
  'PUT:/source/$project/$package/$filename rev? user:? comment:? keeplink:bool? force:bool? meta:bool?' => \&putfile,
  'DELETE:/source/$project/$package/$filename rev? user:? comment:? keeplink:bool? force:bool? meta:bool?' => \&delfile,

  # /published name spec: access published binaries
  '/published' => \&published,
  '/published/$project' => \&published,
  '/published/$project/$repository view=publishedpath medium:?' => \&published_path,
  '/published/$project/$repository' => \&published,
  '/published/$project/$repository/$arch:filename view:?' => \&published,
  '/published/$project/$repository/$arch:filename/$filename view:?' => \&published,
  '/published/$project/$repository/$arch:filename/$filename/$subfilename:filename view:?' => \&published,

  # scheduler calls
  '/getprojpack $project* $repository* $package* $arch? withrepos:bool? withsrcmd5:bool? withdeps:bool? withconfig:bool? expandedrepos:bool? ignoredisable:bool? nopackages:bool? withremotemap:bool? remotemaponly:bool? noremote:bool? parseremote:bool? buildinfo:bool? partition:? view:?' => \&getprojpack,
  'POST:/relsync $project $repository $arch' => \&postrelsync,
  '/relsync $project $repository $arch' => \&getrelsync,

  # worker capabilities
  '/worker/$workerid' => \&getworkercap,
  'POST:/worker cmd=checkconstraints $project $repository $arch $package' => \&checkconstraints,

  # worker calls
  '/getsources $project $package $srcmd5:md5' => \&getsources,
  '/getconfig $project $repository path:prp*' => \&getbuildconfig,

  '/getsignkey $project withpubkey:bool? autoextend:bool? withalgo:bool?' => \&getsignkey,
  '/getsslcert $project autoextend:bool?' => \&getsslcert,
  '/getbinaries $project $repository $arch binaries: nometa:bool?' => \&worker_getbinaries,
  '/getbinaryversions $project $repository $arch binaries: nometa:bool?' => \&worker_getbinaryversions,
  '!- /lastevents $filter:* start:num? obsname:?' => \&lastevents,
  '/lastnotifications start:num? view:? block:bool? noprune:bool?' => \&lastnotifications,
  '/notificationpayload/$payloadkey:filename' => \&getnotificationpayload,
  'DELETE:/notificationpayload/$payloadkey:filename' => \&deletenotificationpayload,
  'POST:/event type: project: package:? repository:? arch:? job:?' => \&newevent,
  # tmp until lightty gets fixed
  '/public/lastevents $filter:* start:num? obsname:?' => \&lastevents,

  # search interface
  '/search $in: $match: return:? values:bool?' => \&search,
  '/search/project $match:' => \&search_proj,
  '/search/project/id $match:' => \&search_proj_id,
  '/search/package $match:' => \&search_pack,
  '/search/package/id $match:' => \&search_pack_id,

  'POST:/search/published cmd:' => \&search_published_updatedb,
  '/search/published/binary/id $match: limit:num? withdownloadurl:bool?' => \&search_published_binary_id,
  '/search/published/pattern/id $match: limit:num? withdownloadurl:bool?' => \&search_published_pattern_id,

  # service interface, just for listing for now
  '/service' => \&listservices,
#  '/service/$service' => \&service,

  # configuration
  'PUT:/configuration' => \&putconfiguration,
  '/configuration' => \&getconfiguration,

  # issue trackers
  'PUT:/issue_trackers' => \&putissuetrackers,
  '/issue_trackers' => \&getissuetrackers,

  # build calls for binary files
  '/build' => \&getprojectlist,
  '/build/_workerstatus scheduleronly:bool? daemonsonly:bool? arch*' => \&getworkerstatus,
  'PUT:/build/_dispatchprios' => \&putdispatchprios,
  '/build/_dispatchprios' => \&getdispatchprios,
  '/build/$project/_availablebinaries url:* path:prp* arch*' => \&getavailablebinaries,
  'POST:/build/$project cmd: repository* arch* package* code:* wipe:* comment:?' => \&docommand,
  '/build/$project' => \&getrepositorylist,
  '/build/$project/_result oldstate:md5? view:resultview* lastbuild:bool? repository* arch* package* code:* multibuild:bool? locallink:bool?' => \&getresult,
  '/build/$project/$repository' => \&getarchlist,
  '/build/$project/$repository/_buildconfig path:prp*' => \&getbuildconfig,
  '/build/$project/$repository/$arch package* view:?' => \&getpackagelist_build,
  '!- /build/$project/$repository/$arch/_builddepinfo package* view:?' => \&getbuilddepinfo,
  '/build/$project/$repository/$arch/_jobhistory package* code:* limit:num?' => \&getjobhistory,
  'POST:/build/$project/$repository/$arch/_repository match:' =>  \&postrepo,
  'POST:/build/$project/$repository/$arch/$package cmd=copy oproject:project? opackage:package? orepository:repository? setupdateinfoid:? resign:bool? setrelease:? multibuild:bool?' => \&copybuild,
  'POST:/build/$project/$repository/$arch/$package' => \&uploadbuild,
  '/build/$project/$repository/$arch/$package_repository view:? binary:filename* nometa:bool? nosource:bool? withmd5:bool?' => \&getbinarylist,
  'POST:/build/$project/$repository/$arch/$package_repository/_buildinfo add:* debug:bool?' => \&getbuildinfo_post,
  '/build/$project/$repository/$arch/$package/_buildinfo add:* internal:bool? debug:bool?' => \&getbuildinfo,
  '/build/$project/$repository/$arch/$package/_jobstatus' => \&getjobstatus,
  '/build/$project/$repository/$arch/$package/_log nostream:bool? last:bool? start:intnum? end:num? view:?' => \&getlogfile,
  '/build/$project/$repository/$arch/$package/_reason' => \&getbuildreason,
  '/build/$project/$repository/$arch/$package/_status' => \&getbuildstatus,
  '/build/$project/$repository/$arch/$package/_history limit:num?' => \&getbuildhistory,
  '/build/$project/$repository/$arch/$package_repository/$filename view:?' => \&getbinary,
  'PUT:/build/$project/$repository/$arch/_repository/$filename ignoreolder:bool? wipe:bool?' => \&putbinary,
  'DELETE:/build/$project/$repository/$arch/_repository/$filename' => \&delbinary,

  '/request/_lastid' => \&getlastidrequest,	# just required for migration into api
  '/request/$id:num' => \&getrequest, 		# just required for migration into api

  # for masterdispatcher syncing
  '/jobs/$arch partition:*' => \&listjobs,

  # notifications from publisher/repserver - CGI
  'POST:/notify/$_type: *:?' => \&external_notification,
  # called from the API to notify hermes/rabbitmq
  'POST:/notify_plugins/$_type:' => \&notify_plugins,

  # cloud upload calls
  'POST:/cloudupload $project $repository $arch $package $filename user: target:' => \&cloudupload_create,
  'POST:/cloudupload/$job cmd=kill' => \&cloudupload_kill,
  '/cloudupload/_pubkey view:?' => \&cloudupload_pubkey,
  '/cloudupload/$job' => \&cloudupload_status,
  '/cloudupload/$job/_log nostream:bool? start:intnum? end:num? view:?' => \&cloudupload_log,
  '/cloudupload name:num*' => \&cloudupload_joblist,

  '/ajaxstatus' => \&getajaxstatus,
  '/serverstatus' => \&BSStdServer::serverstatus,
];

####################################################################

my $dispatches_ajax = [
  '/' => \&hello,
  '/ajaxstatus' => \&getajaxstatus,
  '/build/$project/_result oldstate:md5? view:resultview* repository* arch* package* code:*' => \&getresult,
  '/build/$project/$repository/$arch package* view:?' => \&getpackagelist_build,
  '/build/$project/$repository/$arch/$package/_log nostream:bool? last:bool? start:intnum? end:num?' => \&getlogfile,
  '/build/$project/$repository/$arch/$package_repository view:? binary:filename* nometa:bool? nosource:bool? withmd5:bool?' => \&getbinarylist,
  '/getbinaries $project $repository $arch binaries: nometa:bool? raw:bool?' => \&worker_getbinaries,
  '/getbinaryversions $project $repository $arch binaries: nometa:bool?' => \&worker_getbinaryversions,
  '/lastevents $filter:* start:num? obsname:?' => \&lastevents,
  '/lastnotifications start:num? view:? block:bool?' => \&lastnotifications,
  '/source/$project/$package cmd=waitservice servicemark:' => \&waitservicerun,
  '/source/$project/$package rev view:' => \&getfilelist_ajax,
  '/source/$project/$package:package/$filename rev?' => \&getfile,
  '/request/$id:num withkey:bool? oldkey:md5?' => \&getrequest,
  '/sourcediffcache/$cacheid:md5 view:?' => \&getsourcediffcache,
  '/cloudupload/$job/_log nostream:bool? start:intnum? end:num? view:?' => \&cloudupload_log,
];

####################################################################

my $conf = {
  'port' => $port,
  'dispatches' => $dispatches,
  'maxchild' => 20,
  'maxchild2' => 20,
  'slowrequestthr' => 10,
};

my $aconf = {
  'socketpath' => $ajaxsocket,
  'dispatches' => $dispatches_ajax,
};

if ($BSConfig::workersrcserver) {
  my $wport = $port;
  $wport = $1 if $BSConfig::workersrcserver =~ /:(\d+)$/;
  $conf->{'port2'} = $wport if $wport != $port;
}

# create bsdir before root privileges are dropped
BSUtil::mkdir_p_chown($BSConfig::bsdir, $BSConfig::bsuser, $BSConfig::bsgroup);

# set a repoid for identification of this data repository
if (! -e "$projectsdir/_repoid") {
  BSUtil::mkdir_p_chown($projectsdir, $BSConfig::bsuser, $BSConfig::bsgroup);
  $datarepoid = sprintf("%09d", int(rand(1000000000)));
  writestr("$projectsdir/._repoid$$", "$projectsdir/_repoid", $datarepoid);
}
$datarepoid = readstr("$projectsdir/_repoid");

BSStdServer::server('bs_srcserver', \@ARGV, $conf, $aconf);

