Source

packaging / Build

#!/bin/bash

do_time=0
do_list=0
do_clean=0
this_host=`hostname -s`
cc=${CC}
#----------------------------------------------------------------------------
function rpmbuild_link
{
	# Create a link like /home/palmer/rpmbuild to keep rpmbuild happy.
	link=$HOME/rpmbuild
	link_dest=`pwd`/rpmbuild
	if [ -s "$link" ]; then
		echo "Sorry, the symbolic link $link already exists, we need to link it to $link_dest"
		exit 1;
	fi
	if ! [ -f "$link" ]; then
		ln -s "$link_dest" "$link"
	else
		echo "Sorry, $link exists and is not a symbolic link, please check it and remove it"
		exit 1;
	fi
}
#----------------------------------------------------------------------------
function rpmbuild_unlink
{
	# We will only remove ~/rpmbuild if it's a symlink to a directory.
	link=$HOME/rpmbuild
	if [ -d "$link" ]; then
		if [ -s "$link" ]; then
			echo "Removing symbolic link $link"
			rm "$link"
		fi
	fi
}
#----------------------------------------------------------------------------
function do_debian
{
	# For now, we're going to convert from the .rpm generated on CentOS
	# using Alien, we will generate directly in a later revision of this
	# script.

	# Call dl_get_from_spec to get some important knowledge
	specfile="./rpmbuild/SPECS/$1.spec"
	buildarch=`./dl-get_from_spec -s $specfile -v BuildArch`
	version=`./dl-get_from_spec -s $specfile -v Version`
	pkgname=`./dl-get_from_spec -s $specfile -v Name`
	rel=`./dl-get_from_spec -s $specfile -v Release`

	rpmpath=./rpmbuild/RPMS/$buildarch/$pkgname-$version-$rel.$buildarch.rpm
	debpath=`./dl-rpm_to_deb -m path -r $rpmpath`
	debepath=`./dl-rpm_to_deb -m epath -r $rpmpath`
	if [ "$?" -ne "0" ]; then
		echo "Build: Cannot assure path for $rpmpath via support tool - dl-rpm_to_deb"
		exit $?
	fi
	./dl-rpm_to_deb -m alien -r $rpmpath -b $debpath
	if [ "$?" -ne "0" ]; then
		echo "Build: Cannot generate .deb from .rpm via Alien (dl-rpm_to_deb)"
		exit $?
	fi

	# We now have a directory created by Alien, suitable for passing through the standard Debian toolchain
	# First we must change the rules slightly using a slight of hand.

	tmp_rules=/tmp/Build-packaging.rules.`hostname -s`.$$
	./dl-rules_hack -d -i $debepath/debian/rules -o $tmp_rules
	if [ "$?" -ne "0" ]; then
		if [ "$?" -eq "1" ]; then
			echo "dl-rules_hack: Input error"
			exit "$?"
		fi
		if [ "$?" -eq "2" ]; then
			echo "dl-rules_hack: Output error"
			exit "$?"
		fi
		if [ "$?" -eq "3" ]; then
			echo "dl-rules_hack: Command-line syntax error"
			exit "$?"
		fi
		echo "dl-rules_hack: Unknown error"
		exit "$?"
	fi

	if [ -f $tmp_rules ]; then
		diff -u $debepath/debian/rules $tmp_rules | patch -u $debepath/debian/rules
		if [ "$?" -eq "0" ]; then
			rm -v $tmp_rules
			old_path=`pwd` && \
			cd $debepath/ && dpkg-buildpackage && \
			cd $old_path
			echo "Converted $rpmpath to $debpath"
		fi
	fi
}
#----------------------------------------------------------------------------
function do_centos
{
	if ! [ -x '/usr/bin/rpmbuild' ]; then
		sudo yum -y install rpm-build
	fi

	if [ -x '/usr/bin/rpmbuild' ]; then
		rpmbuild_link
		old_dir=`pwd`
		cd "$old_dir/rpmbuild/" && \
		/bin/sh -x ./tar.sh $1 && \
		rpmbuild -v -bb --clean "SPECS/$1.spec"
		rpmbuild_unlink
		if [ "$?" -ne "0" ]; then
			echo "rpmbuild exited with status $?"
			cd "$old_dir"
			exit $?;
		fi
		cd "$old_dir"
	else
		echo "rpm-build is not installed."
		exit 1;
	fi
}
#----------------------------------------------------------------------------
function do_list
{
	if ! [ -x './rpmbuild/tmp/spec_lister' ]; then
		inst_cc -f 0
		$cc -o ./rpmbuild/tmp/spec_lister ./rpmbuild/SOURCES/spec_lister.c
	fi
	if [ -x './rpmbuild/tmp/spec_lister' ]; then
		./rpmbuild/tmp/spec_lister rpmbuild/SPECS
	else
		echo 'Helper program failed to build.'
		exit 1;
	fi
	exit 0;
}
#----------------------------------------------------------------------------
function do_clean_list
{
	if [ -f './rpmbuild/tmp/spec_lister' ]; then
		rm -v ./rpmbuild/tmp/spec_lister
	fi
}
#----------------------------------------------------------------------------
function inst_cpp
{
	if ! [ -x '/usr/bin/g++' ]; then
		if [ -x '/usr/bin/yum' ]; then
			sudo /usr/bin/yum -y install 'gcc-c++'
		else
			sudo apt-get install 'g++'
		fi
	fi
	if ! [ -x '/usr/bin/g++' ]; then
		echo "Failed to install C++ compiler (g++)"
		exit 2;
	fi
}
#----------------------------------------------------------------------------
function inst_cc
{
	if ! [ -x /usr/bin/cc ]; then
		if [ -x /usr/bin/yum ]; then
			sudo /usr/bin/yum -y install gcc
		else
			sudo apt-get install gcc
		fi
	fi
	if ! [ -x /usr/bin/cc ]; then
		echo "Failed to install C compiler (gcc)"
		exit 2;
	fi
}
#----------------------------------------------------------------------------
if ! [ "$cc" ]; then
	cc=cc
fi

if ! [ "$1" ]; then
	echo "Specify package to build or --list to view packages, use --clean for cleanup"
	exit 1;
fi

if [ "$1" = '--list' ]; then
	do_list=1
fi
if [ "$1" = '-l' ]; then
	do_list=1
fi

if [ "$1" = '--clean' ]; then
	do_clean=1
fi
if [ "$1" = '-c' ]; then
	do_clean=1
fi

if [ "$do_list" -eq "1" ]; then
	do_list -f 0
	exit 0;
fi

if [ "$do_clean" -eq "1" ]; then
	do_clean_list -f 0
	exit 0;
fi

if [ "$this_host" = "violet-club" ]; then
	do_time=1
elif [ "$this_host" = "runt" ]; then
	do_time=1
fi

if [ "$do_time" -eq "1" ]; then
	if ! [ -x '/usr/sbin/ntpdate' ]; then
		sudo yum -y install ntp
	fi
	if [ -x '/usr/sbin/ntpdate' ]; then
		sudo /usr/sbin/ntpdate ntp1.gradwell.net
	fi
fi

# Install compilers and other tools configure will look for
inst_cc -f 0
inst_cpp -f 0
if [ -f '/etc/debian_version' ]; then
	do_debian $1
else
	do_centos $1
fi

exit 0;
#----------------------------------------------------------------------------