#!/bin/ash
#called from 0setup, either in Woof or /usr/local/petget/0setup in running Puppy.
#(in Woof, this script is in support/, in running pup /usr/local/petget/mageia2ppm).
#pass name of file on commandline, which is 'synthesis.hdlist' online (renamed to Packages-mageia-1-corepre|nonfreepre|taintedpre).
#second param is Mageia release version number. ex: 1
#110612 first release.
#110613 sometimes comes up with a dep that is itself.
#110615 determine online subdir.
#120515 release 1 was all "mga1" in pkg name, but release 2 has mix of "mga1" and "mga2" pkgs. also pkgs can have "nonfree" or "tainted" in name.

export LANG=C #faster.

SYNTHHDLIST="$1"
[ "$SYNTHHDLIST" = "" ] && exit 1
[ ! -f "$SYNTHHDLIST" ] && exit 1
MAGEIA_VERSION="$2"
[ "$MAGEIA_VERSION" = "" ] && exit

#110615 determine online subdir...
SUBREPO="`echo "$SYNTHHDLIST" | rev | cut -f 1 -d '-' | rev | sed -e 's%pre$%%'`" #ex: core
DB_path="${SUBREPO}/release"

#ppm package database format, last 3 are optional...
#pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
#...'compileddistro|compiledrelease' identify where the package was compiled.

echo
#120515 the pkg name can now be mga1 or mga2, allow for the future, up to version 9...
#120515 also, nonfree db can have pkg names ex: libopenmotif-devel-2.3.3-1.mga2.nonfree.i586.rpm
# tainted db ex: audacious-adplug-3.2.1-1.mga2.tainted.i586.rpm
#mgaPTN0="\\[== [^@]*\\.mga${MAGEIA_VERSION}\\]"
#mgaPTN1="s%\\.mga${MAGEIA_VERSION}.*%%"
mgaPTN0="\\[== [^@]*\\.mga[1-9]\\]"
mgaPTN1="s%\\.mga[1-9].*%%"
xmgaPTN0="\\[== [^@]*\\.mga[1-9]\\.nonfree\\]"
ymgaPTN0="\\[== [^@]*\\.mga[1-9]\\.tainted\\]"

echo -n "" > /tmp/mageia2ppm-ppm-temp1
cat $SYNTHHDLIST |
while read ONELINE
do
 echo -n '.'
 case $ONELINE in
  @provides@*)
   DB_version="`echo -n "$ONELINE" | grep -o "$mgaPTN0" | cut -f 2 -d ' ' | rev | cut -f 2-9 -d '.' | rev | head -n 1 | cut -f 2 -d ':'`"
   [ "$DB_version" = "" ] && DB_version="`echo -n "$ONELINE" | grep -o "$xmgaPTN0" | cut -f 2 -d ' ' | rev | cut -f 3-9 -d '.' | rev | head -n 1 | cut -f 2 -d ':'`"
   [ "$DB_version" = "" ] && DB_version="`echo -n "$ONELINE" | grep -o "$ymgaPTN0" | cut -f 2 -d ' ' | rev | cut -f 3-9 -d '.' | rev | head -n 1 | cut -f 2 -d ':'`"
   [ "$DB_version" = "" ] && DB_version="0" #error condition.
   PROVIDES_SO=" `echo -n "$ONELINE" | tr '@' '\n' | tr -s '\n' | grep -E '\.so$|\.so\.[0-9]' | cut -f 1 -d '(' | sort -u | tr '\n' ' '`" #list of shared libs, space-delimited.
  ;;
  @conflicts@*)
   true
  ;;
  @requires@*)
   REQUIRES_SO=" `echo -n "$ONELINE" | tr '@' '\n' | tr -s '\n' | grep -E '\.so$|\.so\.[0-9]' | cut -f 1 -d '(' | sort -u | tr '\n' ' '`" #list of shared libs, space-delimited.
   REQUIRES_PKG=" `echo -n "$ONELINE" | tr '@' '\n' | tr -s '\n' | grep -v -E '\.so$|\.so\.[0-9]|/|requires|\(' | cut -f 1 -d '[' | sort -u | tr '\n' ' '`"
  ;;
  @summary@*)
   DB_description="`echo -n "$ONELINE" | cut -f 3 -d '@' | tr '[|<>]' ' '`"
  ;;
  @info@*)
   FULLNAME="`echo -n "$ONELINE" | cut -f 3 -d '@'`"
   DB_fullfilename="${FULLNAME}.rpm"
   DB_pkgname="`echo -n "$FULLNAME" | sed -e "$mgaPTN1"`"
   vPTN="s%\\-${DB_version}.*%%"
   DB_nameonly="`echo -n "$DB_pkgname" | sed -e "$vPTN"`"
   DB_compileddistro="mageia";DB_compiledrelease="$MAGEIA_VERSION"
   if [ "`echo -n "$FULLNAME" | grep '\.noarch$'`" != "" ];then
    DB_compileddistro=""
    DB_compiledrelease=""
   fi
   sizeBYTES=`echo -n "$ONELINE" | cut -f 5 -d '@'`
   DB_size=$(($sizeBYTES/1024))
   categoryORIG="`echo -n "$ONELINE" | cut -f 6 -d '@'`"
   #puppy categories are: Desktop, System, Setup, Utility, Filesystem, Graphic, Document, Business, Personal, Network, Internet, Multimedia, Fun, BuildingBlock, Help, Develop
   case $categoryORIG in
    Development*) DB_category="Development" ;;
    Archiving*|Sciences*|Monitoring*|Terminals*) DB_category="Utility" ;;
    System/Internationalization*|System/Font*) DB_category="System" ;;
    System/Configuration*) DB_category="Setup" ;;
    System*) DB_category="BuildingBlock" ;;
    Office*|Database*) DB_category="Business" ;;
    Networking/Other|Networking/Remote*|Communications*) DB_category="Network" ;;
    Networking*) DB_category="Internet" ;;
    *desktop*|Accessibility*) DB_category="Desktop" ;;
    Video*|Sound*) DB_category="Multimedia" ;;
    Graphics*) DB_category="Graphic" ;;
    Games*|Toys*) DB_category="Fun" ;;
    Text*|Publishing*|Education*|Editors*) DB_category="Document" ;;
    File*) DB_category="Filesystem" ;;
    Books*) DB_category="Help" ;;
    *) DB_category="BuildingBlock" ;;
   esac
   #3 fields on end are temporary...
   echo "$DB_pkgname|$DB_nameonly|$DB_version||$DB_category|${DB_size}K|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|${DB_compileddistro}|${DB_compiledrelease}||${PROVIDES_SO}|${REQUIRES_SO}|${REQUIRES_PKG}|" >> /tmp/mageia2ppm-ppm-temp1
   PROVIDES_SO="";REQUIRES_SO="";REQUIRES_PKG="";FULLNAME="";DB_pkgname='';DB_nameonly='';DB_version='';DB_category='';DB_size='';DB_fullfilename='';DB_dependencies='';DB_description=''
  ;;
 esac
