#!/bin/bash

# force prompt for installation of java
java -version &

if [ -f /Library/LaunchDaemons/com.lassosoft.lassoinstancemanager.plist ]; then
	launchctl unload /Library/LaunchDaemons/com.lassosoft.lassoinstancemanager.plist
fi

killall -9 lassoim
killall -9 lassoserver

##########
##### Create/update the Lasso user & group
##########

##### Check if the Lasso group exists, and create one if needed.
if ( ! dscl . -list /groups PrimaryGroupID | grep -qxe "_lasso *14550" )
then
	echo "Creating Lasso group"
	if ( dscl . -list /groups PrimaryGroupID | grep -qxe "lasso *14550" )
	then
		dscl . -create /groups/lasso RecordName _lasso
	else
		dscl . -create /groups/_lasso
	fi
	dscl . -append /groups/_lasso RecordName lasso
	dscl . -create /groups/_lasso RealName "Lasso Group"
	dscl . -create /groups/_lasso PrimaryGroupID 14550
	dscl . -create /groups/_lasso Password \*
fi

##### Check if the Lasso user exists, and create one if needed.
if ( ! dscl . -list /users UniqueID | grep -qxe "_lasso *14550" )
then
	echo "Creating Lasso user"
	if ( dscl . -list /users UniqueID | grep -qxe "lasso *14550" )
	then
		dscl . -create /users/lasso RecordName _lasso
	else
		dscl . -create /users/_lasso
	fi
	dscl . -append /users/_lasso RecordName lasso
	dscl . -create /users/_lasso RealName "Lasso User"
	dscl . -create /users/_lasso NFSHomeDirectory /var/empty
	dscl . -create /users/_lasso UserShell /usr/bin/false
	dscl . -create /users/_lasso UniqueID 14550
	dscl . -create /users/_lasso PrimaryGroupID 14550
	dscl . -create /users/_lasso Password \*
	dscl . -create /users/_lasso Expire 0
fi

##### Remove unused user attributes added in OS X Lion.
dscl . -delete /users/_lasso AuthenticationAuthority
dscl . -delete /users/_lasso PasswordPolicyOptions
dscl . -delete /users/_lasso dsAttrTypeNative:KerberosKeys
dscl . -delete /users/_lasso dsAttrTypeNative:ShadowHashData
dscl . -delete /users/_lasso dsAttrTypeNative:destroy_on_uninstall

##### Add the Lasso user to the Lasso group.
if ( ! dscl . -read /groups/_lasso GroupMembership | grep -q "_lasso" )
then
	echo "Adding Lasso user to the Lasso group"
	dscl . -create /groups/_lasso GroupMembership _lasso
fi
