<commit_before>#!/bin/bash
# Copyright (C) 2012 <NAME> <<EMAIL>>
# Licensing: Simplified BSD License, see LICENSE file


# Nice terminal tools
b=$(tput bold)
n=$(tput sgr0)
header() { printf "\n${b}${1}${n}\n\n"; }

# Unmount on error
bind_mounts="dev dev/pts sys proc"
rm_mounts() {
	header "Cleaning up chroot"
	for mt in $(echo $bind_mounts | tac -s' ') build; do
		umount chroot/$mt || true
		sleep 0.5 # Give it time...
	done
}
trap 'header "Failed!!!"; exit 1' ERR


ij_version=3.00
ij_name="cnijfilter-common-$ij_version"
if apt-cache search -n libtiff5 | grep -q .; then
	tiff=libtiff5
else
	tiff=libtiff4
fi

# Get the source
if [ "$1" != "ROOT" ]; then
	# Available here: http://support-au.canon.com.au/contents/AU/EN/0100160604.html
	ij_src="$ij_name-1.tar.gz"
	ij_uri="http://gdlp01.c-wss.com/gds/6/0100001606/01/$ij_src"
	if [ ! -f $ij_src ]; then
		header "Downloading IJ driver source"
		wget $ij_uri
	fi
	if [ ! -d $ij_name ]; then
		header "Extracting driver source"
		tar xf $ij_src
		
		header "Patching driver"
		for patch in patch/*; do
			echo Applying $(basename $patch)
			patch -d $ij_name -p1 < $patch
		done
		sed -i -e "s/libtiff4/$tiff/g" $ij_name/debian/control
	fi
fi


# Run as root
if [ $UID != 0 ]; then
	header "Becoming root, to build/install packages"
	exec sudo $0 ROOT
fi


# Create a chroot
if ! ls *.deb >& /dev/null; then
	if [ ! -d "chroot/usr" ]; then
		if ! which debootstrap >&/dev/null; then
			header "Installing debootstrap"
			apt-get -y install debootstrap
			apt-mark auto debootstrap # autoremovable
		fi
		suite=$(lsb_release -sc)
		header "Creating chroot"
		debootstrap --verbose --variant=buildd --arch=i386 $suite \
			./chroot $debootstrap_mirror \
			|| (sudo rm -rf chroot; false EndWithError)
	fi

	header "Configuring chroot"
	cp /etc/hosts /etc/resolv.conf chroot/etc/
	# Don't install extra packages
	cat > chroot/etc/apt/apt.conf.d/recommends <<EOF
APT::Install-Recommends "0";
APT::Install-Suggests "0";
EOF

	trap 'rm_mounts' EXIT
	for mt in $bind_mounts; do
		mount -o bind /$mt chroot/$mt
	done
	mkdir -p chroot/build
	mount -o bind $ij_name chroot/build # Make source available in chroot

	old_locale=$LC_ALL
	export LC_ALL=C # no locale errors


	header "Installing build dependencies"
	ij_bdeps="build-essential debhelper automake libtool libcups2-dev libpopt-dev $tiff-dev libpng12-dev libgtk2.0-dev libxml2-dev"
	chroot chroot apt-get -y install $ij_bdeps

	header "Building driver package"
	chroot chroot sh -c "cd /build && dpkg-buildpackage -b"
	ln --target-directory=. chroot/*.deb
	
	trap - EXIT
	rm_mounts
fi

check_pkg() {
	dpkg-query -Wf '${db:Status-Abbrev}' $1 2>/dev/null | grep -q ii
}

# Install packages
header "Installing driver package"
pkg=cnijfilter-mp630series
if ! check_pkg $pkg; then
	if ! ls *.deb >& /dev/null; then
		echo "No packages were built!"; false
	fi
	if ! dpkg -i *.deb >&/dev/null; then
		apt-get -fy install
	fi
fi

if ! check_pkg cups-bjnp; then
	header "Installing network protocol support from PPA"
	apt-add-repository -y ppa:robbiew/cups-bjnp<commit_msg>Use precise-compatible way to check for installed package<commit_after>#!/bin/bash
# Copyright (C) 2012 <NAME> <<EMAIL>>
# Licensing: Simplified BSD License, see LICENSE file


# Nice terminal tools
b=$(tput bold)
n=$(tput sgr0)
header() { printf "\n${b}${1}${n}\n\n"; }

# Unmount on error
bind_mounts="dev dev/pts sys proc"
rm_mounts() {
	header "Cleaning up chroot"
	for mt in $(echo $bind_mounts | tac -s' ') build; do
		umount chroot/$mt || true
		sleep 0.5 # Give it time...
	done
}
trap 'header "Failed!!!"; exit 1' ERR


ij_version=3.00
ij_name="cnijfilter-common-$ij_version"
if apt-cache search -n libtiff5 | grep -q .; then
	tiff=libtiff5
else
	tiff=libtiff4
fi

# Get the source
if [ "$1" != "ROOT" ]; then
	# Available here: http://support-au.canon.com.au/contents/AU/EN/0100160604.html
	ij_src="$ij_name-1.tar.gz"
	ij_uri="http://gdlp01.c-wss.com/gds/6/0100001606/01/$ij_src"
	if [ ! -f $ij_src ]; then
		header "Downloading IJ driver source"
		wget $ij_uri
	fi
	if [ ! -d $ij_name ]; then
		header "Extracting driver source"
		tar xf $ij_src
		
		header "Patching driver"
		for patch in patch/*; do
			echo Applying $(basename $patch)
			patch -d $ij_name -p1 < $patch
		done
		sed -i -e "s/libtiff4/$tiff/g" $ij_name/debian/control
	fi
fi


# Run as root
if [ $UID != 0 ]; then
	header "Becoming root, to build/install packages"
	exec sudo $0 ROOT
fi


# Create a chroot
if ! ls *.deb >& /dev/null; then
	if [ ! -d "chroot/usr" ]; then
		if ! which debootstrap >&/dev/null; then
			header "Installing debootstrap"
			apt-get -y install debootstrap
			apt-mark auto debootstrap # autoremovable
		fi
		suite=$(lsb_release -sc)
		header "Creating chroot"
		debootstrap --verbose --variant=buildd --arch=i386 $suite \
			./chroot $debootstrap_mirror \
			|| (sudo rm -rf chroot; false EndWithError)
	fi

	header "Configuring chroot"
	cp /etc/hosts /etc/resolv.conf chroot/etc/
	# Don't install extra packages
	cat > chroot/etc/apt/apt.conf.d/recommends <<EOF
APT::Install-Recommends "0";
APT::Install-Suggests "0";
EOF

	trap 'rm_mounts' EXIT
	for mt in $bind_mounts; do
		mount -o bind /$mt chroot/$mt
	done
	mkdir -p chroot/build
	mount -o bind $ij_name chroot/build # Make source available in chroot

	old_locale=$LC_ALL
	export LC_ALL=C # no locale errors


	header "Installing build dependencies"
	ij_bdeps="build-essential debhelper automake libtool libcups2-dev libpopt-dev $tiff-dev libpng12-dev libgtk2.0-dev libxml2-dev"
	chroot chroot apt-get -y install $ij_bdeps

	header "Building driver package"
	chroot chroot sh -c "cd /build && dpkg-buildpackage -b"
	ln --target-directory=. chroot/*.deb
	
	trap - EXIT
	rm_mounts
fi

check_pkg() {
	dpkg -l $1 2>/dev/null | grep -q ^ii
}

# Install packages
header "Installing driver package"
pkg=cnijfilter-mp630series:i386
if ! check_pkg $pkg; then
	if ! ls *.deb >& /dev/null; then
		echo "No packages were built!"; false
	fi
	if ! dpkg -i *.deb >&/dev/null; then
		apt-get -fy install
	fi
fi

if ! check_pkg cups-bjnp; then
	header "Installing network protocol support from PPA"
	apt-add-repository -y ppa:robbiew/cups-bjnp
{'fraction_non_alphanumeric': 0.13102776424744278, 'fraction_numerical': 0.02062023055690859, 'mean_word_length': 3.5901639344262297, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '441446', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2353, 'n_words': 699}