#!/bin/sh -e

# beware -e : grep can return false!

distrodbdir=$1
component=$2

err_report() {
    echo "repocop-report-helper-distrodb-preprocess: Error in line $1"
}
trap 'err_report $LINENO' ERR

sed -e 's,^/usr,,;s,^/share,,;s,^/lib64,,;s,^/lib,,;s,^/kde[345],,;s,^/kf5,,;s,^/qt[1-6],,;s,^/sbin/,,;s,^/bin/,,;s,^/games/,,;' $distrodbdir/bin/${component}.txt > $distrodbdir/path/${component}.txt

sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^lib,,;s,\.so\t,\t,' $distrodbdir/devel-libs/${component}.txt
sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^lib,,;s,\.a\t,\t,' $distrodbdir/static-libs/${component}.txt
sed -i -e 's,^/usr/include/,,' $distrodbdir/headers/${component}.txt
sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^/lib/,,;s,^/share/,,;s,^pkgconfig/,,;s,\.pc\t,\t,' $distrodbdir/pkg-config/*.txt
sed -i -e 's,^/usr/share/gir-1.0/,,;s,\.gir\t,\t,' $distrodbdir/gir/${component}.txt
sed -i -e 's,^/usr/share/.*/vapi/,,;s,\.vapi\t,\t,' $distrodbdir/vapi/${component}.txt
sed -i -e 's,^/usr/lib64/girepository-1.0/,,;s,\.typelib\t,\t,' $distrodbdir/typelib/${component}.txt
sed -i -e '/^debug64(/d' $distrodbdir/provides/${component}.txt
sed -i -e 's,^/usr/share/fonts/.*/,,;s,\.\(gz\|bz2\|xz\)\t,\t,' $distrodbdir/fonts.raw/${component}.txt

# python prepare
sed -i -e 's,\.cpython-35m\(-[^-]*-linux-gnu\)?\.so\t,.so\t,' $distrodbdir/python3/${component}-sc.txt
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python2[^/]*/,,' $distrodbdir/python2/${component}-{sc,pc}.txt
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python3[^/]*/,,' $distrodbdir/python3/${component}-{sc,pc}.txt
sed -i -e 's,site-packages/,,;s,^lib-dynload/,,' $distrodbdir/python{2,3}/${component}-{sc,pc}.txt

# python raw prepare
sed -i -e 's,\.cpython-35m\(-[^-]*-linux-gnu\)?\.so\t,.so\t,' $distrodbdir/python3.raw/${component}-so.txt
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python2.*/site-packages/,,;s,^lib-dynload/,,' $distrodbdir/python2.raw/${component}-{so,py}.txt
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python3.*/site-packages/,,;s,^lib-dynload/,,' $distrodbdir/python3.raw/${component}-{so,py}.txt

# TODO kill pyeggs here
#Bitten-0.5.3-py2.7.egg/bitten/trac_ext/tests.py    python-module-bitten
#Bitten-0.5.3-py2.7.egg/bitten/trac_ext/tests/charts.py     python-module-bitten
#Bitten-0.5.3-py2.7.egg/bitten/trac_ext/tests/web_ui.py     python-module-bitten
# python prepare
sed -i -e 's,\.so\t,\t,' $distrodbdir/python2/${component}-sc.txt
sed -i -e 's,\.py\t,\t,;s,/__init__\t,\t,;s,/__main__\t,\t,;' $distrodbdir/python2/${component}-pc.txt
sed -i -e 's,\.so\t,\t,' $distrodbdir/python3/${component}-sc.txt
sed -i -e 's,\.py\t,\t,;s,/__init__\t,\t,;s,/__main__\t,\t,;' $distrodbdir/python3/${component}-pc.txt

# python raw merge
cat $distrodbdir/python2.raw/${component}-??.txt | sort -u > $distrodbdir/python2.raw/${component}.txt
cat $distrodbdir/python3.raw/${component}-??.txt | sort -u > $distrodbdir/python3.raw/${component}.txt
rm -f $distrodbdir/python2.raw/${component}-??.txt
rm -f $distrodbdir/python3.raw/${component}-??.txt

# python merge
cat $distrodbdir/python2/${component}-??.txt | sort -u > $distrodbdir/python2/${component}.txt
cat $distrodbdir/python3/${component}-??.txt | sort -u > $distrodbdir/python3/${component}.txt
rm -f $distrodbdir/python2/${component}-??.txt
rm -f $distrodbdir/python3/${component}-??.txt
sed -i -e 's,/,.,g' $distrodbdir/python2/${component}.txt
sed -i -e 's,/,.,g' $distrodbdir/python3/${component}.txt

