summaryrefslogtreecommitdiffstats
path: root/bin/ecompress
blob: 046e546fbd808885d192db8e0ed4e2e868200895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: prepman 5507 2007-01-10 04:22:27Z zmedico $

source "${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"/isolated-functions.sh

if [[ -z $1 ]] ; then
	vecho "${0##*/}: at least one argument needed" 1>&2
	exit 1
fi

# setup compression stuff
PORTAGE_COMPRESS=${PORTAGE_COMPRESS-bzip2}
[[ -z ${PORTAGE_COMPRESS} ]] && exit 0

if [[ ${PORTAGE_COMPRESS_FLAGS+set} != "set" ]] ; then
	case ${PORTAGE_COMPRESS} in
		bzip2|gzip)  PORTAGE_COMPRESS_FLAGS="-9";;
	esac
fi

case $1 in
	--suffix)
		[[ -n $2 ]] && vecho "${0##*/}: --suffix takes no additional arguments" 1>&2

		set -e
		tmpdir="${T}"/.ecompress$$.${RANDOM}
		mkdir "${tmpdir}"
		cd "${tmpdir}"
		# we have to fill the file enough so that there is something
		# to compress as some programs will refuse to do compression
		# if it cannot actually compress the file
		echo {0..1000} > compressme
		${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS} compressme > /dev/null
		suffix=$(ls compressme*)
		suffix=${suffix#compressme}
		cd /
		rm -rf "${tmpdir}"
		echo "${suffix}"
		;;
	--bin)
		[[ -n $2 ]] && vecho "${0##*/}: --bin takes no additional arguments" 1>&2

		echo "${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS}"
		;;
	--queue)
		shift
		exec touch "${@/%/.ecompress.file}"
		;;
	--dequeue)
		[[ -n $2 ]] && vecho "${0##*/}: --dequeue takes no additional arguments" 1>&2
		find "${D}" -name '*.ecompress.file' -print0 \
			| sed -e 's:\.ecompress\.file::g' \
			| ${XARGS} -0 ecompress
		find "${D}" -name '*.ecompress.file' -print0 | ${XARGS} -0 rm -f
		;;
	--*)
		vecho "${0##*/}: unknown arguments '$*'" 1>&2
		exit 1
		;;
	*)
		# If a compressed version of the file already exists, simply
		# delete it so that the compressor doesn't whine (bzip2 will
		# complain and skip, gzip will prompt for input)
		suffix=$(ecompress --suffix)
		[[ -n ${suffix} ]] && rm -f "${@/%/${suffix}}"
		# Finally, let's actually do some real work
		exec "${PORTAGE_COMPRESS}" ${PORTAGE_COMPRESS_FLAGS} "$@"
		;;
esac