view setup @ 716:42e58cea7821

[vimrc] Remove unused craps
author nanaya <me@nanaya.net>
date Mon, 19 Dec 2022 05:39:15 +0900
parents 500c3e9bf73f
children 49538e2aad65
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"

# 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"
      # shellcheck disable=SC2016
      _init "${1}" 'test -r "${HOME}/.ec.tcsh" && source "${HOME}/.ec.tcsh" #ECCORE_TCSH' ".cshrc"
      # shellcheck disable=SC2016
      _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"
      # shellcheck disable=SC2016
     _init "${1}" '[ -r "${HOME}/.ec.bash" ] && . "${HOME}/.ec.bash" #ECCORE_BASH' ".bash_profile"
      # shellcheck disable=SC2016
     _init "${1}" '[ -r "${HOME}/.ec.bash" ] && . "${HOME}/.ec.bash" #ECCORE_BASH' ".bashrc"
    ;;
  esac
}

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

_zsh() {
  case "${1}" in
    uninstall|install)
      _rc "${1}" "zshrc" ".ec.zsh"
      # shellcheck disable=SC2016
      _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
  for d in "${basedir}/vendor/vim-${x}"/*/; do
    test -d "$d" || continue
    dir_name="`basename "/${d}"`"
    _echon "[${dir_name}]"
    mkdir -p ".vim/${x}/${dir_name}"
    for i in "${d}"/*.vim; do
      file_name="`basename "/${i}"`"
      _echon "[${file_name}]"
      _rc "${1}" "../vendor/vim-${x}/${dir_name}/${file_name}" ".vim/${x}/${dir_name}/${file_name}" > /dev/null
    done
  done
  _echo ...done
}

_vim_packs() {
  case "${1}" in
    install) _echon "Symlinking vim packs...";;
    uninstall) _echon "Removing vim packs...";;
  esac

  for i in "${basedir}/vendor/vim-packs"/*; do
    pack_name="`basename "/${i}"`"
    _echon "[${pack_name}]"
    _rc "${1}" "../vendor/vim-packs/${pack_name}" ".vim/pack/default/opt/${pack_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}" syntax
    _vim_packs "${1}"
    _rc "${1}" "vimrc" ".vimrc"
    _rc "${1}" "irbrc" ".irbrc"
  ;;
  *)
    _help
  ;;
esac