# pyegg
sed -i -e '/^.usr.share.jython./d' $distrodbdir/pyegg.raw/${component}.txt
grep -E '^/usr/lib(64)?/python' $distrodbdir/pyegg.raw/${component}.txt > $distrodbdir/pyegg.raw/${component}-1.txt ||:
grep -E -v '^/usr/lib(64)?/python' $distrodbdir/pyegg.raw/${component}.txt > $distrodbdir/pyegg.raw/${component}-2.txt ||:
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python\([2-9]\).*/site-packages/,\1/,;s,/[^/]*/,/,' $distrodbdir/pyegg.raw/${component}-1.txt
sed -i -e 's,^\([2-9]\)/\([^-]*\)-\([^-]*\)-py\([2-9]*\)\..*\.egg-info\t,pythonegg(\1)(\2)\t,;s,^\([2-9]\)/\([^-]*\)-py\([2-9]*\)\..*\.egg-info\t,pythonegg(\1)(\2)\t,;s,^\([2-9]\)/\([^-]*\)-\([^-]*\)\.egg-info\t,pythonegg(\1)(\2)\t,;s,^\([2-9]\)/\([^-]*\)\.egg-info\t,pythonegg(\1)(\2)\t,' $distrodbdir/pyegg.raw/${component}-1.txt
sed -i -e 's,^.*/,,;s,^\([^-]*\)-\([^-]*\)-py\([2-9]*\)\..*\.egg-info\t,pythonegg(\3)(\1)\t,;s,^\([^-]*\)-py\([2-9]*\)\..*\.egg-info\t,pythonegg(\2)(\1)\t,' $distrodbdir/pyegg.raw/${component}-2.txt
sed -i -e '/^pythonegg/!d' $distrodbdir/pyegg.raw/${component}-2.txt
cat $distrodbdir/pyegg.raw/${component}-?.txt > $distrodbdir/pyegg.raw/${component}.txt
rm -f $distrodbdir/pyegg.raw/${component}-?.txt
sed -i -e '/^pythonegg(.)(UNKNOWN)/d' $distrodbdir/pyegg.raw/${component}.txt

# raw php
sed -i -e 's,^/usr/lib64/php/.*/extensions/,,' $distrodbdir/php.raw/${component}-so.txt
sed -i -e 's,^/usr/share/php.*/modules/,,;s,^/usr/share/php/pear/,,;s,^/usr/share/php/,,' $distrodbdir/php.raw/${component}-mo.txt
cat $distrodbdir/php.raw/${component}-??.txt | sort -u > $distrodbdir/php.raw/${component}.txt
rm -f $distrodbdir/php.raw/${component}-??.txt

# texmf
sed -i -e 's,^/usr/share/texmf/,,;s,^/usr/share/texmf-texlive/,,;s,^/usr/share/texlive/texmf-dist/,,;s,^/usr/share/texmf-dist/,,;' $distrodbdir/texmf.raw/${component}.txt
sed -i -e '/^doc\//d' $distrodbdir/texmf.raw/${component}.txt

sed -i -e 's,^/usr/\(share\|lib64\|lib\)/java/,,' $distrodbdir/java.raw/${component}.txt
sed -i -e 's,^/usr/lib/mono/,,' $distrodbdir/mono.raw/${component}.txt
sed -i -e 's,^/usr/lib64/aspell/,,' $distrodbdir/aspell.raw/${component}.txt
sed -i -e 's,^/usr/share/gocode/src/,,' $distrodbdir/golang.raw/${component}.txt
sed -i -e 's,^/usr/share/,,' $distrodbdir/shared-data.raw/${component}.txt
sed -i -e 's,^/usr/lib/,,' $distrodbdir/shared-lib.raw/${component}.txt
sed -i -e 's,^/usr/lib64/,,;s,browser-plugins/,,;s,mozilla/plugins/,,' $distrodbdir/mozplugin.raw/${component}.txt
sed -i -e 's,^/usr/\(share\|lib64\|lib\)/nagios/plugins/,,' $distrodbdir/nagios.raw/${component}.txt
sed -i -e 's,^/usr/lib64/vdr/,,;s,\.so[\.0-9]*\t,.so\t,' $distrodbdir/vdr.raw/${component}.txt
sed -i -e 's,^/usr/\(share\|lib64\|lib\)/erlang/lib/,,;s,^\([^/-]\+\)-[0-9.]\+/,\1/,' $distrodbdir/erlang.raw/${component}.txt
sed -i -e 's,^/usr/lib/node_modules/,,' $distrodbdir/nodejs.raw/${component}.txt
sed -i -e 's,^/usr/lib64/ocaml/,,' $distrodbdir/ocaml.raw/${component}.txt
sed -i -e 's,^/usr/share/applications/,,' $distrodbdir/desktop.raw/${component}.txt
# note: modules can contain any garbage: libqutim-devel|/usr/share/cmake/Modules/FindPhonon.cmake
# repocop it!
## cmake - still not a good stem - we select DIRS only -- thus /^[Mm]odules/d
sed -i -e 's,^/usr,,;s,/lib/,/lib64/,;s,^/lib64/,/share/,;s,^/share/cmake/,,' $distrodbdir/cmake/${component}.txt
sed -i -e '/\//d;/^[Mm]odules/d' $distrodbdir/cmake/${component}.txt

### should be no CPANPLUS in autodependencies
sed -i -e '/^perl(CPANPLUS[\.:]/d' $distrodbdir/perl/${component}.txt
### this lumped module provides nothing good
sed -i -e '/perl-Perinci-CmdLine-Any-Lumped$/d' $distrodbdir/perl/${component}.txt
### usually SourceAnalyser hiccup.
sed -i -e '/^perl(use\.pm)/d' $distrodbdir/perl/${component}.txt

# plugins.raw
sed -i -e 's,^/usr,,;s,^/lib64/,,;s,^X11/modules/,xorg/modules/,;/\(python[23]\|perl5|php\)/d' $distrodbdir/plugins.raw/${component}.txt


### TODO: this is a quick hack
### integrate with .so not in devel check!!!
### automapped to libsocket due to .so -- remap to libsocket-devel
sed -i -e 's,\tlibsocket$,\tlibsocket-devel,' $distrodbdir/devel-libs/${component}.txt

### https://bugzilla.altlinux.org/show_bug.cgi?id=32280
sed -i -e '/\tlibgdiplus$/d' $distrodbdir/devel-libs/${component}.txt
