#! /bin/sh
# Copyright (c) 2010, Novell Inc.
#
# Author: adrian@suse.de
#
# /etc/init.d/obsapisetup
#   and its symbolic  link
# /usr/sbin/rcobsapisetup
#
### BEGIN INIT INFO
# Provides:          obsapisetup
# Start-Before:      lighttpd
# Should-Start:      obsstoragesetup
# Should-Stop:       $none
# Required-Start:    mysql
# Required-Stop:     $null
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Description:       Initialize and update api database, only used in OBS Appliance
### END INIT INFO

. /etc/rc.status

# Determine the base and follow a runlevel link name.
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

apidir=/srv/www/obs/api
webuidir=/srv/www/obs/webui

FQHOSTNAME=`hostname -f`

. /etc/sysconfig/obs-server

if [ "$OBS_API_AUTOSETUP" != "yes" ]; then
   echo "OBS API Autosetup is not enabled in sysconfig, skipping!"
   exit 0
fi

rc_reset
case "$1" in
	start)
                if [ ! -d /obs/MySQL ]; then
			mkdir /obs/MySQL
		fi
                if [ ! -d /obs/MySQL/api_production ]; then
			echo -n "Initialize OBS database (first time only)"
			mysqladmin -u root create api_production
			mysqladmin -u root password "opensuse"
			echo
		fi

		echo -n "Setup or update OBS webui database"
                cd $webuidir
		RAILS_ENV=production rake db:migrate >> $webuidir/log/db_migrate.log
		chown -R lighttpd.lighttpd $webuidir/log
		echo
		echo -n "Setup or update OBS api database"
                cd $apidir
		RAILS_ENV=production rake db:migrate >> $apidir/log/db_migrate.log
		chown -R lighttpd.lighttpd $apidir/log
		echo
		echo -n "Adjust webui configuration for api"
                # it would be better to solve this in lighttpd.conf to accept also localhost:81,
                # but no idea how to solve this
                sed -i 's,^FRONTEND_HOST = .*,FRONTEND_HOST = "'"$FQHOSTNAME"'",' \
		    /srv/www/obs/webui/config/environments/production.rb
                sed -i 's,^DOWNLOAD_HOST = .*,DOWNLOAD_HOST = '"$FQHOSTNAME:82"',' \
		    /srv/www/obs/webui/config/environments/production.rb
		echo
		echo -n "Write OBS lighttpd config for this host"
		cat > /etc/lighttpd/vhosts.d/obs.conf <<EOF
#
# WARNING: this file gets rewritten on each boot by obsapisetup runlevel script!
#
\$HTTP["host"] == "$FQHOSTNAME:80" {
  rails_app   = "webui"
  rails_root  = "/srv/www/obs/webui"
  rails_procs = 5
  # production/development are typical values here
  rails_mode  = "production"

  log_root = "/srv/www/obs/webui/log"

  include "vhosts.d/rails.inc"
}
\$SERVER["socket"] == ":81" {
  rails_app   = "api"
  rails_root  = "/srv/www/obs/api"
  rails_procs = 5
  # production/development are typical values here
  rails_mode  = "production"

  log_root = "/srv/www/obs/api/log"

  include "vhosts.d/rails.inc"
}
\$SERVER["socket"] == ":82" {
  server.name = "$FQHOSTNAME"
  server.document-root = "/obs/repos/"

  dir-listing.activate = "enable"
}

EOF
		OBSVERSION=`rpm -q --qf '%{VERSION}' obs-server`
		OS=`head -n 1 /etc/SuSE-release`
		cat > /etc/issue <<EOF
Welcome to openSUSE Build Service(OBS) Appliance $OBSVERSION
based on $OS

EOF
		if [ -n "$FQHOSTNAME" ]; then
			cat >> /etc/issue <<EOF

  Connect to the web interface via:     http://$FQHOSTNAME
  Connect to the api interface via:     http://$FQHOSTNAME:81
  Browse the build packages via:        http://$FQHOSTNAME:82

 * "Admin" user password is "opensuse" by default.
 * Connect to the web interface now to finish the OBS setup.

More informations about this appliance are available here:

 http://en.opensuse.org/Build_Service/OBS-Appliance

                                  Greetings from the openSUSE Build Service Team

EOF
		else
			echo '**********************************************' >> /etc/issue
			echo '**           NETWORK SETUP FAILED           **' >> /etc/issue
			echo '**                                          **' >> /etc/issue
			echo '** OBS is not usable                        **' >> /etc/issue
			echo '** A working DHCP and DNS server in network **' >> /etc/issue
			echo '** is required!                             **' >> /etc/issue
			echo '**********************************************' >> /etc/issue
		fi
		rc_status -v
	;;
	stop)
                # nothing to do
		rc_status -v
	;;
	restart)
                # nothing to do
		rc_status
	;;
	try-restart)
                # nothing to do
		rc_status
	;;
	reload)
                # nothing to do
		rc_status
	;;
	status)
		# nothing to do
		rc_status -v
	;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|reload}"
		exit 1
	;;
esac
rc_exit
