#!/usr/bin/perl
#
# Date:    2009-12-10
# Author:  Ole Kristian Lien
# License: GNU General Public License
#
# Parse csv-file for information
#
# needs: libtext-csv-perl

use strict;
use warnings;
use Text::CSV;

if ($#ARGV != 2 ) {
	print "usage: csv <csv-file> [<video-file> [<info>]]\n";
	exit;
}

my $file=$ARGV[0];
my $video=$ARGV[1];
my $info=$ARGV[2];

my $filename;
my $logo;
my $name;
my $title;
my $what;
my $date;
my $location;
my $genre;
my $license;
my $organization;
my $language;
my $contact;
my @takk;
my @url;

my $csv = Text::CSV->new();
open (CSV, "<:encoding(utf8)", "$file") or die "$file: $!";

while (<CSV>) {
	if ($csv->parse($_)) {
		my @columns  = $csv->fields();

		if ($. == 1) {
				my $index;
				foreach (@columns) {
					if ($_ eq "Filename") {
						$filename=$index;
					} elsif ($_ eq "Title") {
						$title=$index;
					} elsif ($_ eq "Logo") {
						$logo=$index;
					} elsif ($_ eq "Name") {
						$name=$index;
					} elsif ($_ eq "Title") {
						$title=$index;
					} elsif ($_ eq "What") {
						$what=$index;
					} elsif ($_ eq "Date") {
						$date=$index;
					} elsif ($_ eq "Location") {
						$location=$index;
					} elsif ($_ eq "Genre") {
						$genre=$index;
					} elsif ($_ eq "License") {
						$license=$index;
					} elsif ($_ eq "Organization") {
						$organization=$index;
					} elsif ($_ eq "Contact") {
						$contact=$index;
                                        } elsif ($_ eq "Language") {
                                                $language=$index;
					} elsif ($_ eq "Takk") {
						push(@takk, $index); 
					} elsif ($_ eq "URL") {
						push(@url, $index); 
					}
					$index++;
				}
		}

		next if ($. == 1);
#print "# $. \n\n";

		if ($video eq $columns[0]) {
			if ($info eq "filename") {
				print "$columns[0]"; #$filename]";
			} elsif ($info eq "logo") {
				print "$columns[$logo]";
			} elsif ($info eq "name") {
				print "$columns[$name]";
			} elsif ($info eq "title") {
				print "$columns[$title]";
			} elsif ($info eq "what") {
				print "$columns[$what]";
			} elsif ($info eq "date") {
				print "$columns[$date]";
			} elsif ($info eq "location") {
				print "$columns[$location]";
			} elsif ($info eq "genre") {
				print "$columns[$genre]";
			} elsif ($info eq "license") {
				print "$columns[$license]";
			}  elsif ($info eq "organization") {
				print "$columns[$organization]";
			}  elsif ($info eq "contact") {
				print "$columns[$contact]";
                        }  elsif ($info eq "language") {
                                print "$columns[$language]";
			} elsif ($info eq "takk") {
				foreach (@takk) {
					my $takk = $columns[$_];
					if($takk) {
						print "$takk\n";
					}
				}
			} elsif ($info eq "url") {
				foreach (@url) {
					my $lenke = $columns[$_];
					print "$lenke\n";
				}
			} else {
				print "$info column doesn't exist\n";
			}

		}# else {
		#	print "$info row doesn't exist\n";
		#}
	} else {
		my $err = $csv->error_input;
		print "Failed to parse line #$..: $err";
	}
}

close CSV;
