Advertisement
justin_hanekom

setup-links-to-dropbox.sh

Mar 30th, 2019
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.95 KB | None | 0 0
  1. #!/bin/bash -
  2.  
  3. # File: setup-links-to-dropbox.sh
  4. # Copyright (c) 2018-2019 Justin Hanekom <justin_hanekom@yahoo.com>
  5. # Licensed under the MIT License
  6.  
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files
  9. # (the "Software"), to deal in the Software without restriction,
  10. # including without limitation the rights to use, copy, modify, merge,
  11. # publish, distribute, sublicense, and/or sell copies of the Software,
  12. # and to permit persons to whom the Software is furnished to do so,
  13. # subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be
  16. # included in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26. # Setup a safe Bash scripting environment
  27.  
  28. set -o errexit      # Exit immediately if an error occurs
  29. set -o noclobber    # Do not allow files to be overwritten via redirect
  30. set -o nounset      # Do not allow unset variables
  31.  
  32. # Set the exit code of a pipeline to the rightmost non-zero on error
  33. set -o pipefail
  34. #set -o xtrace       # Trace script execution (i.e., debug mode)
  35. # Set the internal field separator to newline or tab, but not space
  36. IFS=$'\n\t'
  37.  
  38. # Setup a secure Bash scripting environment by: setting a secure path;
  39. # clearing all aliases; clearing the command path hash; setting the hard limit
  40. # to 0 to turn off core dumps; and setting a secure umask
  41.  
  42. PATH=$(PATH='/bin:/usr/bin' getconf PATH); export PATH
  43. builtin unalias -a
  44. hash -r
  45. ulimit -H -c 0 --
  46.  
  47. UMASK=002
  48. umask ${UMASK}
  49.  
  50. # Constant definitions
  51.  
  52. readonly COMMON_LINK_DIRS=( 'Documents' 'Music' 'Pictures' 'src' 'Videos' )
  53.  
  54. readonly LINUX_LINK_DIRS=( 'bin' 'Downloads' '.gnupg' '.m2' '.pki' 'sbin' \
  55.     '.ssh' '.vim' 'VirtualBox VMs' )
  56.  
  57. readonly ALL_LINK_DIRS=( "${COMMON_LINK_DIRS[@]}" "${LINUX_LINK_DIRS[@]}" )
  58.  
  59. readonly LINUX_LINK_FILES=( '.bash_justin' '.gitconfig' '.gitignore' \
  60.     '.hgignore' '.hgrc' '.perltidyrc' 'hosts' '.tmux.conf' '.vimrc' )
  61. readonly STARTED_AT=$(date '+%s')
  62.  
  63. ################################################################################
  64. # Function:     chomp_slash
  65. # Description:  Removes any trailing slash ("/") from a string
  66. # Arguments:    $1 :- String from which to remove any trailing slashes
  67. # Ouputs:       Prints string with trailing slashes removed
  68. function chomp_slash {
  69.     local dir="$1"
  70.     while [ "${dir:(-1)}" = '/' ]; do
  71.         dir=${dir::-1}
  72.     done
  73.     echo "$dir"
  74. }
  75.  
  76. ################################################################################
  77. # Function:     trim
  78. # Description:  Trims any/all whitespace from the beginning and end of a string
  79. # Arguments:    $1 :- The string from which to trim whitespace
  80. # Outputs:      Prints string with any leading/trailing whitespace removed
  81. function trim {
  82.     local str="$1"
  83.     str="${str#"${str%%[![:space:]]*}"}"
  84.    str="${str%"${str##*[![:space:]]}"}"
  85.     echo "$1"
  86. }
  87.  
  88. # Parse the command-line options
  89.  
  90. readonly OPTIONS='s:d:rv'
  91. readonly LONGOPTS='srcdir:,destdir:,test-run,remove,verbose'
  92.  
  93. if ! readonly PARSED_OPTIONS=$(getopt --options=${OPTIONS} \
  94.                                 --longoptions=${LONGOPTS} \
  95.                                 --name "$(basename "$0")" -- "$@"); then
  96.     echo 'Unknown error parsing getopt options. Exiting.' >&2
  97.     exit 2
  98. fi
  99.  
  100. eval set -- "${PARSED_OPTIONS}"
  101.  
  102. # Extract command-line options and their arguments, if applicable
  103.  
  104. destdir=$(chomp_slash "${HOME}")
  105. srcdir=$(chomp_slash "${destdir}/Dropbox/Justin")
  106. is_remove=''
  107. is_verbose=''
  108.  
  109. while (( $# >= 1 )); do
  110.     case "$1" in
  111.         -s|--srcdir)
  112.             srcdir=$(chomp_slash "$(trim "$2")") ; shift 2
  113.             ;;
  114.         -d|--destdir)
  115.             destdir=$(chomp_slash "$(trim "$2")") ; shift 2
  116.             ;;
  117.         -r|--remove)
  118.             is_remove='true' ; shift
  119.             ;;
  120.         -v|--verbose)
  121.             is_verbose='true' ; shift
  122.             ;;
  123.         --)
  124.             shift ; break
  125.             ;;
  126.         *)
  127.             echo "Unrecognized option: <$1>. Exiting" >&2 ; exit 3
  128.             ;;
  129.     esac
  130. done
  131.  
  132. readonly COMMON_DIR="${srcdir}/Common"
  133. readonly LINUX_DIR="${srcdir}/Linux"
  134.  
  135. if [ -n "${is_verbose}" ]; then
  136.     readonly VERBOSE_FLAG='--verbose'
  137. else
  138.     readonly VERBOSE_FLAG=''
  139. fi
  140.  
  141. # Optionally remove any files or directories
  142. # that would cause this script to abort
  143.  
  144. if [ -n "${is_remove}" ]; then
  145.     for d in "${ALL_LINK_DIRS[@]}" ; do
  146.         sudo rm --force --recursive ${VERBOSE_FLAG} "${destdir}/$d"
  147.     done
  148.  
  149.     for f in "${LINUX_LINK_FILES[@]}" ; do
  150.         sudo rm --force ${VERBOSE_FLAG} "${destdir}/$f"
  151.     done
  152. fi
  153.  
  154. # Create symbolic links from each of the Dropbox common directories
  155.  
  156. for d in "${COMMON_LINK_DIRS[@]}" ; do
  157.     sudo ln --symbolic ${VERBOSE_FLAG} "${COMMON_DIR}/$d" "${destdir}/$d"
  158. done
  159.  
  160. # Create symbolic links from each of the Dropbox Linux directories
  161.  
  162. for d in "${LINUX_LINK_DIRS[@]}" ; do
  163.     sudo ln --symbolic ${VERBOSE_FLAG} "${LINUX_DIR}/$d" "${destdir}/$d"
  164. done
  165.  
  166. # Create symbolic links from each of the Dropbox Linux files
  167.  
  168. for f in "${LINUX_LINK_FILES[@]}" ; do
  169.     sudo ln --symbolic ${VERBOSE_FLAG} "${LINUX_DIR}/$f" "${destdir}/$f"
  170. done
  171.  
  172. # Report that we are done, and how long the script took
  173.  
  174. if [ -n "$IS_VERBOSE" ]; then
  175.     readonly ENDED_AT=$(date '+%s')
  176.     readonly DIFF=$(( ${ENDED_AT} - ${STARTED_AT} ))
  177.     echo "Done, in ${DIFF} seconds"!
  178. fi
  179.  
  180. # vim: set filetype=sh autoread expandtab smarttab softtabstop=4 shiftwidth=4 tabstop=4 autoindent smartindent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement