#!/bin/sh
# public domain
# Rewritten in sh

case ${1--h} in
-h)
	;&
--help)
	echo "wrap 2.0 - Word wrap files."
	echo
	echo "Usage:"
	echo "$0 [-w width] filenames"
	exit 0
	;;
-w)
	width=$2
	shift 2
	;;
esac

while [ "$#" -gt "0" ]; do
	if [ ! -f "$1" ]; then
		echo "$1 does not exist.  Skipping it..."
		shift
		continue
	elif ! file "$1" | grep -q ':.*text'; then
		echo "$1 appears not to be text."
		echo "Are you sure you want to wrap this ((Y/N)?"
		read answer
		case answer in
[yY])
			;;
*)
			shift
			continue
			;;
		esac
	elif [ ! -w "$1" ]; then
		echo "$1 is not writeable.  Skipping it..."
		shift
		continue
	fi
	mv $1 $1.bak
	fold ${width+-w $width} $1.bak > $1
	echo "$1 wrapped."
	shift
done
