#!/usr/bin/local/perl

###############################################################################
###############################################################################
#
# To convert linbreaks in a text file to MS-DOS/Windows format
#
#			Version 1
#
#	by Andrew Hardwick, http://duramecho.com, 2002/4/11
#
#	Released under GNU Public Licence.
#
###############################################################################
###############################################################################
#
# How To Use:
#
# Run from a command line with the file name (path) as arguement.
#
###############################################################################
###############################################################################

# Include libraries
use Cwd;		# To find current directory
use strict;		# Disenable automatic variables

my ($Text);
my ($TextUnix,$TextMac,$TextDos);
my ($CountUnix,$CountMac,$CountDos);
# Get data from file
unless($ARGV[0])
	{	die("Error: file name not specified to read.");}
unless(open(FILE,'+<'.$ARGV[0]))
	{	die("Error: Cannot open $ARGV[0] to read.");}
binmode FILE;
read FILE,$Text,-s $ARGV[0];
# Convert linebreaks trying different possibilities & counting number found
$CountUnix=(($TextUnix=$Text)=~s/\x0A/\x0D\x0A/g);
$CountMac=(($TextMac=$Text)=~s/\x0D/\x0D\x0A/g);
$CountDos=(($TextDos=$Text)=~s/\x0D\x0A/\x0D\x0A/g);
# Check which file format it was in
if(!$CountUnix&&!$CountMac&&!$CountDos)
{	die("Error: No linebreaks found.\n");}
elsif($CountUnix&&!$CountMac&&!$CountDos)
{	print "Converted from Unix format.\n";
	$Text=$TextUnix;}
elsif($CountMac&&!$CountUnix&&!$CountDos)
{	print "Converted from Mac format.\n";
	$Text=$TextMac;}
elsif($CountDos&&$CountUnix==$CountDos&&$CountMac==$CountDos)
{	die("It is already in DOS format.\n");}
else
{	die("Error: It appears to be in mixed format. It might not be text.\n");}
# Write data to file
seek FILE,0,0;
print FILE $Text;
close FILE;

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

