view setup @ 513:9066463340a4

Don't add ca to chain.
author edogawaconan <me@myconan.net>
date Thu, 27 Mar 2014 12:18:43 +0900
parents bbcffc594d1e
children cb4e2a6595ea
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 and writable.
test -n "${HOME}" || exit 1
test -w "${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
}

_touch() {
  touch "${@}"
  _echo "Ensured existence of ${@}"
}

_tmux() {
  # Remove old tmux config.
  _rc uninstall "tmux.conf" ".tmux.conf"
  for tmux_conf in "${rcdir}/tmux-"*.conf; do
    tmux_conf_file="`basename "/${tmux_conf}"`"
    _rc "${1}" "${tmux_conf_file}" ".${tmux_conf_file}"
  done
}

_vim_syntax() {
  _echon "Copying vim syntaxes..."
  for i in "${basedir}/vendor/vim-syntax"/*.vim; do
    syntax_file_name="`basename "/${i}"`"
    _echon "[${syntax_file_name}]"
    _rc "${1}" "../vendor/vim-syntax/${syntax_file_name}" ".vim/syntax/${syntax_file_name}" > /dev/null
  done
  _echo ...done
}

_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"
    _touch "${HOME}/.hgrc.local"
    _rc "${1}" "hgignore_global" ".hgignore_global"
    _touch "${HOME}/.hgignore_global.local"
    _rc "${1}" "inputrc" ".inputrc"
    _tmux "${1}"
    _rc "${1}" "vim-base16-default" ".vim/colors/base16-default.vim"
    _vim_syntax "${1}"
    _rc "${1}" "vim-autoload-pathogen" ".vim/autoload/pathogen.vim"
    _rc "${1}" "vimrc" ".vimrc"
    _rc "${1}" "irbrc" ".irbrc"
  ;;
  *)
    _help
  ;;
esac