#!/bin/bash -ex
# By Adrian Moya <adrian@turnkeylinux.org>

install()
{
    DEBIAN_FRONTEND=noninteractive apt-get -y \
        -o DPkg::Options::=--force-confdef \
        -o DPkg::Options::=--force-confold \
        install $@
}

# Set Hostname
HOSTNAME=jenkins
echo "$HOSTNAME" > /etc/hostname
sed -i "s|127.0.1.1 \(.*\)|127.0.1.1 $HOSTNAME|" /etc/hosts

# Add jenkins repo
wget -q -O - http://pkg.jenkins-ci.org/debian-stable/jenkins-ci.org.key | apt-key add -
echo "deb http://pkg.jenkins-ci.org/debian-stable binary/" > /etc/apt/sources.list.d/jenkins.list

# Update package information
apt-get update

# Install required packages
install jenkins apache2 postfix

# Enable apache2 modules
a2enmod ssl proxy proxy_http

# Enable apache2 sites
a2dissite default default-ssl
a2ensite jenkins jenkins-ssl

# Change ownership to overlayed files
chown -R jenkins:nogroup /var/lib/jenkins

# Stop services
service apache2 stop
service jenkins stop
service postfix stop

# Clean apt cache
apt-get clean
