#! /bin/sh

# specialist: assist creation of special files.

PATH=/bin:/usr/bin:/usr/local/bin; export PATH

SCRIPT=`basename "$0"`
INPUT_FORMAT=${INPUT_FORMAT:=path}

usage() {
    echo "usage: ${SCRIPT} [ -T ]" 1>&2
    exit 1
}

die() {
    msg=$*

    echo "${msg}" 1>&2
    exit 2
}

# create a special transcript line for the given path.
specialize() {
    local path="$1"

    [ -n "${path}" -a -f "${path}" ] || die "Invalid path: ${path}"

    fsdiff -1 -c sha1 "${path}"
    return $?
}

specialize_transcript() {
    local path=""
    status=0

    while read type path remainder; do
	if [ x"${type}" != x"f" ]; then
	    continue
	fi

	specialize "${path}"
    done
}

specialize_paths() {
    local path=""
    status=0

    while read path; do
	specialize "${path}"
	if [ $? -ne 0 ]; then
	    status=1
	fi
    done

    return "${status}"
}

while getopts T opt; do
    case $opt in
    T)
	INPUT_FORMAT="transcript"
	;;

    *)
	usage
	;;
	
    esac
done
shift $((OPTIND - 1))

if [ x"${INPUT_FORMAT}" = x"transcript" ]; then
    specialize_transcript
else
    specialize_paths
fi

exit $?
