#!/bin/bash
# Georg Pfuetzenreuter <mail+opensuse@georg-pfuetzenreuter.net>

set -f

usage() {
	echo "Usage: $0 <command> <file>"
	echo 'Tool will write the command output to stdout and to the file.'
	echo 'If ANSI escape sequences are used in the output, they will be printed but not written.'
	echo 'If the specified output file already exists, it will be overwritten.'
	exit 0
}

if [ "$#" -lt 3 ] 
then
	usage
fi

file="${@:$#}"
cmd="${*%${!#}}"

if ! command -v "$1" >/dev/null
then
	echo "Cannot execute $1."
	exit 1
fi

unbuffer $cmd | pee tee "ansifilter | tee $file >/dev/null"
