#! /bin/sh -e
#Passes on the information gathered in ubiquity
#about mythtv package variable
# Copyright (C) 2007 Mario Limonciello
# Written by Mario Limonciello <superm1@ubuntu.com>

. /usr/share/debconf/confmodule

if [ "$1" ]; then
	ROOT="$1"
	chroot=chroot
	log='log-output -t mythbuntu'
else
	ROOT=
	chroot=
	log=
fi

#Either way, turn off mysql,mythtv-backend, apache if it running
#Builds based on alpha 1 need this
/etc/init.d/mysql stop
/etc/init.d/mythtv-backend stop
/etc/init.d/apache2 stop

# Prepare mythtv-common
# And copy over env as needed
local USER
local PASSWORD
local DB
local HOSTNAME
db_get mythtv/mysql_mythtv_user
USER="$RET"
db_get mythtv/mysql_mythtv_password
PASSWORD="$RET"
if [ -z "$PASSWORD" ]; then
	#in case the password screen isn't shown
	#or if they forget to enter one
	set +e
	PASSWORD="$(pwgen -s 8)"
	set -e
fi
db_get mythtv/mysql_mythtv_dbname
DB="$RET"
db_get mythtv/mysql_host
HOSTNAME="$RET"
if [ -z "$HOSTNAME" ]; then
	HOSTNAME="localhost"
fi
cat <<EOF >$ROOT/tmp/myth_setup
#! /bin/sh
set -e

. /usr/share/debconf/confmodule	
db_set mythtv/mysql_mythtv_user $USER
db_set mythtv/mysql_mythtv_password $PASSWORD
db_set mythtv/mysql_mythtv_dbname $DB
db_set mythtv/mysql_host $HOSTNAME

cat /etc/mythtv/mysql.txt | grep -v 'DBPassword=' | sed -e "
s/^\(\(str  *\)\?DBHostName\)=.*$/\1=$HOSTNAME/g;
s/^\(\(str  *\)\?DBUserName\)=.*$/\1=$USER/g;
s/^\(\(str  *\)\?DBName\)=.*$/\1=$DB/g;" > /etc/mythtv/mysql.txt.dist
cat <<EOF2 >> /etc/mythtv/mysql.txt.dist
DBPassword=$PASSWORD
EOF2
mv /etc/mythtv/mysql.txt.dist /etc/mythtv/mysql.txt
exit 0
EOF
chmod +x $ROOT/tmp/myth_setup
$log $chroot $ROOT /tmp/myth_setup
rm $ROOT/tmp/myth_setup

#Check for needing to set up a DB too
#If so - do it
db_get mythbuntu/install_type
TYPE="$RET"
if [ "$TYPE" = "Master Backend/Frontend" -o "$TYPE" = "Master Backend" ]; then
	local ADMINUSER
	local ADMINPASS
	db_get mythtv/mysql_admin_user
	ADMINUSER="$RET"
	db_get mythtv/mysql_admin_password
	ADMINPASS="$RET"
	cat <<DBSETUP >$ROOT/tmp/db_setup
#!/bin/sh
#Sets up mythtv-database on a new mythbuntu
#installation
#Copyright (C) 2007 Mario Limonciello

escape_quotes() {
    cat <<EOF | sed -e "s/'/\\\\\\\\'/g"
\$1
EOF
}

DoSQL() {
    local host="\$1"
    local ADMINUSER="\$2"
    local ADMINPASS="\$3"
    local DB="\$4"
    local statement="\`escape_quotes \"\$5\"\`"
    local tmp=\`tempfile -m 600\`
    cat <<EOF >\$tmp
\$ADMINPASS
EOF
    perl -e "
use DBI;
chomp(\\\$password=<>);
@statements=split(/;/, '\$statement');
\\\$db = DBI->connect('dbi:mysql:host=\$host;database=\$DB',
                    '\$ADMINUSER', \\\$password,
                    { PrintError => 0 }) || die 'Failed to connect to DB: ' . \\\$DBI::errstr;
for \\\$s (@statements) { \\\$db->do(\\\$s) || die 'Failed to execute SQL: ' . \\\$s . '\n' . \\\$DBI::errstr; }
" < \$tmp
    ret=\$?
    rm -f \$tmp
    return \$ret
}

#Start SQL
/etc/init.d/mysql start

#Set our new defaults that were determined to debconf
. /usr/share/debconf/confmodule
db_set mythtv/mysql_admin_user $ADMINUSER
if [ ! -z $ADMINPASS ]; then
	db_set mythtv/mysql_admin_password $ADMINPASS
fi

#Setup the mysql server with the new defaults
DoSQL "$HOSTNAME" "$ADMINUSER" "$ADMINPASS" "" "CREATE DATABASE $DB"
DoSQL "$HOSTNAME" "$ADMINUSER" "$ADMINPASS" "$DB" "GRANT ALL PRIVILEGES ON $DB.* TO $USER@localhost IDENTIFIED BY '$PASSWORD'"
DoSQL "$HOSTNAME" "$ADMINUSER" "$ADMINPASS" "$DB" "GRANT ALL PRIVILEGES ON $DB.* TO $USER@'%' IDENTIFIED BY '$PASSWORD'"

#Stop SQL
/etc/init.d/mysql stop

## Setup services to Start On boot - Stop on halt/reboot
update-rc.d mythtv-backend defaults
update-rc.d apache2 defaults
update-rc.d mysql defaults

DBSETUP

	chmod +x $ROOT/tmp/db_setup
	mount -t proc none $ROOT/proc
	$log $chroot $ROOT /tmp/db_setup
	umount $ROOT/proc
	rm $ROOT/tmp/db_setup
fi

exit 0
