diff 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 diff
--- a/setup	Wed Oct 26 16:54:25 2011 +0700
+++ b/setup	Thu Oct 27 16:23:23 2011 +0700
@@ -1,34 +1,97 @@
 #!/bin/sh
 
-install() {
-  uninstall
-  #create symlinks
-  linker "tcsh" ".ecos_tcsh"
-  linker "bash" ".ecos_bash"
-  linker "zsh" ".ecos_zsh"
-  linker "tmux" ".tmux.conf"
-  linker "hg" ".hgrc"
-  linker "vim" ".vimrc"
-  linker "inputrc" ".inputrc"
-  #originally bin-ec. Renamed to .ecos.bin for sanity when listing home
-  #and replaced . with _ for more sanity
-  linker "bin" ".ecos_bin"
+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
+}
 
-  #modify cshrc
-  echo '[ -r "${HOME}/.ecos_tcsh" ] && source "${HOME}/.ecos_tcsh" #ECCORE_TCSH' >> "${HOME}/.cshrc"
-  #modify bash_profile and bashrc
-  echo '[ -r "${HOME}/.ecos_bash" ] && . "${HOME}/.ecos_bash" #ECCORE_BASH' >> "${HOME}/.bash_profile"
-  echo '[ -r "${HOME}/.ecos_bash" ] && . "${HOME}/.ecos_bash" #ECCORE_BASH' >> "${HOME}/.bashrc"
-  #modify zshrc
-  echo '[ -r "${HOME}/.ecos_zsh" ] && . "${HOME}/.ecos_zsh" #ECCORE_ZSH' >> "${HOME}/.zshrc"
+_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() {
+_uninstall_init() {
   script="${1}"
   scripttmp="${1}.new.tmp"
   hashdata="${2}"
@@ -38,43 +101,24 @@
   fi
 }
 
-uninstall() {
-  for i in .ecos_tcsh .tmux.conf .hgrc .vimrc .inputrc bin-ec .ecos.bin .ecos_bin .ecos_bash .ecos_zsh; do
-    if [ -e "${HOME}/${i}" ]; then
-      echo "Removing file: ~/${i}"
-      rm -f "${HOME}/${i}"
-    fi
-  done
-  uninstall_init "${HOME}/.cshrc" "#ECCORE_TCSH"
-  uninstall_init "${HOME}/.bashrc" "#ECCORE_BASH"
-  uninstall_init "${HOME}/.bash_profile" "#ECCORE_BASH"
-  uninstall_init "${HOME}/.zshrc" "#ECCORE_ZSH"
-}
-
-update() {
-  if [ -f "${HOME}/.tcsh_exec" ]; then
-    mv -f "${HOME}/.tcsh_exec" "${HOME}/.ecos_tcsh.after"
-    echo "Moved: ${HOME}/.tcsh_exec => ${HOME}/.ecos_tcsh.after"
-  fi
-}
-
-help() {
+_help() {
   cat <<EOF
 Usage: ${0} [install|uninstall]
 EOF
 }
 
-case "$1" in
-  install)
-    install
-  ;;
-  uninstall)
-    uninstall
-  ;;
-  update)
-    update
+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
+    _help
   ;;
 esac