#!/usr/pkg/bin/perl
################################################################
# upload.pl : サーバにアップロードする
#
# (*) Net::FTP, Net::Netrc が必要です。
#     まだテスト版です
# $Id: upload.pl,v 1.4 2000/10/01 14:30:29 tom Exp $
################################################################

use strict;
use lib qw(lib);
use File::Find;

use Net::FTP;                # CPAN
use Net::Netrc;

use TDS::System;

use vars qw($FTP_SERVER);

require './conf.ph';

# 設定
$FTP_Server = "triones.morito.mgmt.waseda.ac.jp";
my $time_stamp_file = "upload.stamp";

my %mode = (static=>$TDS::System::StaticMode,
	    cgi=>$TDS::System::CanUseCGI);

################################################################
my $diary_dir = $TDS::System::DiaryDir;

my %FilesTable = ('cgi'=> [findfile($diary_dir, ["tdf", "dat", "txt"]),
			  <image/*>,
			  <*skelton.html>],
		 'static'=>["$TDS::System::RecentHtml",
			    <d1*.html>, <d2*.html>,
			    'summary.html', 'dictionary.html',
			    'url.html']);

my $time_stamp_lm = (stat($time_stamp_file))[9];

# put するファイル群を取得
my @files;
for (keys %FilesTable){
    next unless $mode{$_};
    for (@{$FilesTable{$_}}){
	chomp;
	push(@files, $_);
    }
}

for (@files){
    my $file = $_;
#    next if $file =~ m!/CVS$!;
#    print "$file, ";
    my $lm = (stat($file))[9];
    if ($lm > $time_stamp_lm){
	print "$file, $lm\n";
    }
}

{
    # サーバに put
    my $ftp = new Net::FTP($FTP_Server);
    $ftp->login;
    $ftp->cd("tmp");
    for (@files){
#	$ftp->put($_);
    }
}

# touch stamp
open(F, ">$time_stamp_file") || die $!;
close F;


################################################################
sub findfile ($;$)
{
    my ($dir, $ext) = @_;
    my @files;

    my $code;
    if (ref $ext eq 'ARRAY'){
	for (@$ext){
	    $code .= "/\.$_\$/ ||";
	}
	$code .= "0";
    } elsif ($ext) {
	$code = "/\.$ext\$/";
    } else {
	$code = "1";
    }
#    print $code;
#    exit;
    find(sub {
	return if (/~$/ || /\.\.?$/ || /~$/ || /^\#/);
	return unless eval $code;
	my $name = $File::Find::name;
	return if $name =~ /CVS/;

	push (@files, $name);
#	print "$name, ";
    },
	 $dir);

    @files;
}
	

