#!/bin/sh

# https://trac.macports.org/wiki/howto/SetupFetchmail
# Last tested with Mac OS X 10.9.3

# /System/Library/CoreServices/Directory\ Utility.app can be helpful

# Get next available group ID
MAXID=$(dscl . -list groups gid | awk '{print $2}' | sort -ug | tail -1)
GROUPID=$((MAXID+1))

# Get next available user ID
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
USERID=$((MAXID+1))

# To view: sudo dscl . -read /Groups/_fetchmail
# To delete: sudo dscl . -delete /Groups/_fetchmail
sudo dscl . -create /Groups/_fetchmail
sudo dscl . -create /Groups/_fetchmail gid $GROUPID

# To view: sudo dscl . -read /Users/_fetchmail
# To delete: sudo dscl . -delete /Users/_fetchmail
sudo dscl . -create /Users/_fetchmail
sudo dscl . -create /Users/_fetchmail UserShell /bin/false
sudo dscl . -create /Users/_fetchmail RealName "Fetchmail Mail Client"
sudo dscl . -create /Users/_fetchmail UniqueID $USERID
sudo dscl . -create /Users/_fetchmail PrimaryGroupID $GROUPID
sudo dscl . -create /Users/_fetchmail NFSHomeDirectory /opt/local/var/run/fetchmail

# To view - defaults read /Library/Preferences/com.apple.loginwindow
sudo defaults write /Library/Preferences/com.apple.loginwindow HiddenUsersList -array-add _fetchmail
