#!/usr/bin/perl
#
# dbmusic.pl v0.0.11 - Database for music albums
# Copyright (C) 2005, 2006 Stian Skjæveland
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#

use CGI qw(:standard);
use strict;

print header;

print qq(
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- 
	Copyright (C) 2005, 2006 Stian Skjæveland.
	Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved. 
-->

<html>
<head><title>Music database - Results</title>
<meta name="AUTHOR" content="stian at atlantiscrew period net">
<meta name="KEYWORDS" content="personal, music, database">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="icon" type="image/png" href="/pics/music.png">
</head>
<body onLoad="window.document.forms[0].elements[0].focus();">
);

&display_search();

print qq(
<p>
<b>Music database - Results:</b>
<hr>
);
my $param_search=lc(param('search'));
my $param_alt=lc(param('alt'));
my $file="/home/stunix/domains/atlantiscrew.net/public_html/stian/doc/list.txt"; # You may set this to whatever fits your system.
my ($bandname,
    $albumtitle,
    $yearofrelease,
    $genre,
    $other,
    $i,
    $filelength,
    @filelines);
my $found=0;
my $count=0;

open(DB_FILE, "<$file") || print "<b>Cannot find file: $file<b><p>";
@filelines=<DB_FILE>;

print "You're searching for \"$param_search\" in $param_alt (<a href=\"http://stian.atlantiscrew.net/uimusic.html\">Back</a>):<p>";

if ($param_alt) {
  &view_db();
}
close(DB_FILE);

sub view_db() {
  print qq(
    <table border="1" width="100%" cellspacing="0" cellpadding="0">
    <colgroup>
      <col width="15%">
      <col width="15%">
      <col width="5%">
      <col width="10%">
      <col width="10%">
    </colgroup>
  );
  if ($param_alt eq "all") {
    for ($i=0; $i<999; $i++) {
      if (lc($filelines[$i])=~/$param_search/) {
        &view_db_results(); 
        if (!($filelines[$i+1])) {
          last;
        }
      }
    }
  }
 for ($i=0; $i<999; $i++) {
    ($bandname, $albumtitle, $yearofrelease, $genre, $other)=split('@', $filelines[$i]);
    if ($param_alt eq "band name") {
      if (lc($bandname)=~/$param_search/) {
        &view_db_results();
      }
    }
    if ($param_alt eq "album title") {
      if (lc($albumtitle)=~/$param_search/) {
        &view_db_results();
      }
    }
    if ($param_alt eq "year of release") {
      if (lc($yearofrelease)=~/$param_search/) {
        &view_db_results();
      }
    }
    if ($param_alt eq "genre") {
      if (lc($genre)=~/$param_search/) {
        &view_db_results();
      }
    }
    if ($param_alt eq "other") {
      if (lc($other)=~/$param_search/) {
        &view_db_results();
      }
    }
    if (!($filelines[$i+1])) {
      last;
    }
  }
}

print qq(
  <tr><td></td></tr>
  </table>
  <p>
);

if ($found ne 1) {
  print "<p>No match. Try different keyword(s).";
} else {
  $filelength=@filelines;
  print "<p><b>$count</b> match(es) of total <b>$filelength</b>.";
}

sub view_db_results() {
  ($bandname, $albumtitle, $yearofrelease, $genre, $other)=split('@', $filelines[$i]);
  print "<tr><td>$bandname<td>$albumtitle<td>$yearofrelease<td>$genre<td>$other<br>";
  $count+=1;
  $found=1;
}

print qq(
<hr>
Copyright (C) 2005, 2006 Stian Skjæveland.
<br>Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.
<p>
<a href="http://www.perl.com"><img src="http://www.atlantiscrew.net/pics/perl.gif" border="0" alt="Powered by Perl"></a>
<a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-html401.png" border="0" alt="Valid HTML 4.01!"></a>
</body>
</html>
);

sub display_search() {
print qq(
  <form action="http://stian.atlantiscrew.net/cgi-bin/dbmusic.pl" method="get" target="_self">
  Search for <input type="text" name="search" size="40">
  in <select name="alt" size="1">
  <option value="all" selected>whole database</option>
  <option value="band name">band name</option>
  <option value="album title">album title</option>
  <option value="year of release">year of release</option>
  <option value="genre">genre</option>
  <option value="other">other</option>
  </select>
  <input type="submit" value="Search">
  </form>
);
}
