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