#!/bin/sh

# Copyright (c) 2007-2008 Aleksey Cheusov <cheusov@NetBSD.org>
# All rights reserved.
#
# See LICENSE file
######################################################################

set -e

. pkg_online_server.env
. pipestatus

export LC_ALL=C

suff="-$PKG_SUMMARY_TYPE"
msg=" $PKG_SUMMARY_TYPE"

if test "$#" = 0; then
    echo 'usage: pkg_summary2field-index <comment|maintainer|...> [files...]' 1>&2
    exit 1
fi

field=$1
shift

summary2dict (){
awk -v field_name="$field" -F= '
NF == 1 {
    next
}

# PKGBASE
$1 == "PKGNAME" {
    pkgbase = $2
    sub(/-[^-]*$/, "", pkgbase)
}

# PKGPATH
$1 == "PKGPATH" {
    pkgpath = $2
}

# ONLY|NOT-FOR
$1 == field_name && field_name ~ /^(ONLY|NOT)FOR$/ {
    if ($2 == "any"){
	next
    }
}

# PLIST
$1 == field_name && field_name == "PLIST" {
    sub(/^[^=]*=/, "")

    plist [++plist_count] = $0

    next
}

# DESCRIPTION
$1 == field_name && field_name == "DESCRIPTION" {
    sub(/^[^=]*=/, "")

    descr [++descr_count] = $0

    next
}

# <FIELD!>
$1 == field_name {
    copy = $0
    sub(/^[^=]*=/, "", copy)

    field = field " " copy

    next
}

# printing
NF == 0 {
    if (field_name == "PKGBASE"){
        field = pkgbase
    }else{
        field = substr(field, 2)
    }
    if (pkgbase != ""){
        if (field != ""){
            printf "_____\n\n%s\n", field "|%%|" pkgpath "," pkgbase
        }
        for (i=1; i <= plist_count; ++i){
            printf "_____\n\n%s\n", plist [i] "|%%|" pkgpath "," pkgbase
        }
        if (descr_count){
            printf "_____\n\n"
            for (i=1; i <= descr_count; ++i){
                if (i > 1) printf " "
                printf "%s", descr [i]
            }
            printf "|%%%%|%s\n", pkgpath "," pkgbase
        }
    }
    field       = ""
    pkgpath     = ""
    pkgbase     = ""
    plist_count = 0
    descr_count = 0
}
' "$@"
}

replace_8bit_symbs (){
    tr -d '\200-\377'
}

dbname=pkgsrc${suff}-index-"$field"
runpipe0 \
    cat "$@" '|' \
    replace_8bit_symbs '|' \
    summary2dict '|' \
    dictfmt -c5 -q -s "$field index" \
       --allchars --index-data-separator '|%%|' \
       --without-header --without-headword -u 'http://www.pkgsrc.org' \
       $dbname

# test for dictfmt bug (max size of headword is limited in
# dictfmt-1.10.10).  DESCRIPTION and other long fields may be broken
# due to this limitation
awk -F'\t' '
NF != 4 && length($0) >= 1000 {
   print "dictfmt is broken, update it to the latest version" > "/dev/stderr"
   exit 1
}' $dbname.index
