#!/usr/bin/perl
# vorbisinfo v0.4
# Desc: a vorbisinfo for ogg/vorbis files like mp3info for mp3 files
#
# (C) Mar 2001, 2002, 2004 by René Rebe <rene@exactcode.de>
#     Jan 2002 by Lars Kuhtz (formerly Siggelkow) <lars@exactcode.de>
#
# ChangeLog:
#   2001-03-04 René: 1st version
#   2001-03-11 René: added help text, license, no option case
#   2002-01-11 Lars: fixed the the exection of command by escaping space in the filename-param
#   2004-11-23 René: fixed the file usage so all names hopefully work

$h_txt = "vorbisinfo v0.4 Copyright (C) 2001, 2002, 2004 by:\n" .
  "  René Rebe <rene\@exactcode.de>\n" .
  "  Lars Kuhtz <lars\@exactcode.de>\n\n" .
  "vorbisinfo comes with ABSOLUTELY NO WARRANTY. This is free software, and\n" .
  "you are welcome to redistribute it under certain conditions.\n" .
  "See the file GPL for more informations.\n\n" .
  "This script takes mp3info style arguments and uses vorbiscomment to feed them\n" .
  "into ogg-vorbis files.\n\n" .
  "USAGE:\n" .
  "\tvorbisinfo OPTIONS file\n\n" .
  "OPTIONS:\n" .
  "\t-h\t\tDisplay this help text\n" .
  "\t-a artist\tSpecify artist name\n" .
  "\t-c comment\tSpecify comment\n" .
  "\t-t title\tSpecify track title\n" .
  "\t-l album\tSpecify album name\n" .
  "\t-y year\t\tSpecify copyright year\n";

use Getopt::Std;

# test params
die $h_txt if ($#ARGV == -1);

# set and test files
my $file = @ARGV[$#ARGV];
my $tmpfile = $file . "-tmph44k";
die "$file not an ogg file?\n" if ($file !~ /\.ogg$/);
die "$file does not exist!" if (! -f $file);

# get and parse arguments
my %opts;
getopts ('ha:c:l:t:y:', \%opts);
die $h_txt if ($opts{h} ne "");
my $tags ="";
$tags .= "artist=" . $opts{a} . "\n" if ($opts{a} ne "");
$tags .= "comment=" . $opts{c} . "\n" if ($opts{c} ne "");
$tags .= "title=" . $opts{t} . "\n" if ($opts{t} ne "");
$tags .= "year=" . $opts{y} . "\n" if ($opts{y} ne "");
$tags .= "album=" . $opts{l} . "\n" if ($opts{l} ne "");

# execute $command
$command = "vorbiscomment";
die "Can not execute $command!\n" if ! open(COMMAND, "| $command -w '$file' '$tmpfile'");
printf COMMAND $tags;
die "Some error during execution!(?)\n" if ! close (COMMAND);

# remove, move file (with max data security ;-) - I must have too much time ...
die "Error while renaming!(?)\n" if ! rename $file, $tmpfile . "2";
die "Error while renaming!(?)\n" if ! rename "$tmpfile", $file;
die "Error while unlinking!(?)\n" if ! unlink $tmpfile . "2";

