Skip to content

Instantly share code, notes, and snippets.

@almir
Last active May 23, 2016 16:31
Show Gist options
  • Save almir/e584d1518ca1bae97b47 to your computer and use it in GitHub Desktop.
Save almir/e584d1518ca1bae97b47 to your computer and use it in GitHub Desktop.
Shell script for managing dotfiles
#!/bin/bash
######################################################################################
## Script for managing dotfiles
##
## IMPORTANT: This script should be located inside the directory where you keep
## your dotfiles, otherwise it will not work.
######################################################################################
######################################################################################
## The MIT License (MIT)
##
## Copyright (c) 2015 Almir Dzinovic
##
## Permission is hereby granted, free of charge, to any person obtaining a copy of
## this software and associated documentation files (the "Software"), to deal in
## the Software without restriction, including without limitation the rights to
## use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
## the Software, and to permit persons to whom the Software is furnished to do so,
## subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
## FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
## COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
## IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
######################################################################################
# Strip dot from names
rename() {
for i in ${SCRIPTPATH}/.*
do
(! [[ "${i}" =~ \.git$ ]] && ! [[ "${i}" =~ \.$ ]]) || continue
mv -f "${i}" "${i/./}"
done
}
# Make backup of the existing dotfiles in $HOME
backup() {
if [[ $FLAG == "--with-backup" ]]; then
[[ -e ~/."${i}" ]] && cp -rf ~/."${i}" ~/."${i}-$(date +%Y%m%d%H%M%S)"
else
cd "${SCRIPTPATH}" || exit
for i in *
do
([[ "${i}" != "manage-dotfiles.sh" ]] && [[ "${i}" != "README.md" ]]) || continue
[[ -e ~/."${i}" ]] && cp -rf ~/."${i}" ~/."${i}-$(date +%Y%m%d%H%M%S)"
done
fi
}
# Remove existing dot directories from $HOME
delete() {
[[ -d ~/."${i}" ]] && rm -rf ~/."${i}"
}
# Copy dotfiles to $HOME
copy() {
cd "${SCRIPTPATH}" || exit
for i in *
do
([[ "${i}" != "manage-dotfiles.sh" ]] && [[ "${i}" != "README.md" ]]) || continue
if [[ $FLAG == "--with-backup" ]]; then
backup && delete
else
delete
fi
cp -rf "${i}" ~/."${i}"
done
}
# Make symlinks to dotfiles in $HOME
link() {
cd "${SCRIPTPATH}" || exit
for i in *
do
([[ "${i}" != "manage-dotfiles.sh" ]] && [[ "${i}" != "README.md" ]]) || continue
if [[ $FLAG == "--with-backup" ]]; then
backup && delete
else
delete
fi
ln -sf "${SCRIPTPATH}/${i}" ~/."${i}"
done
}
# Show help
argmissing() {
echo "Usage: ${0} OPTION [FLAG]"
echo -e "eg.: ${0} link --with-backup
\nAvailable options:
\trename\t\t\tStrip the prepended dot from file names.
\tcopy\t\t\tCompare the script's directory and \$HOME and remove any duplicates from \$HOME, then copy the new dotfiles to \$HOME.
\t\t--with-backup\tCompare the script's directory and \$HOME and make backup of any duplicates from \$HOME, then copy the new dotfiles to \$HOME.
\tlink\t\t\tCompare the script's directory and \$HOME and remove any duplicate directories from \$HOME, then create symbolic
\t\t\t\tlinks in \$HOME pointing to dotfiles and directories located within the script's directory.
\t\t--with-backup\tCompare the script's directory and \$HOME and make backup of any duplicates from \$HOME, then create symbolic
\t\t\t\tlinks in \$HOME pointing to dotfiles and directories located within the script's directory.
\tbackup\t\t\tCompare the script's directory and \$HOME and only make backup of any duplicates from \$HOME."
exit 1
}
# Check whether there is an option specified and show help if it isn't
[[ -z "${1}" ]] && argmissing
# Set variables
## Set path to this script
SCRIPTPATH=$(readlink -f "$(dirname "$(readlink -f "${0}")")")
## Save FLAG variable
[[ -n "${2}" ]] && FLAG="${2}"
# Execute function according to the argument passed
"${1}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment