#!/bin/sh -e

reload_apache()
{
    if apache2ctl configtest 2>/dev/null; then
	if [ -x /usr/sbin/invoke-rc.d ]; then
	    invoke-rc.d apache2 $1 3>/dev/null || true && \
	    echo "Apache restarted."
	else
	    /etc/init.d/apache2 $1 3>/dev/null || true && \
	    echo "Apache restarted."
	fi
    else
	echo "Your Apache 2 configuration is broken, so we're not restarting it for you."
    fi
}

reload_obsapi()
{
    systemctl "$1" obsapi-clockwork.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-consistency_check.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-default.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-issuetracking.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-mailers.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-project_log_rotate.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-quick.service 3>/dev/null || true
    systemctl "$1" obsapi-delayedjob-queue-releasetracking.service 3>/dev/null || true
    echo "obsapi reloaded."
}

case "$1" in
	setup)

	# Generate Gemfile.lock file.
	cd /usr/share/obs/api
	rm -f Gemfile.lock
	rm -f .bundle/config
	bundle --local --quiet

	# Setup database
	RAILS_ENV=production bundle exec rake db:create >> log/db_setup.log || true
	RAILS_ENV=production bundle exec rake db:setup >> log/db_setup.log || true
	RAILS_ENV=production bundle exec writeconfiguration >> log/db_setup.log || true

	export BUNDLE_WITHOUT=test:assets:development
	export BUNDLE_FROZEN=1
	bundle config --local frozen 1
	bundle config --local without test:assets:development

	API_ROOT=/usr/share/obs/api

	run_in_api () {
	export RAILS_ENV="production"
	echo "Run in api."
	chroot --userspec=www-data:www-data / /bin/bash -c "cd $API_ROOT && bundle exec $*"
	}

	run_in_api rake assets:precompile RAILS_ENV=production RAILS_GROUPS=assets
	run_in_api rake ts:index RAILS_ENV=production || true

    # Stop delayed jobs and queues that might be running based on the old codebase.
    # This is a workaround to migrate from obsapidelayed init script to multiple systemd unit files.
    run_in_api script/delayed_job.api.rb stop RAILS_ENV=production || true
    run_in_api clockworkd -l -c config/clock.rb stop || true

    # Start up obsapi daemons
    reload_obsapi start

	# Test whether a2enmod is available (and thus also apache2ctl).
	if [ -x /usr/sbin/a2enmod ]; then
		# Enable the Apache2 modules if not already enabled
		a2enmod ssl     > /dev/null || true
		a2enmod rewrite > /dev/null || true
		a2enmod proxy   > /dev/null || true
		a2enmod proxy_http      > /dev/null || true
		a2enmod xforward        > /dev/null || true
		a2enmod headers > /dev/null || true
		a2enmod expires > /dev/null || true
		a2dissite 000-default   > /dev/null || true
		a2ensite obs.conf	> /dev/null || true
	fi

	# Restart Apache to really enable the module and load obs.conf
	reload_apache restart
	;;
    migrate)
        # Migrate the database
        cd /usr/share/obs/api
        RAILS_ENV=production bundle exec rake db:migrate:with_data >> log/db_migrate.log

        # Destroy any existent delayed job based on the old codebase
        echo 'Delayed::Job.destroy_all' | RAILS_ENV=production bundle exec rails console >> log/destroy_all_jobs.log

        # Restart all daemons
        reload_obsapi restart

        # Restart Apache to really enable the module and load obs.conf
        reload_apache restart
	;;
    *)
	echo "Usage: $0 {setup|migrate}"
	exit 1
    ;;
esac
