#!/bin/sh

# Recurse.bash
# Chieh Cheng
# 2007-02-02

# GNU General Public License (GPL), Version 2, June 1991

  scriptName=`echo $0 | sed s/\\\\/.*\\\\///g`

  usage ()
  {
    echo "  Usage:   $scriptName \"infile\" \"outfile\""
  }

  if [ $# -eq 2 ]
  then
    infile="$1"
    outfile="$2"

    if [ -f "${outfile}" ]
    then
      echo "${outfile} already exists."
    else
      delimiter="__${scriptName}__"
      count=0

      for line in `cat "${infile}" | sed "s/ /${delimiter}/g"`
      do
        lines[${count}]=`echo "${line}" | sed "s/${delimiter}/ /g"`
        count=`expr ${count} + 1`
      done

      count=`expr ${count} - 1`
      while [ $count -ge 0 ]
      do
        echo "${lines[$count]}" >> "${outfile}"
        count=`expr ${count} - 1`
      done
    fi
  else
    usage
  fi
