view setup @ 674:7423d2c773e4

Merge
author nanaya <me@nanaya.pro>
date Tue, 05 Apr 2022 13:55:57 +0900
parents 850b04203732
children 17cacf387f7c
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" ".ec.tcsh"
      _init "${1}" 'test -r "${HOME}/.ec.tcsh" && source "${HOME}/.ec.tcsh" #ECCORE_TCSH' ".cshrc"
      _init "${1}" 'test -r "${HOME}/.ec.tcsh" && source "${HOME}/.ec.tcsh" #ECCORE_TCSH' ".tcshrc"
    ;;
  esac
}

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

_ksh() {
  case "${1}" in
    uninstall|install)
      _rc "${1}" "kshrc" ".ec.ksh"
      _init "${1}" '[ -r "${HOME}/.ec.ksh" ] && . "${HOME}/.ec.ksh" #ECCORE_KSH' ".profile"
    ;;
  esac
}

_zsh() {
  case "${1}" in
    uninstall|install)
      _rc "${1}" "zshrc" ".ec.zsh"
      _init "${1}" '[ -r "${HOME}/.ec.zsh" ] && . "${HOME}/.ec.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() {
  test "${1}" = "uninstall" && return
  shift

  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_x() {
  case "${1}" in
    install) _echon "Copying";;
    uninstall) _echon "Removing";;
  esac
  x="${2}"
  _echon " vim ${x}..."

  for i in "${basedir}/vendor/vim-${x}"/*.vim; do
    file_name="`basename "/${i}"`"
    _echon "[${file_name}]"
    _rc "${1}" "../vendor/vim-${x}/${file_name}" ".vim/${x}/${file_name}" > /dev/null
  done
  _echo ...done
}

_upgrade() {
  for i in bash bash.after bash.before bin tcsh tcsh.after tcsh.before zsh zsh.after zsh.before; do
    src="${HOME}/.ecos_${i}"
    dst="${HOME}/.ec.${i}"
    if test -f "${src}" || test -d "${src}" || test -h "${src}"; then
      if test ! -f "${dst}" && test ! -d "${dst}" && test ! -h "${dst}"; then
        _echo "Moving ${src} to ${dst}"
        mv -f "${src}" "${dst}"
      fi
    fi
  done
}

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

case "${1}" in
  install|uninstall)
    _upgrade
    _tcsh "${1}"
    _bash "${1}"
    _ksh "${1}"
    _zsh "${1}"
    _rc "${1}" "shellinit-posix" ".ec.shellinit-posix"
    _rc "${1}" "../bin" ".ec.bin"
    _rc "${1}" "gemrc" ".gemrc"
    _rc "${1}" "gitconfig" ".gitconfig"
    _rc "${1}" "gitignore_global" ".gitignore_global"
    _rc "${1}" "hgrc" ".hgrc"
    _touch "${1}" "${HOME}/.hgrc.local"
    _rc "${1}" "hgignore_global" ".hgignore_global"
    _touch "${1}" "${HOME}/.hgignore_global.local"
    _rc "${1}" "inputrc" ".inputrc"
    _tmux "${1}"
    _vim_x "${1}" autoload
    _vim_x "${1}" colors
    _vim_x "${1}" syntax
    _rc "${1}" "vimrc" ".vimrc"
    _rc "${1}" "irbrc" ".irbrc"
  ;;
  *)
    _help
  ;;
esac