#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_Get_Shares: list shares for a host
#
# DESCRIPTION
# usage: BackupPC_Get_Shares: [options]
# Required options:
#	-h host         host from which the shares shall be listed
# Other options:
#	-c              comma separated list
#	-d char		use char as separator instead of comma (only used with -c)
#	-t char		enclose strings in char
#
# AUTHOR
#   Dan Johansson  <Dan.Johansson@dmj.nu>
#   (A _lot_ of code is borrowed from other BackupPC programs by Craig Barratt)
#
# COPYRIGHT
#   Copyright (C) 2012	Dan Johansson
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#========================================================================
#
# Version 0.1, released 19 Feb 2012.
# Known issue: If "host" does not exist the script continues and
#              outputs the default share from config.pl
#
#========================================================================
#

use strict;
no  utf8;
use lib "/usr/lib";
use Getopt::Std;
use Encode;
use BackupPC::Lib;
use BackupPC::Xfer;
#use Data::Dumper;

my %opts;
if ( @ARGV < 1 || !getopts("ch:t:d:", \%opts)) {
	print(STDERR "usage: $0 [options]\n");
	print(STDERR "Required options:\n");
	print(STDERR "	-h host         host from which the shares shall be listed\n");
	print(STDERR "Other options:\n");
	print(STDERR "	-c              comma separated list\n");
	print(STDERR "	-d char		use char as separator instead of comma (only used with -c)\n");
	print(STDERR "	-t char		enclose strings in char\n");
	exit(1);
}

if ( $opts{h} !~ /^([\w\.\s-]+)$/ || $opts{h} =~ m{(^|/)\.\.(/|$)} ) {
	print(STDERR "$0: bad host name '$opts{h}'\n");
	exit(1);
}
my $host = $opts{h};
my $shares;
my $share;

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );

#
# Read main config file
#
my($mesg, $config) = $bpc->{storage}->ConfigDataRead();
die $mesg if ( defined($mesg) );
$bpc->{Conf} = $config;

#
# Read host config file
#
($mesg, $config) = $bpc->{storage}->ConfigDataRead($host, $config);
die $mesg if ( defined($mesg) );
$bpc->{Conf} = $config;
$shares=BackupPC::Xfer::getShareNames($bpc->{Conf});

#
# Do the output
#
my $sep = ',';
$sep = $opts{d} if ($opts{d});
my $first=1;

for my $share ( @$shares ) {
	#
	# Convert $shareName to utf8 octets
	#
	$share = encode("utf8", $share);
	$share = $opts{t} . $share . $opts{t} if ($opts{t});
	if ($opts{c}) {
		print(STDOUT  "$sep") unless ($first);
		print(STDOUT  "$share");
		$first=0;
	} else {
		print(STDOUT  "$share\n");
	}
}
print(STDOUT "\n") unless ($first);
exit(0);
