#!/bin/bash

JENKINS_PLIST="/Library/LaunchDaemons/org.jenkins-ci.plist"
DEFAULTS_PLIST="/Library/Preferences/org.jenkins-ci.plist"

# Because PackageMaker just copies the components, we need to fix the permissions
chown root:wheel ${JENKINS_PLIST}
chmod 644 ${JENKINS_PLIST}

JENKINS_HOMEDIR="/Users/Shared/Jenkins"
mkdir -p $JENKINS_HOMEDIR

if dscl . -list /Users/jenkins; then
    echo 'jenkins user already exists, attempting to change the shell to /bin/bash'
    # Will fail if UserShell is not /usr/bin/false, but that's ok.
    # Then we will assume an admin has changed it.
    dscl . -change /Users/jenkins UserShell /usr/bin/false /bin/bash
else
    echo 'No jenkins user found, creating jenkins user and group'

# Find free uid under 500
    uid=$(dscl . -list /Users uid | sort -nrk 2 | awk '$2 < 500 {print $2 + 1; exit 0}')
    if [ $uid -eq 500 ]; then
        echo 'ERROR: All system uids are in use!'
        exit 1
    fi
    echo "Using uid $uid for jenkins"

    gid=$uid
    while dscl -search /Groups gid $gid | grep -q $gid; do
        echo "gid $gid is not free, trying next"
        gid=$(($gid + 1))
    done
    echo "Using gid $gid for jenkins"

    dscl . -create /Groups/jenkins PrimaryGroupID $gid

    dscl . -create /Users/jenkins UserShell /bin/bash
    dscl . -create /Users/jenkins Password '*'
    dscl . -create /Users/jenkins UniqueID $uid
    dscl . -create /Users/jenkins PrimaryGroupID $gid
    dscl . -create /Users/jenkins NFSHomeDirectory "$JENKINS_HOMEDIR"

    dscl . -append /Groups/jenkins GroupMembership jenkins
fi

find "$JENKINS_HOMEDIR" \( -not -user jenkins -or -not -group jenkins \) -print0 | xargs -0 chown jenkins:jenkins

# Add defaults for heap sizing
arch=$(uname -m)
if [ $arch = 'x86_64' ]; then
    defaults write $DEFAULTS_PLIST heapSize 512m
    defaults write $DEFAULTS_PLIST permGen 512m
    defaults write $DEFAULTS_PLIST minHeapSize 256m
    defaults write $DEFAULTS_PLIST minPermGen 256m
else
    # i386
    defaults write $DEFAULTS_PLIST heapSize 128m
    defaults write $DEFAULTS_PLIST permGen 128m
    defaults write $DEFAULTS_PLIST minHeapSize 64m
    defaults write $DEFAULTS_PLIST minPermGen 64m    
fi

# Set tmpdir
JENKINS_TMPDIR="$JENKINS_HOMEDIR/tmp"
defaults write $DEFAULTS_PLIST tmpdir $JENKINS_TMPDIR
mkdir -p $JENKINS_TMPDIR
chown jenkins:jenkins $JENKINS_TMPDIR

# Create log directory, which can be written by Jenkins daemon
mkdir -p /var/log/jenkins
chown jenkins:jenkins /var/log/jenkins

# Enable log rotation by newsyslog
cat <<_EOT_ > /etc/newsyslog.d/jenkins.conf
# logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
# Rotate jenkins log at midnight, and preserve old logs in 3 days
/var/log/jenkins/jenkins.log            644  3     *    \$D0   J
_EOT_

# Load and start the launch daemon
/bin/launchctl load -w ${JENKINS_PLIST}

# Wait for port 8080 to start accepting connections.
# But don't wait forever.
timeout=$(($(date +%s) + 60))
while [ $(date +%s) -lt $timeout ] && ! curl -s http://localhost:8080 >/dev/null; do
    sleep 1
done

if [ $(date +%s) -ge $timeout ]; then
    echo "Timed out waiting for Jenkins port 8080 to start listening!"
    echo "Either Jenkins did not load or this system is very slow."
fi
