#!/bin/sh
package=$1
filename=$(basename -- "$package")
dirname=$(dirname $package)
file_without_ext="${filename%.*}"
mkdir -p /tmp/$file_without_ext
rm -rf /tmp/$file_without_ext/*
dpkg-deb -R $package /tmp/$file_without_ext
#sed -i 's/\/usr\/lib\/dkms\/common.postinst//g' /tmp/$file_without_ext/DEBIAN/postinst
package_name=`sed -n 's/^Package: *//p' /tmp/$file_without_ext/DEBIAN/control`
mkdir -p /tmp/$file_without_ext/usr/share/$package_name
if [ ! -f "/tmp/$file_without_ext/usr/share/$package_name/postinst" ]; then
	cp /usr/lib/dkms/common.postinst /tmp/$file_without_ext/usr/share/$package_name/postinst
fi
sed -i 's/\/usr\/lib\/dkms\/common.postinst/\/usr\/share\/'$package_name'\/postinst/g' /tmp/$file_without_ext/DEBIAN/postinst
for POSTINST in  "/tmp/$file_without_ext/usr/share/*/postinst"; do
	sed -i 's/ARCH/DKMSARCH/g' $POSTINST
done
dpkg-deb -b /tmp/$file_without_ext $dirname/${file_without_ext}_fixed.deb 
rm -rf /tmp/$file_without_ext
