#! /usr/bin/perl

use strict;
use vars qw($header $bookmarks $tmpfile $bakfile $buf $l $atime $mtime
	    $uid $gid);

require "jcode.pl";

$header     = "/home/myname/lib/win-bookmark.head";
$bookmarks  = "/home/myname/public_html/profile/bookmarks";
$tmpfile    = "${bookmarks}.tmp";
$bakfile    = "${bookmarks}.bak";
$uid = 65534;
$gid = 65534;

($atime, $mtime) = (stat($bookmarks))[8,9];

open(OUT, ">${tmpfile}") or die "Error: Can't create ${tmpfile}: $!\n";
open(IN, $header) or die "Error: Can't open ${header}: $!\n";
while (defined($buf = <IN>)) {
    print OUT $buf;
}
close(IN);
open(IN, "$bookmarks") or die "Error: Can't open ${bookmarks}: $!\n";
$l = 0;       
while (defined($buf = <IN>)) {
    next if (++$l <= 4);
    chop $buf if $buf =~ /\n$/;
    chop $buf if $buf =~ /\r$/;
    chop $buf if $buf =~ /\r$/;
    &jcode::euc2sjis(\$buf);
    print OUT $buf, "\r\r\n";
}
close(IN);
close(OUT);
utime($atime, $mtime, $tmpfile);
chown($uid, $gid, $tmpfile);
rename($bookmarks, $bakfile) && rename($tmpfile, $bookmarks);
