#!/bin/zsh
# ====================[ zshrc.ssh                          ]====================
#                     [ Time-stamp: "2009-04-18 19:29:36 leycec" ]
#
# --------------------( SYNOPSIS                           )--------------------
# SSH- and SSHfs-specific aliases, functions, and data structures. ".zshrc"
# includes this file so as to globally expose these definitions on startup.

# ....................{ VARIABLES                          }....................
# Declare a global associative array mapping from SSH hostname to the local
# pathname at which that hostname is to be mounted using SSHfs. SSH hostnames
# correspond to "Host" options declared in "/etc/ssh/ssh_config".
typeset -A SSH_HOST_TO_MOUNT_POINT
SSH_HOST_TO_MOUNT_POINT[omecha.org]="/home/leycec/mnt/omecha.org/"
SSH_HOST_TO_MOUNT_POINT[raiazome.com]="/home/leycec/mnt/raiazome.com/"
SSH_HOST_TO_MOUNT_POINT[sdf]="/home/leycec/mnt/sdf/"

# Declare shortcuts to the above mount points.
SSH_HOST_TO_MOUNT_POINT[omecha]=$SSH_HOST_TO_MOUNT_POINT[omecha.org]
SSH_HOST_TO_MOUNT_POINT[raia]=$SSH_HOST_TO_MOUNT_POINT[raiazome.com]

# ....................{ ALIASES                            }....................
# Shortcut aliases.
alias sshm="ssh-mount"
alias sshu="ssh-umount"

# FUSE aliases.
alias sshfs="sshfs -o allow_other,kernel_cache,reconnect,transform_symlinks,compression=yes,cache_timeout=256,cache_stat_timeout=16,cache_dir_timeout=16,cache_link_timeout=16,idmap=user,workaround=rename"
alias sshfs-umount="fusermount -uz"

# FUSE aliases. (SDF-specific.)
alias sdf-mount="sdf-umount; sshfs -o uid=1000,gid=1000 sdf: ~/mnt/sdf; sshfs -o uid=1000,gid=1000 sdf:/www/af/b/bcurry /www/af/b/bcurry"
alias sdf-umount="sshfs-umount ~/mnt/sdf; sshfs-umount /www/af/b/bcurry"

# SSH aliases.
alias scp="scp -Crv"
alias ssh="TERM=\"xterm\" ssh -v"
alias ssh-keychain-restore="keychain --clear; keychain --agents ssh --quick ~/.ssh/id_rsa"
alias ssh-secondary-control-master="ssh -o 'ControlPath ~/tmp/ssh_controlmaster2-%r@%h:%p'"

# ....................{ FUNCTIONS                          }....................
# Mount the passed SSH hostname to its corresponding mount point.
ssh-mount() {
  if [ -z "$1" ]; then
    echo "ssh-mount: no SSH host passed!" 1>&2
    return 1
  fi

  local ssh_host="$1"
  local mount_point="$SSH_HOST_TO_MOUNT_POINT[$ssh_host]"

  if [ -z "$mount_point" ]; then
    echo "ssh-mount: '$ssh_host' not a recognized host!" 1>&2
    return 1
  fi

  if [ ! -d "$mount_point" ]; then
    echo "ssh-mount: creating '$mount_point'..."
    mkdir --parents "$mount_point"
  fi

  ssh-umount "$ssh_host"

  echo "ssh-mount: mounting '$ssh_host' to '$mount_point'..."
  sshfs "$ssh_host": "$mount_point"
}

# Unmount the passed SSH hostname from its corresponding mount point.
ssh-umount() {
  if [ -z "$1" ]; then
    echo "ssh-umount: no SSH host passed!" 1>&2
    return 1
  fi

  local ssh_host="$1"
  local mount_point="$SSH_HOST_TO_MOUNT_POINT[$ssh_host]"

  if [ -z "$mount_point" ]; then
    echo "ssh-umount: '$ssh_host' not a recognized host!" 1>&2
    return 1
  fi

  echo "ssh-umount: unmounting '$ssh_host' from '$mount_point'..."
  sshfs-umount "$mount_point"
}

# --------------------( COPYRIGHT AND LICENSE              )--------------------
# The information below applies to everything in this distribution,
# except where noted.
#              
# Copyleft 2007, 2008, and 2009 by B.w.Curry.
#   
#   http://www.raiazome.com
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
