#!/bin/bash

# Install Open source Puppet on Darwin (OS X)

GEM=/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/gem
PUPPET=/usr/bin/puppet
CURL=/usr/bin/curl
BASEURI='http://anfadmin.ucsd.edu/puppet'

if [ "${USER}" != 'root' ]; then
    echo "Must run as root"
    exit 1
fi

# Shut down puppet as needed
if [ -f /Library/LaunchDaemons/com.puppetlabs.puppet.plist ]; then
    launchctl unload -w /Library/LaunchDaemons/com.puppetlabs.puppet.plist
fi

$GEM install facter --version 2.4.4 && \
$GEM install hiera  --version 1.3.4 && \
$GEM install puppet --version 3.8.1 && \
$PUPPET resource group puppet ensure=present && \
$PUPPET resource user puppet ensure=present gid=puppet shell='/sbin/nologin' && \
$PUPPET resource file /private/etc/puppet ensure=directory group=puppet && \
$CURL -o /private/etc/puppet/puppet.conf ${BASEURI}/darwin.conf && \
$CURL -o /Library/LaunchDaemons/com.puppetlabs.puppet.plist ${BASEURI}/puppet.plist && \
$PUPPET resource file /private/etc/puppet/puppet.conf ensure=file group=puppet && \
$PUPPET resource file /Library/LaunchDaemons/com.puppetlabs.puppet.plist ensure=file owner=root group=wheel && \
$PUPPET resource service com.puppetlabs.puppet ensure=running enable=true
