#!/bin/sh
#
# This preprocesses a set of word lists into a suitable form for input
# into cracklib-packer
#
# Truncates lines longer than 1022 characters long as cracklib-packer
# does not handle them correctly.
#
# The last part of the pipeline uses 'grep -v' to remove any blank
# lines (possibly introduced by earlier parts of the pipeline) as
# cracklib-packer will generate "skipping line" warnings otherwise.
#
LC_ALL=C
export LC_ALL

case "$@" in
*.gz)
	CMD="gzip"
	ARGS="-cdf"
	;;
*.bz2)
	CMD="bzcat"
	ARGS=""
	;;
*.xz)
	CMD="xzcat"
	ARGS=""
	;;
*)
	CMD="gzip"
	ARGS="-cdf"
	;;
esac

$CMD $ARGS "$@" |
    grep -a -v '^#' |
    tr '[A-Z]' '[a-z]' |
    tr -cd '\n[:graph:]' |
    cut -c 1-1022 |
    grep -v '^$' |
    sort -u
