# dos2unix - A program to take a cr/lf delimited ASCII file
# (from DOS, OS/2 or Windows) and change the line delimiters
# to only lf used by Unix. Rick Smereka, Copyright (C) 2006.

# 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, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# Program syntax:

#    dos2unix inputfile outputfile

# Initial tcl version. Aug/2006, Rick Smereka

# required path to library modules
set TCLDIR /home/rick/gpl/tcl

source $TCLDIR/lib/stdhead.tcl

const VERSION "1.4.01-2006.08.13"
const APNAME "dos2unix"

proc usage {} {
   global APNAME

   puts "$APNAME - A program to convert a DOS/Windows ASCII file for"
   puts "          use by Linux/Unix/BSD\n"
   puts "usage: $APNAME infile outfile"
   puts "       where 'infile' is the name of the input file"
   puts "       and 'outfile' is the name of the converted output file."
}

# program main line
# unbuffered stdout
fconfigure stdout -buffering none

if {$argc == 0} {
   usage
   exit
}

if {$argc < 2} {
   puts "$APNAME:incorrect number of parameters"
   usage
   exit
}

# display logo
puts "$APNAME for $tcl_platform(platform) Version $VERSION"
puts "by Rick Smereka, Copyright (c) 2006"
puts "$APNAME comes with ABSOLUTELY NO WARRANTY"
puts "This is free software, and you are welcome to redistribute it"
puts "under certain conditions; see 'gpl.txt' for information."
set fin [lindex $argv 0]
set fout [lindex $argv 1]

if {[catch {open $fin r} in]} {
   puts "$APNAME:unable open input file '$fin'"
   exit
}

if {[catch {open $fout w} out]} {
   puts "$APNAME:unable open output file '$fout'"
   close $in
   exit
}

# if we are running under Windows, set the channel to
# binary translation
if {$tcl_platform(platform) == "windows"} {
   fconfigure $out -translation binary
}

# ascii number of the line feed character and carriage return
set lf 10
set cr 13

puts -nonewline "converting, please wait..."
set line [get_rec $in]

while {![eof $in]} {
   set len [string length $line]

   if {$tcl_platform(platform) == "unix"} {
      # if running on Linux/BSD/Unix machine, just write out with
      # standard text delimiter after removing extra carriage return

      if {$len} {
         set ch [string index $line end]
         set cmp [format "%c" $cr]

         if {$ch == $cr} {
            set line [string range 0 [expr $len - 1]]
	 }
      }

      puts $out $line
   } else {
      puts -nonewline $out [format "%s%c" $line $lf]
   }

   set line [get_rec $in]
}

close $in
close $out
puts "\nprogram complete"
exit
