#!/bin/sh

set -e

which rpm2cpio >/dev/null || (echo 'FAIL: rpm2cpio not in $PATH' ; exit 1)
me=$(basename $0)

usage () {
	echo "${me}"
	echo "Extract Adobe ICC profiles and installs them into ~/.color/icc"
	echo "Usage: ${me} [-S] profilesfile"
	echo "  -S    systemwide installation"
	echo "The Adobe ICC profiles can be downloaded at"
	echo "http://www.adobe.com/digitalimag/adobergb.html"
	echo
}

if [ x"${1}" = "x-h" ] ; then
	usage
	exit 0
fi

installdir="${HOME}/.color/icc"
system=0
if [ x"${1}" = "x-S" ] ; then
	installdir="/usr/local/share/color/icc"
	system=1
	shift
fi

if [ x"${1}" = x -o ! -r "${1}" ] ; then
	usage
	exit 1
fi

TDIR="${TEMP:-/var/tmp}"
WDIR=$(mktemp -d "${TDIR}"/profiles.XXXXXX)

gzip -dc "$1" | tar -C "${WDIR}" -xf -
cd "${WDIR}"
rpm2cpio adobe-color-profiles-1.0-1.noarch.rpm | cpio -i -d -m
find usr -type f -print0 | xargs -0 chmod 0640
find usr -type d -print0 | xargs -0 chmod 0750
mkdir -p "${installdir}"
cd usr/share/color/icc
cp -r "Adobe ICC Profiles" "${installdir}"
if [ $system ] ; then
	chmod -R a+rX "${installdir}"
fi

cd
rm -rf "${WDIR}"
echo "Profiles installed successfully"
