#!/usr/bin/perl # ===================================================================== # W S - M A K E # # A utility to build, test and publish a Website from a master set # of files to a target Web host somewhere. # # -- H Marc Lewis, Nov-2008 # ===================================================================== use strict; use Net::FTP; use constant ASCII => "A"; use constant BINARY => "B"; my ($i, $ftp, $line, @lines, $item, $filename); my ($host, $user, $password, $there, $homedir); my (@commands, @put, $mode, $t); my $did_do = 0; my $did_put = 0; $host = $user = $password = $homedir = "?not-set?"; $mode = BINARY; readMakefile(); mp("Making..."); foreach $line (@commands) { print "\t$line\n"; print qx/$line/; } if ($ARGV[0] ne "publish") { mp("$did_do commands executed"); mp("Done"); exit; } mp("Connecting to: $host..."); $ftp = Net::FTP->new($host, Debug => 0) or die "Cannot connect to $host: $@"; mp("Logging in..."); $ftp->login($user,$password) or die("Cannot login ", $ftp->message); mp("Moving into $homedir directory..."); $ftp->cwd($homedir); mp("Sending files (mode is BINARY)..."); foreach $line (@put) { if ($line =~ s|cd:||) { $line = ".." if ($line =~ m|^\s*$|); mp("Changing into directory: $line"); chdir($line); $ftp->mkdir($line); # Create it, in case it isn't there $ftp->cwd($line); next; } if ($line =~ s/([A|B])://) { $t = $1; } #mp("t=$t, mode=$mode: $line"); if ($t ne $mode) { if ($mode eq ASCII) { mp("Switching to BINARY mode"); $mode = BINARY; $ftp->binary(); } else { mp("Switching to ASCII mode"); $mode = ASCII; $ftp->ascii(); } } print "\t$line\n"; $ftp->put($line); } mp("Logging out..."); $ftp->quit; mp("$did_do commands executed, $did_put files uploaded"); mp("Done"); # ==================================== # Write a log message to the console # ==================================== sub mp($) { my $msg = shift; print STDERR ">>> $msg\n"; } # =========================================================== # Read the makefile and build two arrays, one with commands # the other with files to be uploaded... # =========================================================== sub readMakefile() { my ($line, $command, $string); open(MF,") { chomp; next if (m|^\s*$|); next if (m|^#|); ($command, $string) = split(/\s+/,$_,2); $command = lc($command); #print "Command is '$command', string is '$string'\n"; SWITCH: { if ($command =~ m|host:|i) { $host = $string; last SWITCH; } if ($command =~ m|user:|i) { $user = $string; last SWITCH; } if ($command =~ m|password:|i) { $password = $string; last SWITCH; } if ($command =~ m|homedir:|i) { $homedir = $string; last SWITCH; } if ($command =~ m|do:|i) { push(@commands,$string); ++$did_do; last SWITCH; } if ($command =~ m|put:|i) { push(@put,"B:$string"); ++$did_put; last SWITCH; } if ($command =~ m|binary:|i) { push(@put,"B:$string"); ++$did_put; last SWITCH; } if ($command =~ m|ascii:|i) { push(@put,"A:$string"); ++$did_put; last SWITCH; } if ($command =~ m|changedir:|i) { push(@put,"cd:$string"); last SWITCH; } } } }