#!/bin/bash

### Licence and Responsibility:
#
# © 2009-2010 Roland Wolters <roland.wolters@credativ.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following 
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the credativ GmbH nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.


# This script tries to avoid NFS performance problems on weak wireless networks.
# It is done by syncing the problematic directories to local disc at login.

### Preparation

TMPHOME="/tmp/$USER"
RSYNCLOCK="/var/lock/rsynclock"

# Remove lock file
rm -f $RSYNCLOCK

### Mozilla

# Create mozilla dir in /tmp, sync everything; also make sure everything works even if
# an earlier session broke down
/bin/mkdir -p "$TMPHOME/.mozilla"
if [ -d "$HOME/.mozilla" ] && [ ! -L "$HOME/.mozilla" ];then
  /usr/bin/rsync -aS --exclude=*Cache --exclude=*mfasl --exclude=*lock --delete "$HOME/.mozilla" "$TMPHOME"
elif [ -d "$HOME/.mozilla.old" ];then
  /usr/bin/rsync -aS --exclude=*Cache --exclude=*mfasl --exclude=*lock --delete "$HOME/.mozilla.old/" "$TMPHOME/.mozilla/"
fi

# Make sure old mozilla files don't get lost.
if [ -d "$HOME/.mozilla" ] && [ ! -d "$HOME/.mozilla.old" ] && [ ! -L "$HOME/.mozilla" ];then
  mv "$HOME/.mozilla" "$HOME/.mozilla.old"
elif [ ! -L "$HOME/.mozilla" ];then
  rm -rf "$HOME/.mozilla"
fi

ln -snf "$TMPHOME/.mozilla" "$HOME/.mozilla"

### OpenOffice

# First check if the config dir is set to the right place.

[ -d "$HOME/.openoffice.org" ] && mv "$HOME/.openoffice.org" "$HOME/.config/.openoffice.org" && ln -snf "$HOME/.config/.openoffice.org" "$HOME/.openoffice.org"

# Create OpenOffice-dir in tmp, sync everything; also make sure everything works even if
# an earlier session broke down.

/bin/mkdir -p "$TMPHOME/.openoffice.org"
if [ -d "$HOME/.config/.openoffice.org" ] && [ ! -L "$HOME/.config/.openoffice.org" ];then
  /usr/bin/rsync -aS --exclude=cache --exclude=*sog --exclude=*sob --exclude=.~lock.* --delete "$HOME/.config/.openoffice.org" "$TMPHOME"
elif [ -d "$HOME/.config/.openoffice.org.old" ];then
  /usr/bin/rsync -aS --exclude=cache --exclude=*sog --exclude=*sob --exclude=.~lock.* --delete "$HOME/.config/.openoffice.org.old/" "$TMPHOME/.openoffice.org/"
fi

# Make sure old mozilla files don't get lost.
if [ -d "$HOME/.config/.openoffice.org" ] && [ ! -d "$HOME/.config/.openoffice.org.old" ] && [ ! -L "$HOME/.config/.openoffice.org" ];then
  mv "$HOME/.config/.openoffice.org" "$HOME/.config/.openoffice.org.old"
elif [ ! -L "$HOME/.config/.openoffice.org" ];then
  rm -rf "$HOME/.config/.openoffice.org"
fi

ln -snf "$TMPHOME/.openoffice.org" "$HOME/.config/.openoffice.org"

### Java

# Create Java dir in /tmp, don't sync; just create.
/bin/mkdir -p "$TMPHOME/.java"
[ -d "$HOME/.java" ] && rm -rf "$HOME/.java"
ln -snf "$TMPHOME/.java" "$HOME/.java"
