/home/alex/dev/get-profile.sh (1)

From RaySoft
#!/bin/bash -
# ------------------------------------------------------------------------------
# get-profile.sh
# ==============
#
# Scope     macOS
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------

set -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'errexit' -o 'xtrace'

# ------------------------------------------------------------------------------
# Configuration
# -------------

GP_FILES=(
  # Bash configuration
  '.bashrc'   '.profile'

  # (Neo)vim configuration
  '.vimrc'    '.vimrc'
)

# Sublime Text configuration directories
GP_SRC_SL_DIR='.config/sublime-text/Packages/User'
GP_DST_SL_DIR='Library/Application Support/Sublime Text/Packages/User'

# Sublime Text configuration
for name in 'Preferences' 'Package Control' 'Bash' 'JavaScript' 'Markdown' \
            'PlainTasks'
do
  GP_FILES+=({"${GP_SRC_SL_DIR}","${GP_DST_SL_DIR}"}"/${name}.sublime-settings")
done

# Development directories
GP_SRC_DEV_DIR='dev'
GP_DST_DEV_DIR='Documents/dev'

# Bash libraries
for name in 'nf' 'os'; do
  GP_FILES+=({"${GP_SRC_DEV_DIR}","${GP_DST_DEV_DIR}"}"/lib/${name}.sh")
done

# Bash scripts
for name in 'deploy-apps-libs' 'deploy-macos' 'maintain-macos'; do
  GP_FILES+=({"${GP_SRC_DEV_DIR}","${GP_DST_DEV_DIR}"}"/${name}.sh")
done

# OSA scripts
for name in 'align-projects.jxa' 'finder-grid.applescript'; do
  GP_FILES+=({"${GP_SRC_DEV_DIR}","${GP_DST_DEV_DIR}"}"/${name}")
done

# ------------------------------------------------------------------------------

# Print headings
echo
sed 'p; s/./=/g' <<<'Get my profile from RaySoft & install it'
echo
sed 'p; s/./-/g' <<<'Process files'

# For all files defined in the array 'GP_FILES'
for ((i=0; i<${#GP_FILES[@]}; i=i+2)); do
  src="${GP_FILES[$i]// /_}"
  dst="${GP_FILES[(($i+1))]}"

  src_url="https://www.raysoft.ch/wiki//home/alex/${src}_(1)?action=raw"
  dst_file="${HOME}/${dst}"

  echo "- ~/${dst}"

  # Create the destination directory if it doesn't exist
  if [[ ! -d "${dst_file%/*}" ]]; then
    mkdir -p "${dst_file%/*}"
  fi

  # Download the file from 'RaySoft'
  if ! curl --fail --location --silent "${src_url}" \
       | sed '1,/<syntaxhighlight/d; /<\/syntaxhighlight/,$d' >"${dst_file}"
  then
    echo "Error downloading file: ~/${dst}!"
    exit 1
  fi

  # For 'Bash' files
  if [[ "${dst_file}" =~ \.sh$ ]]; then
    # Rewrite development path
    if [[ ! "${dst_file}" =~ get-profile\.sh$ ]]; then
      sed -i "s|\${HOME}/${GP_SRC_DEV_DIR}|\${HOME}/${GP_DST_DEV_DIR}|g" \
          "${dst_file}"
    fi

    # Rewrite hash bang path
    if [[ ! "${dst_file}" =~ (get-profile|deploy-apps-libs)\.sh$ ]]; then
      sed -i '1s|#!/bin/bash|#!/usr/local/bin/bash|' "${dst_file}"
    fi

    # Link to '$HOME/bin' if the link doesn't exist
    for dst_name in 'maintain-macos'; do
      dst_link="${HOME}/bin/${dst_name}"

      if [[ "${dst_file}" =~ ${dst_name}\.sh$ && ! -L "${dst_link}" ]]; then
        ln -s "${dst_file}" "${dst_link}"
      fi
    done

    # Set file permissions
    if [[ "${dst_file}" =~ /lib/ ]]; then
      chmod 0600 "${dst_file}"
    else
      chmod 0700 "${dst_file}"
    fi

  # For OSA scripts
  elif [[ "${dst_file}" =~ \.(applescript|jxa)$ ]]; then
    # Rewrite development path
    sed -i "s|/Users/alex/${GP_SRC_DEV_DIR}|${HOME}/${GP_DST_DEV_DIR}|g" \
      "${dst_file}"

    # Compile script
    if [[ "${dst_file}" =~ finder-grid\.[^.]+$ ]]; then
      dst_scpt="$(sed 's/\.[^.]\+$/.scpt/' <<<"${dst_file}")"

      osacompile -x -o "${dst_scpt}" "${dst_file}"
    fi

    # Link to '$HOME/bin' if the link doesn't exist
    for dst_name in 'align-projects' 'finder-grid'; do
      dst_link="${HOME}/bin/${dst_name}"

      if [[ "${dst_file}" =~ ${dst_name}\.[^.]+$ && ! -L "${dst_link}" ]]; then
        ln -s "${dst_file}" "${dst_link}"
      fi
    done

    # Set file permissions
    chmod 0700 "${dst_file}"

  # For all other files
  else
    # Set file permissions
    chmod 0600 "${dst_file}"
  fi

  # Remove the attribute 'com.apple.quarantine'
  if xattr "${dst_file}" | grep 'com.apple.quarantine' >'/dev/null' 2>&1; then
    xattr -d 'com.apple.quarantine' "${dst_file}"
  fi
done

# Print heading
echo
sed 'p; s/./-/g' <<<"Configure '(Neo)vim'"

# Get the 'vi' implementation ('Neovim' or 'vim')
for item in 'nvim' 'vim'; do
  if editor="$(type -P "${item}" 2>'/dev/null')"; then
    break
  fi
done

# Configure '(Neo)vim'
if [[ -n "${editor}" ]]; then
  # Download the plugin 'vim-plug' if it doesn't exist
  if [[ ! -f "${HOME}/.vim/autoload/plug.vim" ]]; then
    curl --create-dirs --fail --location --silent \
      --output "${HOME}/.vim/autoload/plug.vim" \
      'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
  fi

  # Configure 'Neovim' if it is installed
  if [[ "${editor##*/}" == 'nvim' ]]; then
    # Create the directory '${HOME}/.config/nvim' if it doesn't exist
    if [[ ! -d "${HOME}/.config/nvim" ]]; then
      mkdir -p "${HOME}/.config/nvim"
    fi

    # Link the 'vim' configuration file if the link doesn't exist
    if [[ ! -L "${HOME}/.config/nvim/init.vim" ]]; then
      ln -s "${HOME}/.vimrc" "${HOME}/.config/nvim/init.vim"
    fi

    # Link the 'vim' configuration directories if the links don't exist
    for item in 'autoload' 'plugged'; do
      if [[ ! -L "${HOME}/.config/nvim/${item}" ]]; then
        ln -s "${HOME}/.vim/${item}" "${HOME}/.config/nvim/${item}"
      fi
    done
  fi

  # Install resp. maintain the plugins using 'vim-plug'
  "${editor}" -c 'PlugUpgrade | PlugUpdate | PlugClean | quitall'
fi

# ------------------------------------------------------------------------------

exit 0

Usage

~/dev/get-profile.sh