done

#want only DB_nameonly|PROVIDES_SO...
echo
cat /tmp/mageia2ppm-ppm-temp1 | cut -f 2,14 -d '|' > /tmp/mageia2ppm-ppm-temp2

echo -n "" > /tmp/mageia2ppm-ppm-temp3
cat /tmp/mageia2ppm-ppm-temp1 |
while read ONELINE
do
 echo -n '.'
 REQUIRES_SO="`echo -n "$ONELINE" | cut -f 15 -d '|'`"
 REQUIRES_PKG="`echo -n "$ONELINE" | cut -f 16 -d '|'`"
 DB_dependencies=""
 NAMEONLY="`echo -n "$ONELINE" | cut -f 2 -d '|'`" #110613
 
 for ONESO in $REQUIRES_SO
 do
  ONEDEP="`grep " ${ONESO} " /tmp/mageia2ppm-ppm-temp2 | cut -f 1 -d '|' | head -n 1`"
  if [ "$ONEDEP" ];then
   [ "$ONEDEP" = "$NAMEONLY" ] && continue #110613 sometimes comes up with a dep that is itself.
   [ "$ONEDEP" = "glibc" ] && continue #110613 leave these out.
   [ "$ONEDEP" = "pkgconfig" ] && continue #110613 leave these out.
   DB_dependencies="${DB_dependencies},+${ONEDEP}"
  fi
 done
 for ONEPKG in $REQUIRES_PKG
 do
  [ "$ONEPKG" = "$NAMEONLY" ] && continue #110613 sometimes comes up with a dep that is itself.
   [ "$ONEPKG" = "glibc" ] && continue #110613 leave these out.
   [ "$ONEPKG" = "pkgconfig" ] && continue #110613 leave these out.
  DB_dependencies="${DB_dependencies},+${ONEPKG}"
 done
 DB_dependencies="`echo -n "$DB_dependencies" | sed -e 's%^,%%' | tr ',' '\n' | sort -u | tr '\n' ',' | sed -e 's%,$%%'`"

 CUT1to8="`echo -n "$ONELINE" | cut -f 1-8 -d '|'`"
 CUT10to13="`echo -n "$ONELINE" | cut -f 10-13 -d '|'`"
 echo "${CUT1to8}|${DB_dependencies}|${CUT10to13}|" >> /tmp/mageia2ppm-ppm-temp3
done

sort --key=1 --field-separator="|" /tmp/mageia2ppm-ppm-temp3 > /tmp/mageia2ppm-ppm-temp4
rm -f /tmp/mageia2ppm-ppm-temp3
rm -f /tmp/mageia2ppm-ppm-temp1
rm -f /tmp/mageia2ppm-ppm-temp2

###END###
