#!/usr/bin/perl
use DBD::mysql;

open REGISTER, "Reg.Install" or die "Open register : $! \n";
open MUSIC, "Mus.Install" or die "Open Music : $! \n";
open STREAM, "Stre.Install" or die "Open Stream : $! \n
";

print "Streaming Server Install Script. \n";
print "------------------------------------\n\n";
print "Before starting this script, you need to have\n";
print "an HTTP server with cgi capability and a MySQL\n";
print "server up and running on your machine. \n";
print "First off, is your MySQL Server up and running (yes\\no) : ";
$x = <STDIN>;
chomp $x;
if ($x eq "yes") { print "okay, good\n"; }
elsif ($x eq "no") { die "Start it and then restart this script \n"; }
else { die "yes or no answers only please \n";  }  
$x = 0;

print "give me a username and password for the MySQL database \nthat has permission to create tables \n";
print "Username : ";
$MySQLUser = <STDIN>; 
chomp $MySQLUser;
print "Password : ";
$MySQLPass = <STDIN>;
chomp $MySQLPass;
$dbh = DBI->connect("DBI:mysql:", "$MySQLUser","$MySQL") or die "Can't Connect to MySQL DB : $!\n";
#$dbh->do("CREATE DATABASE music") or die "Couldn't Create Database : $!\n";

print "Database Created Successfully! \nNow We need to get some information about where things are on this machine\n";
print "Do NOT put a trailing slash on directory locations,\n/home/music/ = BAD \n/home/music = good \n";
while ($x == 0) {
print "What is the base directory where your MP3's are stored ? ";
$MusicDir = <STDIN>;
chomp $MusicDir;

print "Where is the DocumentRoot of your webserver ? ";
$DocRoot = <STDIN>;
chomp $DocRoot;

print "Where is your cgi-bin located ? ";
$StreamExec = <STDIN>;
chomp $StreamExec;

print "What is the name of your Server (This must either be a fully qualified and VALID DNS name, or an IP address) ? ";
$ServerAddr = <STDIN>;
chomp $ServerAddr;

print "What genre do you want to be sent in the stream headers ? ";
$Genre = <STDIN>;
chomp $Genre;

print "What URL do you want to be sent in the stream headers ? ";
$Url = <STDIN>;
chomp $Url;

 
print "Okay, this is what you gave me : \n";
print "Base MP3 Directory - $MusicDir \n";
print "Web Server Documentroot - $DocRoot \n";
print "Server Address - $ServerAddr \n"; 
print "CGI-BIN - $StreamExec \n";
print "Genre - $Genre \n";
print "URL - $Url \n";
print "If this isn't right, type r, otherwise, just hit enter : ";
$r = <STDIN>;
chomp $r;
if ($r ne "r") { $x = 1; }
}

open STREAMWRITE, ">$StreamExec/Stream.pl" or die "STREAMWRITE : $! \n";
open MUSICWRITE, ">$StreamExec/Music.pl" or die "MUSICWRITE : $! \n";
open REGISTERWRITE, ">$StreamExec/Register.pl" or die "REGISTERWRITE : $! \n";

while (<REGISTER>) {
s/MYSQLUSER/$MySQLUser/g;
s/MYSQLPASS/$MySQLPass/g;
s/MUSICDIR/$MusicDir/g;
print REGISTERWRITE "$_" or die "Write Register : $! \n";
}
while (<MUSIC>) {
s/MYSQLUSER/$MySQLUser/g;
s/MYSQLPASS/$MySQLPass/g;
s/DOCROOT/$DocRoot/g;
s/SERVERADDR/$ServerAddr/g;
s/STREAMEXEC/$StreamExec/g;
print MUSICWRITE "$_" or die "Write Music : $! \n"; 
}

while (<STREAM>) {
s/DOCROOT/$DocRoot/g;
s/SERVERADDR/$ServerAddr/g;
s/GENRE/$Genre/g;
s/URL/$Url/g;
print STREAMWRITE "$_" or die "Write Stream : $! \n";
} 

close STREAM;
close STREAMWRITE;
close MUSIC;
close MUSICWRITE;
close REGISTER;
close REGISTERWRITE;
chmod 0711, "$StreamExec/Stream.pl";
chmod 0711, "$StreamExec/Music.pl";
chmod 0711, "$StreamExec/Register.pl";

print "Okay, everythings configured \n";
print "You'll want to run $Streamexec/Register.pl for the first time to get the database up and running. \n";
#Register.pl
#MYSQLUSER	= Username for the MySQL Database.
#MYSQLPASS   	= Password for the MySQL Database.
#MUSICDIR	= Root Directory where music is stored. 
#
#Music.pl  
#MYSQLUSER	= Username for the MySQL Database.
#MYSQLPASS   	= Password for the MySQL Database.
#DOCROOT	= Documentroot on their apache server.
#SERVERADDR  	= The Fully Qualified DNS or IP Address of the Server. 
#STREAMEXEC	= The location of Stream.pl on the server. 
#
#Stream.pl
#DOCROOT     	= Documentroot on their apache server.
#SERVERADDR	= The Fully Qualified DNS or IP Address of the Server.
#GENRE		= Whatever you want to display on the genre line.
#URL		= Whatever you want to display on the URL line.

