#!/bin/sh
##
#  Korinf project
#
#  convert built packages in chrooted Linux system
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009, 2013
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2013
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

kormod converts/alien

# FIXME HACK: do the same name conversion as alien
internal_get_deb_pkgname_from_rpm()
{
	echo $1 | tr [A-Z] [a-z] | sed -e "s|\.i586.rpm|_i386.deb|g" | sed -e "s|\.x86_64.rpm|_amd64.deb|g" | sed -e "s|\.noarch.rpm|_all.deb|g" |
		sed -e "s|-\([0-9a-z\.]*\)-alt\(.*\)_|_\1-eter\2${PKGVENDOR}_|g"
}

# full package name tranlation rpm->deb
get_deb_pkgname_from_rpm()
{
	internal_get_deb_pkgname_from_rpm $1 | filter_deb_pkgnames
}

__wine_dependence_hack()
{
	local pkg="$1"
	local depf="$2"
	# only for wine packages
	if rhas "$pkg" "wine" ; then
		# HACK due requires on x86_64?? FIXME
		if [ $DISTRNAME = "Ubuntu" ] && version_more_version $DISTRVERSION 12.04 || [ $DISTRNAME = "Debian" ] && version_more_version $DISTRVERSION 7.0 || [ $DISTRNAME = "AstraLinux" ]; then
			# FIXME: replace with grep inplace or just subst
			local df=$DESTLOGDIR/${depf}.deb.depends
			# just drop lib dependencies
			mv $df $df.orig
			grep "^lib" $df.orig > $df
			rm -f $df.orig
			fi
	fi
}


# convert in target system
convert_debian()
{
	assert_var TARGETBUILDNAME PACKAGEVERSION PACKAGERELEASE PKGVENDOR BUILDARCH PKGFORMAT
	assert_var DISTRNAME DISTRVERSION

	# TODO: can be noarch (check the result package)
	prepare_convert

	set_destlogdir
	for i in $BUILTBINPKGLIST ; do
		if [ -e $i ] ; then
			# prepare dependencies file
			# TODO: rewrite this part
			local depf=$(get_pkgname_from_filename $i)
			local dependencies=""
			if get_target_deplist "$depf" | tee $DESTLOGDIR/$depf.deb.depends ; then
				__wine_dependence_hack "$i" "$depf"
				#cp "$DESTLOGDIR/${depf}.deb.depends" ${depf}.deb.depends
				local dep
				for dep in $(cat "$DESTLOGDIR/${depf}.deb.depends") ; do
					# FIXME: handle first arg separately before the cycle
					#[ -z "$dependencies" ] || dependencies="$dependencies, "
					dependencies="$dependencies, $dep"
				done
				#dependencies=$(estrlist uniq $prepreqs)
				#dependencies=$(cat "$DESTLOGDIR/${depf}.deb.depends" | xargs -n1000 echo)
			else
				rm -f $DESTLOGDIR/$depf.deb.depends
				#touch $depf.deb.depends
			fi

	cat <<EOF | run_in_chroot --user "bash -x -" || return
	umask 002
	mkdir -p ~/RPM/log

	rm -rf $WRKDIR/
	mkdir -p $WRKDIR/src/ && cd $WRKDIR/src/ || exit

	pkg=$INTBUILT/$(basename $i)
	test -r \$pkg || exit

	#fakeroot setarch $BUILDARCH alien --keep-version --scripts \$pkg || exit

	# Note: dpkg-architecture detects Arch via gcc -dumpmachine, see korinf/helpers/gcc-stub-dumpmachine if needs

	# repack with add dependencies
	fakeroot alien --keep-version --scripts --single \$pkg || exit
	cd ${depf}* || exit

	# Note: we need sed -i on target
	# TODO: ${misc:Depends} and debian/nx.substvars
	sed -i -e "s/Depends: \(.*\)/Depends: \1$dependencies/g" debian/control || exit

	# hack for nx (see eterbug #11025)
	sed -i -e "s/dh_shlibdeps$/dh_shlibdeps -- -v -xlibx11-private/g" debian/rules
	fakeroot debian/rules binary

	# hack for nx (see eterbug #11025)
	#sed -i -e "s/libx11-private,//g" debian/*/DEBIAN/control
	#fakeroot dh_builddeb

	cd -

	#rm -f \$pkg
	mv -v *.$PKGFORMAT $INTBUILT/
	rm -rf $WRKDIR/
	true
EOF

			# rename result package to filtered name
			#local debname=$(internal_get_deb_pkgname_from_rpm $i)
			#pwd
			#ls -1
			#debname=$(echo *.deb)
			#local newdebname=$(get_deb_pkgname_from_rpm $i)
			#echo "debname=$debname newdebname=$newdebname"
			#[ "$newdebname" != "$debname" ] && mv -v "$debname" "$newdebname"
		else
			warning "Fatal: Package $i is missed in $(pwd)"
			# FIXME: some projects has variable package list
			return 1
		fi
	done


	# Skip, we can get packages locally
	#echo "$BUILTRPM/$TARGETPKG"
	#run_in_chroot --user "cat $INTBUILT/$TARGETPKG" >$BUILTRPM/$TARGETPKG || return
}

