#!/usr/bin/perl
#
# DOS形式ftpサーバーからミラーリングダウンロードする
#
# 2010/8 Takamura @NT system design 
#
# CPANよりNet::DownloadMirrorとNet::MirrorDirをgetしてローカルで使用
# Net::MirrorDir ReadRemoteDirを書き換えてLS7000のDOS型ftpに対応させているので注意！
#
use strict;
# DownloadMirror.pmおよびMirrorDir.pm読み込みのため
push(@INC, '/home/hogehoge/mirror_wp');
require 'DownloadMirror.pm';
#use Net::DownloadMirror;

# 処理するロガー
my @types = ("A", "B");
# データロガーIP
my @ips = ('xxx.xxx.xxx.xxx' ,'xxx.xxx.xxx.xxx');
# ローカル保存先
my $dst_dir0 = "/home/hogehoge/ftpdata";
my @dst_dirs = ("A", "B");

# リモートdir
my $src_dir = "\\DATA\\SHORT";

# 1=全ファイルgetする
#my $all = 1;
my $all = 0;

#
# TYPE LOOP
#
foreach my $type( @types ) {
	my $ip;
	my $dst_dir;
	if ($type eq "A") {
		# A type
		$ip = $ips[0];
		$dst_dir = "$dst_dir0/$dst_dirs[0]";
	} else {
		# B type
		$ip = $ips[1];
		$dst_dir = "$dst_dir0/$dst_dirs[1]";
	}
	print "**** Connect to $ip ****\n";
	print "TYPE=$type\n";
	print "SRC=$src_dir\n";
	print "DST=$dst_dir\n";
	
	# 前日の日付でディレクトリ名作る
	my ($day, $month, $year) = (localtime(time - 24*3600))[3..5];

	my $yymmdd = sprintf "%02d%02d%02d", $year % 100, $month + 1, $day;
	printf "YYMMDD=$yymmdd\n";

	my $localdir;
	my $remotedir;
	if ($all) {
		# 全てget
		$localdir = $dst_dir;
		$remotedir = $src_dir;
	} else {
		# 1day get
		$localdir = "$dst_dir/$yymmdd";
		$remotedir = "$src_dir\\$yymmdd";
	}
	
	my $md = Net::DownloadMirror->new(
	        ftpserver       => $ip,
	        user            => 'hakusan',
	        pass            => 'Datamark',
	        localdir        => $localdir,
	        remotedir       => $remotedir,
	        debug           => 1, # 1 for yes, 0 for no
#	        timeout         => 60 # default 30
	#        delete          => 'enable' # default 'disabled'
	#        connection      => $ftp_object, # default undef
	# "exclusions" default empty arrayreferences []
	#        exclusions      => ["private.txt", "Thumbs.db", ".sys", ".log"],
	# "subset" default empty arrayreferences [ ]
	#        subset          => [".txt, ".pl", ".html", "htm", ".gif", ".jpg", ".css", ".js", ".png"],
	# or substrings in pathnames
	#       exclusions      => ["psw", "forbidden_code"]
	#       subset          => ["name", "my_files"]
	# or you can use regular expressions
	#       exclusinos      => [qr/SYSTEM/i, $regex]
	#       subset          => {qr/(?i:HOME)(?i:PAGE)?/, $regex]
	#       filename                => "modified_times",
	        );

	$md->Download();
}
