#!/bin/sh

#################################################
## line v1.312                                 ##
## convert all spaces, tabs, carriage returns  ##
## and newlines to a new line. multiple        ##
## spaces, tabs and newlines become a single   ##
## newline.                                    ##
##                                             ##
## line can take input from a file (as a       ##
## command-line argument) or from standard     ##
## input. each token from the input file is    ##
## separated from other tokens by a single     ##
## newline. a token is considered any          ##
## character or string separated by one or     ##
## more spaces, tabs newlines or carriage      ##
## returns.                                    ##
##                                             ##
## if there are one or more tokens in the      ##
## input, the output will start with a token   ##
## and end with a newline immediately after    ##
## the last token.                             ##
##                                             ##
## Copyright (c) 13 May 2003 Atom Emet         ##
## Atom*Business-PHP.com                       ##
##                                             ##
## This program is free software; you can      ##
## redistribute it and/or modify it under the  ##
## terms of the GNU General Public License as  ##
## published by the Free Software Foundation;  ##
## either version 2 of the License, or         ##   
## (at your option) any later version.         ##
##                                             ##
## This program is distributed in the hope     ##
## that it will be useful, but WITHOUT ANY     ##
## WARRANTY; without even the implied warranty ##
## of MERCHANTABILITY or FITNESS FOR A         ##
## PARTICULAR PURPOSE. See the GNU General     ##
## Public License for more details.            ##
##                                             ##
## You should have received a copy of the      ##
## GNU General Public License along with this  ##
## program; if not, write to                   ##
##    the Free Software Foundation, Inc.       ##
##    59 Temple Place - Suite 330              ##
##    Boston, MA  02111-1307, USA              ##
#################################################

########################################
## allow either zero or one arguments ##
########################################
if [ "${#}" -gt '1' ]
then
	###############################################
	## if more than one argument is given,       ##
	## echo an error message into STANDARD ERROR ##
	## and exit leaving an exit status "1"       ##
	###############################################
	echo 'line: One file at a time' >&2
	exit 1
fi

#######################################################
## check if an argument is given on the command line ##
#######################################################
if [ "${#}" = '1' ]
then
	########################################################
	## if there's an argument, check that the file exists ##
	########################################################
	if [ -e "${1}" ]
	then
	    ###########################################################
	    ## if the file specified on the command line does exist, ##
	    ## input comes from that file                            ##
	    ###########################################################
	    cat "${1}" | tr -s ' \t\n' '\n' | egrep -v '^$'
	    exit 0
	else
	    ###############################################################
	    ## if the file specified on the command line does not exist, ##
	    ## echo an error message into STANDARD ERROR                 ##
	    ## and exit leaving an exit status "2"                       ##
	    ###############################################################
	    echo "line: No such file or directory \"${1}\"" >&2
	    exit 2
	fi
else
	##################################################
	## if no file is specified on the command line, ##
	## then input comes from STANDARD INPUT         ##
	##################################################
        tr -s ' \t\n\r' '\n' | egrep -v '^$'
	exit 0
fi