#!/usr/bin/perl
#   rssite [site]...
# Sync the TuneFinder's data for a site with one or more of the hosts
# where the TuneFinder is installed.

($P = $0) =~ s"^.*/"";		# Our name without directory path
$ID = $ENV{'USER'} || 'jc';	# The login id to use
$V  = $ENV{"V_$P"} || 1;	# Verbose level

# Options for copying a hst/* file:
#rsOpts = '-vHputz';		# Options for rsync
$rsOpts = '-putz';			# Options for rsync

@hosts = (
	'kendy',
	'rucker',
	'trill',
); 
%hst2id = (		# TuneFinder hosts to sync with, and the login id for each
	'kendy'  => 'jc',
	'rucker' => 'jc',
	'trill'  => 'jc',
);

for $site (@ARGV) {
	print "Site $site:\n" if $V>0;
	for $host (@hosts) {
		print "Host $host:\n" if $V>0;
		$id = $hst2id{$host} || $ID;
		print "Using id \"$id\" for host \"$host\"\n" if $V>0;
		$hst = "w/music/bot/hst/$site";		# The site's hst/ file
		$cfg = "w/music/bot/hst/$site";		# The site's cfg/ file
		$dir = "w/music/bot/cache/$site";	# The site's cache directory
		print "\t$hst ...\n" if $V>0;
		print "\trsync $rsOpts $host:$hst $hst\n";	# Sync host's hst/$site file to ours 
		system  "rsync $rsOpts $host:$hst $hst";	# Sync host's hst/$site file to ours 
		if (-f $hst) {
			print "\trsync $rsOpts $hst $host:$hst\n";	# Sync our hst/$site file to host's 
			system  "rsync $rsOpts $hst $host:$hst";	# Sync our hst/$site file to host's 
		} else {
			print "### No file \"$hst\"\n" if $V>0;
		}
		$f = "$dir/$site";	# Sync the site's cache directories
		if (-d $dir) {
		} else {
			unlink $dir if -f $dir;
			print "\tCreate \"$dir\"\n" if $V>0;
			mkdir $dir;
		}
		link $f, $dir;
		print "\trs$host w/music/bot/cache/$site ...\n" if $V>0;
		system  "rs$host w/music/bot/cache/$site";
		sleep 5 * $V;
	}
}

