#!/usr/bin/perl
#
# Copyright (C) 2009 Germain Garand <germain@ebooksfrance.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this library; see the file COPYING.LIB.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

use Getopt::Std;
use File::Copy;
use File::Temp qw|tempfile|;

getopts('xit:', \%o) or die;

$ARGV[0] or die "doctype [-t type] [-xi] filename
\tAdd or remove or replace doctype declaration from filename.
\tOptions:
\t\t-t type: with type in trans(itional)|loo(se), frame(set), str(ict). Default is 'strict'.
\t\t-x: use xhtml doctypes. Default uses HTML.
\t\t-i: replace input file. Default prints to stdout.
";

open (IN, $ARGV[0]) or die $!;

if (exists  $o{'i'} ) {
    ($fh, $f) = tempfile() if exists $o{'i'};
} else {
    $fh = \*STDOUT;
}

while (<IN>) {

if (1..1) {
    while (/^\s*$/) { $_ = <IN> };
    
    if (/<!DOCTYPE/i) {
         chomp;
         while (!s/<!DOCTYPE[^>]*>//i) {
              $_ .= <IN>;
              chomp;
         }
         $found++
    }
    if ( not $found or exists $o{'t'} ) {
        if ($o{'t'} =~ /^(tran|loo)/i) {
            $_ = (exists $o {'x'} ? 
               '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
                 :
               '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' ) . "\n" . $_;
        } elsif ($o{'t'} =~ /^frame/i) {      
            $_ = (exists $o {'x'} ?
               '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
                 :
               '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">' ) . "\n" . $_;
        } elsif ($o{'t'} =~ /^str/i or not $found) {
            $_ = (exists $o {'x'} ?
               '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
                 :                 
               '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' ) . "\n" . $_;
        }
    } 
}
print $fh $_

}

close;
do { move($f, $ARGV[0]) ; chmod 0644, $ARGV[0] } if exists  $o{'i'}
