view setup @ 463:4180a94a80af

Add FreeBSD tcsh compatibility.
author Edho Arief <edho@myconan.net>
date Mon, 29 Apr 2013 14:27:44 +0900
parents e0f9a54b9dce
children 8ef5ddb9b54b
line wrap: on
line source

#!/bin/sh

# This script is bourne-shell compatible.
# Or at least it is when I last tested it in Solaris 10.

# Ensure no variable is empty when used.
set -u
# Stop on any error.
set -e

# Get directory where this script and dotfiles are located.
# $0 contains either full path when executed from a PATH (godwhy)
# or with complete path or ./<scriptname> when executed that way.
# Therefore it's safe to use dirname in this case.
basedir="`dirname "${0}"`"

# To get actual path, go to the directory and pwd from it.
cd "${basedir}"
basedir="`pwd`"

# FIXME: test existence of the directories.
# dotfiles
rcdir="${basedir}/rc"
# misc scripts
bindir="${basedir}/bin"

# Make sure $HOME points somewhere.
# FIXME: ensure it is writable (or maybe not).
test -n "${HOME}" || exit 1

# A safe echo.
_echo() {
  _echon "${*}"; printf "\n"
}
# A safe and cross platform echo -n.
_echon() {
  printf '%s' "${*}"
}

_tcsh() {
  case "$1" in
    uninstall|install)
      _rc "${1}" "cshrc" ".ecos_tcsh"
      _init "${1}" 'test -r "${HOME}/.ecos_tcsh" && source "${HOME}/.ecos_tcsh" #ECCORE_TCSH' ".cshrc"
      _init "${1}" 'test -r "${HOME}/.ecos_tcsh" && source "${HOME}/.ecos_tcsh" #ECCORE_TCSH' ".tcshrc"
    ;;
  esac
}

_bash() {
  case "${1}" in
    uninstall|install)
      _rc "${1}" "bashrc" ".ecos_bash"
     _init "${1}" '[ -r "${HOME}/.ecos_bash" ] && . "${HOME}/.ecos_bash" #ECCORE_BASH' ".bash_profile"
     _init "${1}" '[ -r "${HOME}/.ecos_bash" ] && . "${HOME}/.ecos_bash" #ECCORE_BASH' ".bashrc"
    ;;
  esac
}

_zsh() {
  case "${1}" in
    uninstall|install)
      _rc "${1}" "zshrc" ".ecos_zsh"
      _init "${1}" '[ -r "${HOME}/.ecos_zsh" ] && . "${HOME}/.ecos_zsh" #ECCORE_ZSH' ".zshrc"
    ;;
  esac
}

_rc() {
  if test -z "${3}" || test -z "${2}" || test -z "${1}"; then
    _echo "Invalid command."
    return
  fi
  _srcfile="${rcdir}/${2}"
  _dstfile="${HOME}/${3}"
  _dstdir="`_echo "${_dstfile}" | sed -e 's,^\(.*\)/[^/]*$,\1,'`"
  mkdir -p "${_dstdir}"
  if test ! -h "${_dstfile}"; then
    if test -f "${_dstfile}"; then
      cp "${_dstfile}" "${_dstfile}.bak"
      _echo "${_dstfile} backed up to ${_dstfile}.bak"
    elif test -d "${_dstfile}"; then
      cp -r "${_dstfile}" "${_dstfile}.bak"
      _echo "Directory ${_dstfile} backed up to ${_dstfile}.bak"
    fi
  fi
  rm -rf "${_dstfile}"
  case "${1}" in
    install)
      ln -fs "${_srcfile}" "${_dstfile}"
      _echo "Installed ${_dstfile}"
    ;;
    uninstall)
      _echo "Removed ${_dstfile}"
    ;;
  esac
}

_init() {
  _script="${HOME}/${3}"
  _scripttmp="${HOME}/.tmp.${3}"
  rm -f "${_scripttmp}"
  _data="${2}"
  if [ -f "${_script}" ]; then
    sed -e "/`_echo "${_data}" | sed -e 's/.*#//'`/d" "${_script}" > "${_scripttmp}"
    mv -f "${_scripttmp}" "${_script}"
  fi
  case "${1}" in
    install)
      _echo "${_data}" >> "${_script}"
      _echo "Added autostart to ${_script}"
    ;;
    uninstall)
      _echo "Removed autostart from ${_script}"
    ;;
  esac
}

_help() {
  cat <<EOF
Usage: ${0} [install|uninstall]
EOF
}

case "${1}" in
  install|uninstall)
    _tcsh "${1}"
    _bash "${1}"
    _zsh "${1}"
    _rc "${1}" "../bin" ".ecos_bin"
    _rc "${1}" "gemrc" ".gemrc"
    _rc "${1}" "gitconfig" ".gitconfig"
    _rc "${1}" "gitignore_global" ".gitignore_global"
    _rc "${1}" "hgrc" ".hgrc"
    _rc "${1}" "inputrc" ".inputrc"
    _rc "${1}" "tmux.conf" ".tmux.conf"
    _rc "${1}" "vim-solarized" ".vim/colors/solarized.vim"
    _rc "${1}" "vim-syntax-coffee" ".vim/syntax/coffee.vim"
    _rc "${1}" "vim-syntax-scss" ".vim/syntax/scss.vim"
    _rc "${1}" "vim-autoload-pathogen" ".vim/autoload/pathogen.vim"
    _rc "${1}" "vimrc" ".vimrc"
  ;;
  *)
    _help
  ;;
esac