#!/bin/bash

set -x -e

emacsdir=~/.emacs.d
mylispdir=$emacsdir/my

mkdir -p $mylispdir
rm -f $emacsdir/autoload-my.el
rm -f ~/.emacs
[ -d $emacsdir/my-lisp ] && mv $emacsdir/my-lisp $mylispdir
for f in .emacs-my .emacs-custom.el .emacs-pre .emacs-post .emacs-places .emacs.desktop .emacs-places .emacs-autogen .ido.last .recentf; do
  if [ -f ~/$f ]; then [ -f $emacsdir/$f ] && mv ~/$f $emacsdir/$f.1 || mv ~/$f $emacsdir; fi
  if [ -f $mylispdir/$f ]; then [ -f $emacsdir/$f ] && mv $mylispdir/$f $emacsdir/$f.2 || mv $mylispdir/$f $emacsdir; fi
done
for f in `find $mylispdir -maxdepth 1 -type f -name '.emacs?*'`; do
  fn=${f##*/}
  [ -f $emacsdir/$fn ] && mv $f $emacsdir/$fn.3 || mv $f $emacsdir
done
if [ -f ~/.emacs.bmk ]; then [ -f $emacsdir/bookmarks ] && mv ~/.emacs.bmk $emacsdir/bookmarks.$$ || mv ~/.emacs.bmk $emacsdir/bookmarks; fi
if [ -f $emacsdir/.emacs.bmk ]; then [ -f $emacsdir/bookmarks ] && mv $emacsdir/.emacs.bmk $emacsdir/bookmarks.$$ || mv $emacsdir/.emacs.bmk $emacsdir/bookmarks; fi
