#!/bin/sh -e

distrodbdir=$1
component=$2

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,,;s,^/lib64/,,;/\(python[23]\|perl5|php\)/d' $distrodbdir/plugins/${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 '/^debug64(/d' $distrodbdir/provides/${component}.txt
sed -i -e 's,^/usr/share/fonts/.*/,,;s,\.\(gz\|bz2\|xz\)\t,\t,' $distrodbdir/fonts/${component}.txt

# python prepare
sed -i -e '/\/site-packages\//d' $distrodbdir/python{2,3}/${component}-{sc,pc}.txt
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

# 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 -e 's,\.so\t,\t,' $distrodbdir/python2.raw/${component}-so.txt > $distrodbdir/python2/${component}-sp.txt
sed -e 's,\.py\t,\t,;s,/__init__\t,\t,;' $distrodbdir/python2.raw/${component}-py.txt >> $distrodbdir/python2/${component}-sp.txt
sed -i -e 's,\.so\t,\t,' $distrodbdir/python2/${component}-sc.txt
sed -i -e 's,\.py\t,\t,;s,/__init__\t,\t,;' $distrodbdir/python2/${component}-pc.txt

sed -e 's,\.so\t,\t,' $distrodbdir/python3.raw/${component}-so.txt > $distrodbdir/python3/${component}-sp.txt
sed -e 's,\.py\t,\t,;s,/__init__\t,\t,;' $distrodbdir/python3.raw/${component}-py.txt >> $distrodbdir/python3/${component}-sp.txt
sed -i -e 's,\.so\t,\t,' $distrodbdir/python3/${component}-sc.txt
sed -i -e 's,\.py\t,\t,;s,/__init__\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


# python old
#sed -i -e 's,^python2\.[0-9]*(,,;s,)\t,.py\t,' $distrodbdir/python2/${component}-rp.txt
#sed -i -e 's,^python3\(\.[0-9]*\)?(,,;s,)\t,.py\t,' $distrodbdir/python3/${component}-rp.txt
#awk '{sub(/\./,"/",$1)}' $distrodbdir/python2/${component}-rp.txt > $distrodbdir/python2/${component}-pf.txt
#awk '{sub(/\./,"/",$1)}' $distrodbdir/python3/${component}-rp.txt > $distrodbdir/python3/${component}-pf.txt
#rm -f $distrodbdir/python2/${component}-rp.txt
#rm -f $distrodbdir/python3/${component}-rp.txt



# pyegg
sed -i -e '/^.usr.share.jython./d' $distrodbdir/pyegg/${component}.txt
egrep '^/usr/lib(64)?/python' $distrodbdir/pyegg/${component}.txt > $distrodbdir/pyegg/${component}-1.txt
egrep -v '^/usr/lib(64)?/python' $distrodbdir/pyegg/${component}.txt > $distrodbdir/pyegg/${component}-2.txt
sed -i -e 's,/lib/,/lib64/,;s,^/usr/lib64/python\([2-9]\).*/site-packages/,\1/,;s,/[^/]*/,/,' $distrodbdir/pyegg/${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/${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/${component}-2.txt
sed -i -e '/^pythonegg/!d' $distrodbdir/pyegg/${component}-2.txt
cat $distrodbdir/pyegg/${component}-?.txt > $distrodbdir/pyegg/${component}.txt
rm -f $distrodbdir/pyegg/${component}-?.txt
sed -i -e '/^pythonegg(.)(UNKNOWN)/d' $distrodbdir/pyegg/${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/,,' $distrodbdir/php.raw/${component}-mo.txt
sed -i -e 's,^/usr/share/php/pear/,,' $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/,,;' $distrodbdir/texmf/${component}.txt

## cmake
sed -i -e 's,^/usr,,;s,/lib/,/lib64/,;s,^/lib64/,/share/,;s,^/share/cmake/,,' $distrodbdir/cmake/${component}.txt
sed -i -e '/\//d' $distrodbdir/cmake/${component}.txt
# modules can contain any garbage: libqutim-devel|/usr/share/cmake/Modules/FindPhonon.cmake
sed -i -e '/^[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

### 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
