#!/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
#This line doesn't work with Hardy: 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
#Mysql ampache user & permissions are appended in ampachedump.sql rather than being passed through bash

#Change Apache2 Default Web Dir - we don't fully understand other methods to accomplish this
sed -i "s|/var/www|/usr/share/ampache/www/|" /etc/apache2/sites-available/default

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

#Symbolic Link for ampache.cfg.php
#doesn't work in this direction: ln -s /usr/share/ampache/www/config/ampache.cfg.php /etc/ampache/ampache.cfg.php
rm /usr/share/ampache/www/config/ampache.cfg.php
#rm command obviated if we use ln -sf in the following command?
ln -s /etc/ampache/ampache.cfg.php /usr/share/ampache/www/config/ampache.cfg.php

# 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
