#!/bin/bash
#
# $Id: archive-lib 73 2004-09-11 09:24:35Z guillem $
#
# Copyright (C) 2003, 2004 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

#
# Requires: procmail (formail)
#

# Import configuration

. archive.conf

# Canonicalize directories

accepted_dir=$queue_dir/accepted
rejected_dir=$queue_dir/rejected
rejected_daily_dir=$rejected_dir/`date -I`
unchecked_dir=$queue_dir/unchecked
byhand_dir=$queue_dir/byhand

######
# file input functions

fetch_field ()
{
  local field=$1

  formail -z -x$field:
}

fetch_files ()
{
  formail -xFiles: | cut -d' ' -f6
}

fetch_md5sums ()
{
  formail -xFiles: | cut -d' ' -f2,6 | sed -e 's/ /  /'
}

fetch_secure_files ()
{
  local changes=$1
  local files=`fetch_files < $changes`

  for file in $files; do
    echo $(basename `readlink -f $file`)
  done
}

get_suite_arches ()
{
  local suite=$(canonic_suite $1)

  eval echo \$arch_list_$suite
}

valid_arch ()
{
  local v_suite=$1
  local v_arch=$2

  for arch in $(get_suite_arches $v_suite) all; do
    if [ "$arch" = "$v_arch" ]; then
      return 0
    fi
  done

  return 1
}

canonic_suite()
{
  local code_name=$1

  for s_alias in $suite_alias; do
    s_code_name=${s_alias%%:*}
    s_suite=${s_alias##*:}

    if [ "$code_name" = $s_code_name ]; then
      echo $s_suite
      return 0
    fi
  done

  echo $code_name
}

poolize_archive_file()
{
  local archive_file=$1
  local package=`fetch_field "Source" < $archive_file`

  poolize_name $package
}

poolize_name()
{
  local name=$1
  local package=`echo $name | cut -d_ -f1`
  local section=main
  local hash_dir=`echo $package | \
    sed -e 's/^\(lib.\).*$/\1/;/^lib/q;s/^\(.\).*$/\1/'`
  local dest_dir=$pool_dir/$section/$hash_dir/$package/

  echo $dest_dir
}

# .changes managment functions

changes_canonic ()
{
  while read f_changes; do
    dir=`dirname $f_changes`
    changes=`basename ${f_changes%.changes}`
    package=`echo $changes | cut -d_ -f1`
    version=`echo $changes | cut -d_ -f2`
    arch=`echo $changes | cut -d_ -f3`

    printf "%-30s %-20s %s %s\n" $package $arch $version $f_changes
  done
}

changes_newest ()
{
  while read package arch version path; do
    if [ "$package" != "$g_package" ] || [ "$arch" != "$g_arch" ]; then
      printf "%-30s %-20s %s %s\n" $g_package $g_arch $g_version $g_path
      g_package=$package
      g_version=$version
      g_arch=$arch
      g_path=$path
    else
      if dpkg --compare-versions $version le $g_version; then
        g_package=$package
        g_version=$version
        g_arch=$arch
        g_path=$path
      fi
    fi
  done
}

changes_obsolete ()
{
  while read package arch version path; do
    if [ "$package" = "$g_package" ] && [ "$arch" = "$g_arch" ]; then
      if dpkg --compare-versions $g_version lt $version; then
        printf "%-30s %-20s %s %s\n" $g_package $g_arch $g_version $g_path
      fi
    fi
    g_package=$package
    g_version=$version
    g_arch=$arch
    g_path=$path
  done
}

changes_strip ()
{
  awk  '
BEGIN { block = 0; contents = 0 }
/^-----BEGIN PGP SIGNATURE/ { exit }
block && contents { print }
/^-----BEGIN PGP SIGNED/ { block = 1 }
block && /^ *$/ { contents = 1 }
'
}

files_owner_perms ()
{
  local files=$@

  chown -f $archive_owner:$archive_group $files
  chmod $archive_perms $files
}

dirs_owner_perms ()
{
  local dirs=$@

  chown -f $archive_owner:$archive_group $dirs
  chmod $archive_perms_dir $dirs
}

logpipe()
{
  local name=$1
  local log_monthly_dir=$log_dir/`date +%Y`/`date +%m`
  local logfile=$log_monthly_dir/$name-`date -I`.log

  mkdir -p $log_monthly_dir
  dirs_owner_perms $log_monthly_dir
  tee -a $logfile
}

log()
{
  local name=$1

  shift
  echo -e "$@" | logpipe "$name"
}

msg_queue ()
{
  local subject=$1
  local recipient=$2

  (
    cat
    echo "$subject"
    echo
  ) | logpipe queue

  if [ "$MAIL_REPORT" = "yes" ]; then
    (
      cat
      echo
      echo "-- "
      echo "Archive Maintainer ($archive_maint)"
    ) | mail -s "$subject" "$recipient"
  fi
}

