#!/bin/bash

set -e

function create_group {
    # Check to see if it exists
    CONDOR_GID=$(dscl . -read /Groups/condor PrimaryGroupID 2>/dev/null | awk '{print $2}')
    if ! [ -z "$CONDOR_GID" ]; then
        return
    fi

    # If it doesn't exist, then create it
    LAST_GID=$(dscl . -list /Groups PrimaryGroupID | awk '{print $2}' | sort -n | tail -1)
    CONDOR_GID=$((LAST_GID + 1))
    dscl . -create /Groups/condor
    dscl . -create /Groups/condor PrimaryGroupID $CONDOR_GID
}

function create_user {
    # Check to see if it exists
    CONDOR_UID=$(dscl . -read /Users/condor UniqueID 2>/dev/null | awk '{print $2}')
    if ! [ -z "$CONDOR_UID" ]; then
        return
    fi

    LAST_UID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1)
    CONDOR_UID=$((LAST_UID + 1))
    dscl . -create /Users/condor
    dscl . -create /Users/condor UniqueID $CONDOR_UID
    dscl . -create /Users/condor PrimaryGroupID $CONDOR_GID
}

# Create condor group
create_group

# Create condor user
create_user

# Create configuration if none exists
if ! [ -d /etc/condor ]; then
    mkdir -p /etc/condor/config.d

    # Install config files
    cp /usr/share/condor/etc/examples/condor_config /etc/condor
    cp /usr/share/condor/etc/examples/condor_config.local /etc/condor

    # Change default config values in condor_config
    sed -i '' \
        -e 's/^RELEASE_DIR/#RELEASE_DIR/' \
        -e 's/^LOCAL_DIR/#LOCAL_DIR/' \
        -e 's/^LOCAL_CONFIG_FILE/#LOCAL_CONFIG_FILE/' \
        -e 's/^LOCAL_CONFIG_DIR/#LOCAL_CONFIG_DIR/' \
        /etc/condor/condor_config
    cat >> /etc/condor/condor_config <<END
RELEASE_DIR = /usr
LOCAL_CONFIG_FILE = /etc/condor/condor_config.local
LOCAL_CONFIG_DIR = /etc/condor/config.d
REQUIRE_LOCAL_CONFIG_FILE = TRUE
LOG = /var/log/condor
SPOOL = /var/spool/condor
EXECUTE = /var/lib/condor
END

    # Change local config values
    cat >> /etc/condor/condor_config.local <<END

BIN = /usr/bin
SBIN = /usr/sbin
LIB = /usr/lib/condor
LIBEXEC = /usr/libexec/condor
INCLUDE = /usr/include/condor
MAIL = /usr/libexec/condor/condor_mail

CONDOR_IDS = $CONDOR_UID.$CONDOR_GID
END

    MODEL=$(sysctl -n hw.model)
    if [[ "$MODEL" =~ .*Book.* ]]; then
        cat >> /etc/condor/condor_config.local <<END

# On laptops we set everything to run on localhost because it
# causes fewer problems when the laptop moves around in the network
CONDOR_HOST = localhost
NETWORK_INTERFACE = 127.0.0.1
UID_DOMAIN = localhost
TRUST_UID_DOMAIN = TRUE
END
    fi

    chown -R condor:condor /etc/condor
fi

# Create var directories
mkdir -p /var/log/condor /var/lib/condor /var/spool/condor
chown -R condor:condor /var/log/condor /var/lib/condor /var/spool/condor

# Install python libraries
PYVER=$(python -V 2>&1 | cut -d" " -f2 | cut -d"." -f1,2)
PYSITE="/Library/Python/$PYVER/site-packages"
if [ -d "$PYSITE" ]; then
    for f in htcondor.so classad.so; do
        if [ -e "/usr/lib/condor/$f" ]; then
            if [ -e "$PYSITE/$f" ]; then
                rm -f $PYSITE/$f
            fi
            ln -s /usr/lib/condor/$f $PYSITE/
        fi
    done
fi

# Set up service with launchd
cat > /Library/LaunchDaemons/edu.wisc.cs.htcondor.plist <<END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>edu.wisc.cs.htcondor</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/sbin/condor_master</string>
    <string>-f</string>
    <string>-c</string>
    <string>/etc/condor/condor_config</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
</dict>
</plist>
END

/bin/launchctl load "/Library/LaunchDaemons/edu.wisc.cs.htcondor.plist"

