#!/usr/bin/env bash
#
#   pkgdelta - create delta files for use with pacman and repo-add
#   Generated from pkgdelta.sh.in; do not edit by hand.
#
#   Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# bash options
set -o errexit

# gettext initialization
export TEXTDOMAIN='pacman-scripts'
export TEXTDOMAINDIR='/usr/share/locale'

declare -r myver='5.0.1'

QUIET=0
USE_COLOR='y'

# minimal of package before deltas are generated (bytes)
min_pkg_size=$((1024*1024))

# percent of new package above which the delta will be discarded
max_delta_size=70


# ensure we have a sane umask set
umask 0022

# getopt-like parser
parseopts() {
	local opt= optarg= i= shortopts=$1
	local -a longopts=() unused_argv=()

	shift
	while [[ $1 && $1 != '--' ]]; do
		longopts+=("$1")
		shift
	done
	shift

	longoptmatch() {
		local o longmatch=()
		for o in "${longopts[@]}"; do
			if [[ ${o%:} = "$1" ]]; then
				longmatch=("$o")
				break
			fi
			[[ ${o%:} = "$1"* ]] && longmatch+=("$o")
		done

		case ${#longmatch[*]} in
			1)
				# success, override with opt and return arg req (0 == none, 1 == required)
				opt=${longmatch%:}
				if [[ $longmatch = *: ]]; then
					return 1
				else
					return 0
				fi ;;
			0)
				# fail, no match found
				return 255 ;;
			*)
				# fail, ambiguous match
				printf "pkgdelta: $(gettext "option '%s' is ambiguous; possibilities:")" "--$1"
				printf " '%s'" "${longmatch[@]%:}"
				printf '\n'
				return 254 ;;
		esac >&2
	}

	while (( $# )); do
		case $1 in
			--) # explicit end of options
				shift
				break
				;;
			-[!-]*) # short option
				for (( i = 1; i < ${#1}; i++ )); do
					opt=${1:i:1}

					# option doesn't exist
					if [[ $shortopts != *$opt* ]]; then
						printf "pkgdelta: $(gettext "invalid option") -- '%s'\n" "$opt" >&2
						OPTRET=(--)
						return 1
					fi

					OPTRET+=("-$opt")
					# option requires optarg
					if [[ $shortopts = *$opt:* ]]; then
						# if we're not at the end of the option chunk, the rest is the optarg
						if (( i < ${#1} - 1 )); then
							OPTRET+=("${1:i+1}")
							break
						# if we're at the end, grab the the next positional, if it exists
						elif (( i == ${#1} - 1 )) && [[ $2 ]]; then
							OPTRET+=("$2")
							shift
							break
						# parse failure
						else
							printf "pkgdelta: $(gettext "option requires an argument") -- '%s'\n" "$opt" >&2
							OPTRET=(--)
							return 1
						fi
					fi
				done
				;;
			--?*=*|--?*) # long option
				IFS='=' read -r opt optarg <<< "${1#--}"
				longoptmatch "$opt"
				case $? in
					0)
						# parse failure
						if [[ $optarg ]]; then
							printf "pkgdelta: $(gettext "option '%s' does not allow an argument")\n" "--$opt" >&2
							OPTRET=(--)
							return 1
						# --longopt
						else
							OPTRET+=("--$opt")
						fi
						;;
					1)
						# --longopt=optarg
						if [[ $optarg ]]; then
							OPTRET+=("--$opt" "$optarg")
						# --longopt optarg
						elif [[ $2 ]]; then
							OPTRET+=("--$opt" "$2" )
							shift
						# parse failure
						else
							printf "pkgdelta: $(gettext "option '%s' requires an argument")\n" "--$opt" >&2
							OPTRET=(--)
							return 1
						fi
						;;
					254)
						# ambiguous option -- error was reported for us by longoptmatch()
						OPTRET=(--)
						return 1
						;;
					255)
						# parse failure
						printf "pkgdelta: $(gettext "invalid option") '--%s'\n" "$opt" >&2
						OPTRET=(--)
						return 1
						;;
				esac
				;;
			*) # non-option arg encountered, add it as a parameter
				unused_argv+=("$1")
				;;
		esac
		shift
	done

	# add end-of-opt terminator and any leftover positional parameters
	OPTRET+=('--' "${unused_argv[@]}" "$@")
	unset longoptmatch

	return 0
}

plain() {
	(( QUIET )) && return
	local mesg=$1; shift
	printf "${BOLD}    ${mesg}${ALL_OFF}\n" "$@" >&1
}

msg() {
	(( QUIET )) && return
	local mesg=$1; shift
	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
}

msg2() {
	(( QUIET )) && return
	local mesg=$1; shift
	printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
}

ask() {
	local mesg=$1; shift
	printf "${BLUE}::${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&1
}

warning() {
	local mesg=$1; shift
	printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

error() {
	local mesg=$1; shift
	printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

if [[ -n "$MSYSTEM" ]]; then
  readonly -a UTILS_NAME=('bsdtar'
                          'bzip2'
                          'bzr'
                          'cat'
                          'ccache'
                          'distcc'
                          'git'
                          'gpg'
                          'gzip'
                          'hg'
                          'lzip'
                          'lzop'
                          'openssl'
                          'svn'
                          'tput'
                          'uncompress'
                          'upx'
                          'xargs'
                          'xz'
                         )

  for wrapper in ${UTILS_NAME[@]}; do
    eval "
    ${wrapper}"'() {
      local UTILS_PATH="/usr/bin/"
      if ! type -p ${UTILS_PATH}${FUNCNAME[0]} >/dev/null; then
        error "$(gettext "Cannot find the %s binary required for makepkg.")" "${UTILS_PATH}${FUNCNAME[0]}"
        exit 1
      fi
      ${UTILS_PATH}${FUNCNAME[0]} "$@"
    }'
  done
fi


# print usage instructions
usage() {
	printf "pkgdelta (pacman) %s\n" "${myver}"
	echo
	printf -- "$(gettext "Usage: pkgdelta [options] <package1> <package2>\n")"
	echo
	printf -- "$(gettext "\
pkgdelta will create a delta file between two packages.\n\
This delta file can then be added to a database using repo-add.\n")"
	echo
	printf -- "$(gettext "Example:  pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")\n"
	echo
	printf -- "$(gettext "Options:\n")"
	printf -- "$(gettext "  -q, --quiet       minimize output\n")"
	printf -- "$(gettext "  --nocolor         remove color from output\n")"
	printf -- "$(gettext "  --min-pkg-size    minimum package size before deltas are generated\n")"
	printf -- "$(gettext "  --max-delta-size  percent of new package above which the delta will be discarded\n")"
}

version() {
	printf "pkgdelta (pacman) %s\n" "$myver"
	printf -- "$(gettext "\
Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}

human_to_size() {
  awk -v human="$1" '
  function trim(s) {
    gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
    return s
  }

  function parse_units(units) {
    if (!units || units == "B")
      return 1
    if (match(units, /^.iB$/))
      return 1024
    if (match(units, /^.B$/))
      return 1000
    if (length(units) == 1)
      return 1024

    # parse failure: invalid base
    return -1
  }

  function parse_scale(s) {
    return index("BKMGTPE", s) - 1
  }

  function isnumeric(string) {
    return match(string, /^[-+]?[[:digit:]]*(\.[[:digit:]]*)?/)
  }

  BEGIN {
    # peel off the leading number as the size, fail on invalid number
    human = trim(human)
    if (isnumeric(human))
      size = substr(human, RSTART, RLENGTH)
    else
      exit 1

    # the trimmed remainder is assumed to be the units
    units = trim(substr(human, RLENGTH + 1))

    base = parse_units(units)
    if (base < 0)
      exit 1

    scale = parse_scale(substr(units, 1, 1))
    if (scale < 0)
      exit 1

    printf "%d\n", size * base^scale + (size + 0 > 0 ? 0.5 : -0.5)
  }'
}


isnumeric() {
	[[ $1 != *[!0-9]* ]]
}

read_pkginfo() {
	unset pkgver pkgname arch
	while IFS='=' read -r field value; do
		# skip comments and invalid lines
		[[ $field = '#'* || -z $value ]] && continue

		# skip lines which aren't fields we care about
		[[ $field != @(pkgver|pkgname|arch) ]] || continue

		declare -g "${field% }=${value# }"

		[[ $pkgname && $pkgver && $arch ]] && return 0
	done < <(bsdtar -xOqf "$1" .PKGINFO 2>/dev/null)

	error "$(gettext "Invalid package file '%s'.")" "$1"
	return 1
}

# $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
create_xdelta()
{
	local oldfile=$1
	local newfile=$2
	local \
	oldname oldver oldarch \
	newname newver newarch \
	deltafile

	read_pkginfo "$oldfile" || return 1
	oldname="$pkgname"
	oldver="$pkgver"
	oldarch="$arch"
	read_pkginfo "$newfile" || return 1
	newname="$pkgname"
	newver="$pkgver"
	newarch="$arch"

	pkgsize="$(stat -c %s -L "$newfile")"

	if ((pkgsize < min_pkg_size)); then
		msg "$(gettext "Skipping delta creation for small package: %s - size %s")" "$newname" "$pkgsize"
		return 0
	fi

	if [[ $oldname != "$newname" ]]; then
		error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
		return 1
	fi

	if [[ $oldarch != "$newarch" ]]; then
		error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
		return 1
	fi

	if [[ $oldver == "$newver" ]]; then
		error "$(gettext "Both packages have the same version : '%s'")" "$newver"
		return 1
	fi

	msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
	deltafile=$(dirname "$newfile")/$pkgname-${oldver}_to_${newver}-$arch.delta
	local ret=0

	xdelta3 -q -f -9 -S lzma -s "$oldfile" "$newfile" "$deltafile" || ret=$?
	if (( ret )); then
		error "$(gettext "Delta could not be created.")"
		return 1
	fi

	deltasize="$(stat -c %s -L "$deltafile")"

	if ((max_delta_size * pkgsize / 100 < deltasize)); then
		msg "$(gettext "Delta package larger than maximum size. Removing.")"
		rm -f "$deltafile"
		return 0
	fi

	msg "$(gettext "Generated delta : '%s'")" "$deltafile"
	(( QUIET )) && echo "$deltafile"

	return 0
}

OPT_SHORT='hqV'
OPT_LONG=('help' 'quiet' 'max-delta-size:' 'min-pkg-size:' 'nocolor' 'version')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
	exit 1
fi
set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPTRET

# determine whether we have gettext; make it a no-op if we do not
if ! type -p gettext >/dev/null; then
	gettext() {
		printf "%s\n" "$@"
	}
else
	gettext() {
		/usr/bin/gettext "$@"
	}
fi

# parse arguments
while :; do
	case $1 in
		-h|--help)
			usage
			exit 0 ;;
		-V|--version)
			version
			exit 0 ;;
		-q|--quiet)
			QUIET=1;;
		--nocolor)
			USE_COLOR='n';;
		--min-pkg-size)
			if ! min_pkg_size=$(human_to_size "$2"); then
				echo "invalid argument '$2' for option -- '$1'"
				exit 1
			fi
			shift ;;
		--max-delta-size)
			arg=$(awk -v val="$2" 'BEGIN { print val * 100 }')
			if ! isnumeric "$arg" || (( arg > 200 )); then
				echo "invalid argument '$2' for option -- '$1'"
				exit 1
			fi
			max_delta_size=$arg
			shift ;;
		--)
			shift
			break 2 ;;
	esac
	shift
