#!/usr/bin/perl
# $Id: oddb_burn 2 2008-03-28 15:52:26Z winex $

use strict;
use Switch;
use Cwd;
use File::Copy;

use vars qw($prog $datadir);
use vars qw($dev @filelist $file);
use vars qw($opt_force $opt_check $opt_skip);

$prog = $0;
$prog =~ s/\/*.*\///g;

# config
$datadir = '/srv/ftp/oddb/.data';
$dev = '/dev/sr0';	# by default, use this device unless specified in argv

sub printusage()
{
	print("Usage: $prog [DEVICE] FILE...\n");
	print("\n");

	return 0;
}

sub printhelp()
{
	print("Options:\n");
	print("    -        read list from stdin, in case of spaces in file names\n");
	print("    --help   print this help\n");
	#print("    --force  force overwriting existing files, use with caution!\n");
	#print("    --check  check media in DEVICE with MEDIALABEL.iso.md5 found under DATADIR\n");
	#print("             in case of making isos from dirs, verify their md5lst and move to DATADIR\n");
	#print("    --skip   skip creating iso itself, create md5 only or smth(?)\n");
	print("    --config print current config\n");
	print("    DEVICE   use this block device to burn to, if not specified use /dev/sr0\n");
	print("\n");
	print("Notes:\n");
	print("    ALL PROCESSING IS DONE USING PATHS SPECIFIED IN CONFIG, see $prog --config\n");
	print("\n");
	print("Examples:\n");
	print("    $prog /dev/sr0 somefile00*.iso  # will burn all matched iso images using /dev/sr0\n");
	print("\n");

	return 0;
}

sub printconfig()
{
	print("DATADIR: $datadir\n");

	return 0;
}

my $argc = $#ARGV+1;
if ($argc < 1)
{
	printusage();
	exit(0);
}


foreach my $arg (@ARGV)
{
	switch ($arg)
	{
		case /^--help$/
		{
			printusage();
			printhelp();
			exit(0);
		}
		case /^--config$/
		{
			printconfig();
			exit(0);
		}
		case /^-$/
		{
			my $tmp = '';
			while (<>)
			{
				$tmp = $_;
				chomp($tmp);
				$tmp = getcwd() if ($tmp =~ /\.\/*?/);
				push(@filelist, $tmp);
			}
		}
		case /^--force$/
		{
			$opt_force=1;
		}
		case /^--check$/
		{
			$opt_check=1;
		}
		case /^--skip$/
		{
			$opt_skip=1;
		}
		case { -b $arg }
		{
			$dev=$arg;
			last;    # process only 1 device
		}
		else
		{
			$arg = getcwd() if ($arg =~ /\.\/*?/);
			push(@filelist, $arg);
		}
	}
}


my $retval = 0;

foreach $file (@filelist)
{
	chomp($file);
	next if ($file eq '');

	print("$file\n");
}

exit($retval);


###

wodim -v dev=$dev -sao speed=8 gracetime=10 "$fname" && \
eject $dev && sleep 1 && eject -t $dev && \
oddb_mkimage --check $dev && \
rm -f "$fname"
