#!/usr/bin/env bash
# Copyright 2006-2010 Gentoo Foundation; Distributed under the GPL v2

# <grobian@gentoo.org> -- 2009-04-04
# added -E flag to enable the previously default behaviour to inject
# EAPI=prefix, this coincides with EAPI=prefix being dropped in our tree
# since this was the main effort of this script, its arguments -f
# (force replacement) is void and ignored for backwards compatability
# removed the very old EDEST and PROOT legacy removal code, it should
# not be used anywhere any more
# <grobian@gentoo.org> -- 2007-11-08
# changed EAPI injection to extend for prefix in case a main tree EAPI
# was defined
# <grobian@gentoo.org> -- 2006-12-21
# added -e flag to indicate to the script that the given file(s) should
# be treated as ebuilds
# <grobian@gentoo.org> -- 2006-11-03
# time to get more careful about what we do.  Don't replace anything if
# an ebuild is already EAPI="prefix", unless -f flag is given to
# "force".  Only run legacy cleanup stuff when using -f.  A special case
# is when the file to process is an eclass; in that case we run the
# normal replace rules, regardless.
# <grobian@gentoo.org> -- 2006-10-24
# because portage now deals differently (saner) with ebuilds, we replace
# all ${EDEST} with ${D} again as "cleanup" action.  Instead we look at
# all ${D} not in a make call and ${ROOT} variables.
# <grobian@gentoo.org> -- 2006-09-22
# eapify is a small hackish script that usually does fine in adding
# EAPI="prefix", or extending if there is already a EAPI=something line
# in the ebuild, if -E flag is given.  It transforms ${ROOT} into
# ${EROOT}.  Next to this, it applies ${D} -> ${ED} transformations to
# all but "make DESTDIR=${D} install".  Because of the nature of this
# script, running it multiple times should not harm.  It might, however,
# do wrong things, so always check the result.  You have been warned.

doeapi=0
isebuild=0
while [[ ! -z $1 ]] ; do
	case $1 in
		-f)
			# ignored for backwards compatability
			# nothing to force any more since we can't check if we
			# already eapified it, and the real old legacy bits are
			# removed
			:
		;;
		-e)
			doeapi=0
			isebuild=1
		;;
		-E)
			doeapi=1
		;;
		*)
			[[ -z $files ]] \
				&& files=$1 \
				|| files="$files $1"
		;;
	esac
	shift
done

# default to all ebuilds if no files given
[[ -z $files ]] && files="*.ebuild"


replace() {
	echo -n "  replacing $1 -> $2 ... "
	local cnt=$(sed -e "${4:-s}|$1|$2|g" "$3" | diff "$3" - | grep -o "$2" | wc -l)
	if [[ $cnt > 0 ]];
	then
		sed -i -e "${4:-s}|$1|$2|g" "$3"
		echo -n "$cnt occurence"
		[[ $cnt == 1 ]] \
			&& echo "" \
			|| echo "s"
	else
		echo "not found"
	fi
}

for f in $files ; do
	if [[ ! -f $f ]] ; then
		echo "no such file: $f"
		exit -1
	fi

	echo "Processing $f"

	if [[ ${f##*.} != "eclass" && ${f##*.} != "ebuild" && ${isebuild} != 1 ]] ; then
		# don't try anything on arbitrary files
		continue
	fi

	if [[ ${doeapi} != 0 ]] ; then
		unset EAPI
		echo -n "  EAPI ... "
		eval "$(egrep "^EAPI=" "$f")"

		if [[ ${EAPI} == *prefix* ]] ; then
			echo "already $EAPI"
		elif [[ -n ${EAPI} ]] ; then
			sed -i \
				-e '/^EAPI=/cEAPI="prefix '"${EAPI}"'"' \
				"$f"
			echo 'extended to EAPI="prefix '"${EAPI}"'"'
		else
			sed -i \
				-e '/^# \$Header:.*$/a\\nEAPI="prefix"' \
				"$f"
			echo 'added EAPI="prefix"'
		fi
	fi

	# we replace all ${D} with ${ED} if not in a make ... install line
	replace '${D}' '${ED}' "$f" '/make.*install/!s'

	# replace ${ROOT} with ${EROOT}
	replace '${ROOT}' '${EROOT}' "$f"
done
