#!/bin/dash
VERSION=1.4.5
NAME=`basename $0`
NV="$NAME $VERSION: print a series of equally separated integers or floats"
LICENSE="Copyright (C) 2006, 2007, 2009, 2011-2012 Dimitar Ivanov

License: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."

################################################################################

ofmt="%.6g"
sep=`echo |awk '{printf( "%080s", 0 )}' |tr 0 -`

show_help()
{
  cat << EOH && exit
$sep
$NV
$sep

Usage: $NAME <begin> <end> [<step>] [<print_fmt>]
   or: $NAME <number>

EOH
}

show_version()
{
  cat << !ver && exit
$NAME $VERSION
$LICENSE
!ver
}

case $1 in
         -h|"") show_help
             ;;
        --help) sep=""
                NV="$NAME produces a series of equally separated integers or floats"
                show_help
             ;;
     --version) show_version
             ;;
            -[-a-zA-Z]*) exec 1>&2
                echo "$NAME: invalid option '$1'"
                echo "Try \`$NAME -h' for help."
                exit 2
             ;;
esac

case $# in
     1) b=0;  e=$1; d=1;
     ;;
     2) b=$1; e=$2; d=1;
     ;;
     3) b=$1; e=$2; d=$3;
     ;;
     4) b=$1; e=$2; d=$3; ofmt=$4;
     ;;
     *) exit 1
     ;;
esac

sign=`echo $d \
      |awk 'BEGIN { if( $1 == 0 ) exit }
            { 
              if( $1 > 0 ) printf "+"
              if( $1 < 0 ) printf "-"
            }'
`
case $sign in
           +) cop='<='
           ;;
           -) cop='>='
           ;;
           *) echo "($NAME) error: step must be different than zero"; exit 2
           ;;
esac

              # Use e1 because of round-off errors with floating numbers
echo |awk "BEGIN { e1 = $e + ($d/10) }
           { 
             i = $b
             while( i ${cop} e1 )
             {
               printf( \"$ofmt\n\", i )
               i = i + ($d)
             }
           }"
