#!/bin/sh 
# 
# sshfs_automount
#	- automount remote filesystems with sshfs
#
# author: Greg Boyington <greg@automagick.us>
#
# Referenced in the following article:
# http://mac.automagick.us/persistent_volumes.mhtml

# where to find the airport utility
AIRPORT=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport

# the sshfs executable
SSHFS=/Applications/sshfs.app/Contents/Resources/sshfs-static-10.5

# Your home network's SSID, signalling that internal hostnames should resolve
HOMESSID=vroomchili

# function remount( mount_point, user, local_host, remote_host, path, volume_name )
# 
# Uses sshfs to mount a remote filesystem, using either internal or external hostnames
# depending on what SSID airport is reporting.  
#
function remount() {
	VOLNAME=$1
	USER=$2
	LOCAL=$3
	REMOTE=$4
	REMOTEPATH=$5
	TARGET="/Volumes/$VOLNAME"
	if [ -d $TARGET ] 
	then
		if [ "`/sbin/mount|/usr/bin/grep $TARGET`" ]
		then
			/usr/bin/hdiutil eject $TARGET
			sleep 1
		else 
			/bin/rmdir $TARGET
		fi
	fi
	SSID=`$AIRPORT --getinfo| /usr/bin/sed -ne 's/^.*[[:space:]]SSID:[[:space:]]*\(.*\)/\1/p'`
	if [ $SSID != "$HOMESSID" ]
	then 
		HOST=$REMOTE
	else 
		HOST=$LOCAL
	fi
	/bin/mkdir $TARGET
	$SSHFS $USER@$HOST:$REMOTEPATH $TARGET -oreconnect,noappledouble,volname=$VOLNAME
}

# Sample Usage:
# remount 'remote_files' 'user' 'fileserv' 'fileserv.automagick.us' '/usr/local/www'
