view setup @ 127:a7484f2c9616

Long-overdue massive reorganization.
author Edho Prima Arief <edho@myconan.net>
date Thu, 27 Oct 2011 16:23:23 +0700
parents 9a15f3d1b5b0
children 66a1d79c4161
line wrap: on
line source

#!/bin/sh

basedir="`dirname "${0}"`"
cd "${basedir}"
basedir="`pwd`"
rcdir="${basedir}/rc"
bindir="${basedir}/bin"

test -n "${HOME}" || exit 1

_echo() {
  _echon "${*}"; printf "\n"
}
_echon() {
  printf "%s" "${*}"
}

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

_bash() {
  case "$1" in
    uninstall|install)
      _rc "${1}" "bash" ".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}" "zsh" ".ecos_zsh"
      _init "${1}" '[ -r "${HOME}/.ecos_zsh" ] && . "${HOME}/.ecos_zsh" #ECCORE_ZSH' ".zshrc"
    ;;
  esac
}

_rc() {
  _srcfile="${rcdir}/${2}"
  _dstfile="${HOME}/${3}"
  if test ! -h "${_dstfile}"; then
    if test -f "${_dstfile}"; then
      cp "${_dstfile}" "${_dstfile}.bak"
    elif test -d "${_dstfile}"; then
      cp -r "${_dstfile}" "${_dstfile}.bak"
    fi
    _echo "${_dstfile} backed up to ${_dstfile}.bak"
  fi
  rm -f "${_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}"
  _data="${2}"
  if [ -f "${_script}" ]; then
    grep -v "${_data}" "${_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
}

linker() {
  echo "Creating symlink: ${1} => ~/${2}"
  [ -f "${1}" ] && rm "${PWD}/$"
  ln -fs "${PWD}/${1}" "${HOME}/${2}"
}

_uninstall_init() {
  script="${1}"
  scripttmp="${1}.new.tmp"
  hashdata="${2}"
  if [ -f "${script}" ]; then
    grep -v "${hashdata}" "${script}" > "${scripttmp}"
    mv "${scripttmp}" "${script}"
  fi
}

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

case "${1}" in
  install|uninstall)
    for i in tcsh bash zsh; do
      _"${i}" "${1}"
    done
    _rc "${1}" "tmux" ".tmux.conf"
    _rc "${1}" "hg" ".hgrc"
    _rc "${1}" "vim" ".vimrc"
    _rc "${1}" "inputrc" ".inputrc"
    _rc "${1}" "bin" ".ecos_bin"
  ;;
  *)
    _help
  ;;
esac