Justin Vreeland 794d92
#!/bin/sh
Justin Vreeland 794d92
# Reads filenames on stdin, xz-compresses each in place.
Justin Vreeland 794d92
# Not optimal for "compress relatively few, large files" scenario!
Justin Vreeland 794d92
Justin Vreeland 794d92
# How many xz's to run in parallel:
Justin Vreeland 794d92
procgroup=""
Justin Vreeland 794d92
while test "$#" != 0; do
Justin Vreeland 794d92
	# Get it from -jNUM
Justin Vreeland 794d92
	N="${1#-j}"
Justin Vreeland 794d92
	if test "$N" = "$1"; then
Justin Vreeland 794d92
		# Not -j<something> - warn and ignore
Justin Vreeland 794d92
		echo "parallel_xz: warning: unrecognized argument: '$1'"
Justin Vreeland 794d92
	else
Justin Vreeland 794d92
		procgroup="$N"
Justin Vreeland 794d92
	fi
Justin Vreeland 794d92
	shift
Justin Vreeland 794d92
done
Justin Vreeland 794d92
Justin Vreeland 794d92
#This seems to cause problems with large numbers
Justin Vreeland 794d92
if (( $procgroup > 6 )); then
Justin Vreeland 794d92
	procgroup=6
Justin Vreeland 794d92
fi
Justin Vreeland 794d92
Justin Vreeland 794d92
# If told to use only one cpu:
Justin Vreeland 794d92
test "$procgroup" || exec xargs -r xz
Justin Vreeland 794d92
test "$procgroup" = 1 && exec xargs -r xz
Justin Vreeland 794d92
Justin Vreeland 794d92
# xz has some startup cost. If files are really small,
Justin Vreeland 794d92
# this cost might be significant. To combat this,
Justin Vreeland 794d92
# process several files (in sequence) by each xz process via -n 16:
Justin Vreeland 794d92
exec xargs -r -n 16 -P $procgroup xz