#!/bin/bash
MPATH="$1"
BETCDIR='/etc/mcollective'
BRUBYDIR='/Library/Ruby/Site/1.8'
BSBINDIR='/usr/sbin'
BBINDIR='/usr/bin'
BLIBEXECDIR='/usr/libexec/mcollective'
BDOCDIR='/usr/share/doc/mcollective'
BLAUNCHDIR='/Library/LaunchDaemons'
BLOGDIR='/var/log/mcollective'
PACKAGEMAKER='/Applications/PackageMaker.app/Contents/MacOS/PackageMaker'

if [ -z $MPATH ]; then
  echo 'Please give the path to the MCollective source directory'
  exit 1
fi

msg_stomp() {
  echo "It is recommended to install stomp on this system using ruby gems"
  exit 2
}

msg_xcode() {
  echo 'It is required to have the latest XCode installed'
  exit 3
}

# Make sure we have stomp so we can load mcollective
/usr/bin/ruby <<EOF || msg_stomp
require 'rubygems'
require 'stomp'
EOF

# Make sure we have PackageMaker installed
[ -x $PACKAGEMAKER ] || msg_xcode

# Get the MCollective version
export RUBYLIB=$RUBYLIB:$MPATH/lib
mcversion=$(/usr/bin/ruby <<EOF
require 'mcollective'
puts MCollective.version
EOF
)

# Make our tmp directory
tmpbase=`basename $0`
common_tmpdir=`mktemp -d /tmp/${tmpbase}-common_$mcversion.XXXX` || exit 1
client_tmpdir=`mktemp -d /tmp/${tmpbase}-client_$mcversion.XXXX` || exit 1
tmpdir=`mktemp -d /tmp/${tmpbase}_$mcversion.XXXX` || exit 1
pkgdir=`mktemp -d /tmp/${tmpbase}_${mcversion}_packages.XXXX` || exit 1

# Build the common environment
mkdir -p "$common_tmpdir/$BRUBYDIR"
mkdir -p "$common_tmpdir/$BLIBEXECDIR"
mkdir -p "$common_tmpdir/$BDOCDIR"
mkdir -p "$common_tmpdir/$BLOGDIR"

cp -r $MPATH/lib/mcollective     $common_tmpdir/$BRUBYDIR/
cp    $MPATH/lib/mcollective.rb  $common_tmpdir/$BRUBYDIR/
cp -r $MPATH/plugins/mcollective $common_tmpdir/$BLIBEXECDIR/
cp    $MPATH/COPYING             $common_tmpdir/$BDOCDIR/

# Build the server environment
mkdir -p "$tmpdir/$BSBINDIR"
mkdir -p "$tmpdir/$BETCDIR"
mkdir -p "$tmpdir/$BETCDIR/ssl/clients"
mkdir -p "$tmpdir/$BLAUNCHDIR"

cp $MPATH/bin/mcollectived    $tmpdir/$BSBINDIR/mcollectived
cp $MPATH/etc/facts.yaml.dist $tmpdir/$BETCDIR/facts.yaml
cp $MPATH/etc/server.cfg.dist $tmpdir/$BETCDIR/server.cfg

# This is needed for macs since launcd will handle daemonizing
perl -i -pe 's/daemonize = 1/daemonize = 0/' $tmpdir/$BETCDIR/server.cfg

# Build the client environment
mkdir -p "$client_tmpdir/$BETCDIR"
mkdir -p "$client_tmpdir/$BSBINDIR"

cp $MPATH/etc/client.cfg.dist $client_tmpdir/$BETCDIR/client.cfg
cp $MPATH/etc/rpc-help.erb    $client_tmpdir/$BETCDIR/
cp $MPATH/bin/mco             $client_tmpdir/$BSBINDIR/

#Build our launchd property list file
cat - > $tmpdir/$BLAUNCHDIR/org.marionette-collective.mcollective.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/sbin:/usr/sbin:/bin:/usr/bin</string>
                <key>RUBYLIB</key>
                <string>/Library/Ruby/Site/1.8</string>
        </dict>
        <key>Label</key>
        <string>org.marionette-collective.mcollective</string>
        <key>OnDemand</key>
        <false/>
        <key>KeepAlive</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/sbin/mcollectived</string>
                <string>--config=/etc/mcollective/server.cfg</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceDescription</key>
        <string>MCollective Server</string>
        <key>ServiceIPC</key>
        <false/>
</dict>
</plist>
EOF


#Make our Packages.  This requires XCode be installed
$PACKAGEMAKER -r $tmpdir --version $mcversion --title "MCollective" -l / -o $pkgdir/MCollective_$mcversion.pkg -i org.marionette-collective.mcollective
$PACKAGEMAKER -r $common_tmpdir --version $mcversion --title "MCollective Common" -l / -o $pkgdir/MCollective-Common_$mcversion.pkg -i org.marionette-collective.mcollective-common
$PACKAGEMAKER -r $client_tmpdir --version $mcversion --title "MCollective Client" -l / -o $pkgdir/MCollective-Client_$mcversion.pkg -i org.marionette-collective.mcollective-client

# Make sure that we install the stomp gem, this is ugly and should be part of the package
cat - > $pkgdir/MCollective-Common_$mcversion.pkg/Contents/Resources/postflight <<EOF
#!/bin/bash
/usr/bin/gem install --no-ri --no-rdoc stomp
EOF
chmod +x $pkgdir/MCollective-Common_$mcversion.pkg/Contents/Resources/postflight

# launchd complains if the permissions aren't right, this is a dumb hack that
# I needed since PackageMaker doesn't seem to respect permissions (originally had chmod 644 in the tmpdir)
cat - > $pkgdir/MCollective_$mcversion.pkg/Contents/Resources/postflight <<EOF
#!/bin/bash
chmod 644 /Library/LaunchDaemons/org.marionette-collective.mcollective.plist
EOF
chmod +x $pkgdir/MCollective_$mcversion.pkg/Contents/Resources/postflight

# Create a r/o compressed dmg from our pkgs
hdiutil create -srcfolder $pkgdir -format UDZO -scrub -imagekey zlib-level=9 -volname "mcollective-$mcversion" mcollective-$mcversion.dmg

#Clean up
rm -rf $tmpdir
rm -rf $common_tmpdir
rm -rf $client_tmpdir
rm -rf $pkgdir
