#!/bin/bash -ex

# set hostname
HOSTNAME=ampache
echo "$HOSTNAME" > /etc/hostname
sed -i "s|127.0.1.1 \(.*\)|127.0.1.1 $HOSTNAME|" /etc/hostname

#Start MySQL Daemon
/etc/init.d/mysql start

#create dir for samba share
mkdir /media/media 

#Set permissions for samba share
chmod 777 /media/media

# install Samba and webmin-samba and Ampache
#Add repo - NO, doesn't work
#echo "deb http://us.archive.ubuntu.com/ubuntu/ lucid universe">>/etc/apt/sources.list
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y \
    -o DPkg::Options::=--force-confdef \
    -o DPkg::Options::=--force-confold \
    install samba webmin-samba ampache

#Change MySQL Root Account
mysqladmin -u root password root

#Change Apache2 Default Web Dir
sed -i "s|/var/www|/usr/share/ampache/www/|" /etc/apache2/sites-available/default

#Rename ampache.conf
#mv /etc/ampache/ampache.conf former_ampache.conf

#Add Ampache user to MySQL and grant privileges
#Handled now in overlay- mysql -u root -proot -e GRANT ALL PRIVILEGES ON ampache.* TO 'ampache'@'localhost' INDENTIFIED BY 'ampache'

#import database
mysql -u root -proot < /usr/share/ampache/www/sql/ampachedump.sql

# Configure samba share
echo "[media]" >> /etc/samba/smb.conf
echo "     writeable = yes" >> /etc/samba/smb.conf
echo "     public = yes" >> /etc/samba/smb.conf
echo "     path = /media/media" >> /etc/samba/smb.conf
#Stop MySQL Server
/etc/init.d/mysql stop