done

# check if messages are to be printed using color
unset ALL_OFF BOLD BLUE GREEN RED YELLOW
if [[ -t 2 && ! $USE_COLOR = "n" ]]; then
	# prefer terminal safe colored and bold text when tput is supported
	if tput setaf 0 &>/dev/null; then
		ALL_OFF="$(tput sgr0)"
		BOLD="$(tput bold)"
		BLUE="${BOLD}$(tput setaf 4)"
		GREEN="${BOLD}$(tput setaf 2)"
		RED="${BOLD}$(tput setaf 1)"
		YELLOW="${BOLD}$(tput setaf 3)"
	else
		ALL_OFF="\e[1;0m"
		BOLD="\e[1;1m"
		BLUE="${BOLD}\e[1;34m"
		GREEN="${BOLD}\e[1;32m"
		RED="${BOLD}\e[1;31m"
		YELLOW="${BOLD}\e[1;33m"
	fi
fi
readonly ALL_OFF BOLD BLUE GREEN RED YELLOW


if (( $# != 2 )); then
	usage
	exit 1
fi

for i in "$@"; do
	if [[ ! -f $i ]]; then
		error "$(gettext "File '%s' does not exist")" "$i"
		exit 1
	fi
done

if ! type xdelta3 &>/dev/null; then
	error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
	exit 1
fi

create_xdelta "$@"

# vim: set noet:
