#!/bin/bash

#checking for essential devices
echo "---checking for essential devices---"
if [ -r /dev/mixer ]; then
	printf "\n"
	echo "This must be your lucky day you have the /dev/mixer"
else
	printf "\n"
	echo "BUMMER! you dont have a /dev/mixer no use for installing is there?"
	echo "EXIT!!!!!"
	exit
fi

if [ -r /dev/dsp ]; then
	printf "\n"
	echo "This must be your lucky day you have the /dev/dsp"
else
	printf "\n"
	echo "BUMMER! you dont have a /dev/dsp no use for installing is there?"
	echo "EXIT!!!!!"
	exit
fi

# gets the name of the user and prints a greeting
clear
echo -n "Enter the location to install the Jukebox "
read install_dir


# the user did not enter anything:
if [ -z "$install_dir" ]; then
	echo  "You did not provide a Instalation location:"
	exit
fi

#if there is a location given wich already exists
if [ -d $install_dir ]; then
	echo "Directory already exists do you want to copy to $install_dir anyway? y/n: ?"
	read confirm
	if [ "$confirm" = y ]; then
	echo "doing it anyway"
	cp -fR $PWD/. $install_dir
	else
	echo "leaving the directory $install_dir alone"
	exit
	fi
#when the location doesnt exist we are creating the directory
else
	echo "creating New directory $install_dir"
	mkdir $install_dir
	if [ -d $install_dir ]; then
	echo "created $install_dir succesfully"
	 cp -R $PWD/. $install_dir
	else
	echo "creation of $install_dir failed"
	exit
	fi
fi
echo "<?" > $install_dir/config.php
echo "\$htmldocroot=\"$install_dir\";" >> $install_dir/config.php

#setting the mp3 player
 printf "\n"
 echo "-- checking for the amp player--"
if [ -x /bin/amp ]; then
	echo "found amp in /bin/amp should i use this amp? y/n:"
	read confirm
		if [ "$confirm" = y ]; then
		echo "using /bin/amp as the player"
		echo "\$mp3player=\"/bin/amp\";" >> $install_dir/config.php
		else
		echo "not using any player now edit the config.php by hand"
		fi
else		
	echo "could not find AMP in the default location the location of the player please give the location by hand"
	read amp
	
	echo "\$mp3player=\"$amp\";" > $install_dir/config.php
	echo "using $amp as the location"
fi

printf "\n"
echo "Give the location to the mp3 files if the directory does not exist it will be created"
read mp3_files
if [ -d $mp3_files ]; then
	echo "\$filedir=\"$mp3_files\";" >> $install_dir/config.php
	echo "done setting the mp3 directory to $mp3_files"
else
	echo "creating the mp3 directory $mp3_files"
	mkdir $mp3_files
	if [ -d $mp3_files ]; then
	echo "done"
	else
	echo "failed to create the mp3 directory"
	fi
fi

#setting the temp dir
printf "\n"
echo "--We need a directory where user apache/nobody can write files usually this is /tmp--"
if [ -d /tmp ]; then
	echo "found your /tmp dir should i use this?"
	read confirm
	if [ "$confirm" = y ]; then
		echo "using /tmp as the /tmp dir"
		echo "\$tempdir=\"/tmp\";" >> $install_dir/config.php
		echo "done"
	else
		echo "you should edit the temprary folder manually in the config.php"
		echo "\$tempdir=\"edit by hand\";" >> $install_dir/config.php
	fi
else
	echo "did not find the default /tmp directory please give it now"
	read temp
	if [ -z "$temp" ]; then
		echo  "You did not provide a temp dir you should edit the config.php manually:"
		echo "\$tempdir=\"edit by hand\";" >> $install_dir/config.php
	else
		echo "\$tempdir=\"$tmp\";" >> $install_dir/config.php
		echo "Done adding $temp as the temp dir"
	fi
fi

#setting mysql user information
printf "\n"
echo "Enter the mysql root password"
mysql -u root -p < install/wmj.sql
echo "\$mysqluser=\"wmjuser\";" >> $install_dir/config.php
printf "\n"
echo "-- Now give your personal password for the wmj database when left blank the default password (wmjpass) will be used --"
read password

if [ -z "$password" ]; then
	echo  "You did not give a personal password the default password (wmjpass) is being used"
	echo "\$mysqlpass=\"wmjpass\";" >> $install_dir/config.php
else
	echo "now enter the old password by default this is wmjpass"
	mysqladmin -p -u wmjuser password $password
	echo "\$mysqlpass=\"$password\";" >> $install_dir/config.php
fi
echo "\$mysqldatabase=\"wmjdb\";" >> $install_dir/config.php
echo "?>" >> $install_dir/config.php

printf "\n"
echo "ATTENTION in need to add something to your rc.local to chmod the /dev/mixer and /dev/dsp on boot"
echo "do you want me to do this? y/n"
read confirm
if [ "$confirm" = y ]; then
	if [ -w /etc/rc.local ]; then
		echo "/bin/chmod 666 /dev/mixer" >> /etc/rc.local
		echo "/bin/chmod 666 /dev/dsp" >> /etc/rc.local
		echo "done"
	else
		echo "Could not find the rc.local edit by hand please"
	fi
else
	echo "you should edit the rc.local yourselfe"
fi
printf "\n"
echo "###################################################"
echo "all done setting up the jukebox"
echo "if something does not work check the config.php"
echo "have fun listening"
echo "Check our website for updates"
echo "http://wmj.sourceforge.net"
echo "##################################################"