Skip to content

Instantly share code, notes, and snippets.

@ameeno
Last active September 18, 2022 21:43
Show Gist options
  • Save ameeno/b7696d9015077ddad8990ba5407b7092 to your computer and use it in GitHub Desktop.
Save ameeno/b7696d9015077ddad8990ba5407b7092 to your computer and use it in GitHub Desktop.
Dotfiles Install Script
#!/usr/bin/env bash
set -o errexit -o pipefail -o noclobber -o nounset
function config {
git --git-dir="$HOME"/.dotfiles.git/ --work-tree="$HOME" "$@"
}
export -f config
read -r -p " Delete existing Git DIR? [y/N] " response </dev/tty
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
/bin/rm -rf "$HOME"/.dotfiles.git
fi
KEYNAME="$HOME/.ssh/id_rsa"
BRANCH_NAME=$USER@$(hostname)
printf "checking/creating this machines id_rsa.pub\n"
if [ -f "${KEYNAME}" ]; then
if [ -s "${KEYNAME}" ]; then
printf "existing Key:\n"
cat ~/.ssh/id_rsa.pub
else
printf "File Exists but empty, New Key:\n"
rm ~/.ssh/id_rsa && rm ~/.ssh/id_rsa.pub
ssh-keygen -t rsa -C "$BRANCH_NAME" -f "$HOME/.ssh/id_rsa" -P "" && cat ~/.ssh/id_rsa.pub
fi
else
printf "New Key:\n"
mkdir -p ~/.ssh
ssh-keygen -t rsa -C "$BRANCH_NAME" -f "$HOME/.ssh/id_rsa" -P "" && cat ~/.ssh/id_rsa.pub
fi
echo "now go to https://github.com/ameeno/dotfiles/settings/keys and add the public key as a deploy key with write support."
printf "\n#####################\n"
read -r -p " Do you wish to continue? [y/N] " response </dev/tty
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
git clone --bare [email protected]:ameeno/dotfiles.git "$HOME"/.dotfiles.git
set +o errexit
config checkout
if [ $? = 0 ]; then
echo "Checked out config."
else
echo "Backing up pre-existing dot files."
[ -d "$HOME/.dotfiles.backup" ] && tar -czvf "$HOME"/.dotfiles.backup-"$(date +"%Y%m%d-%H%M%S")".tar.gz "$HOME"/.dotfiles.backup \
&& /bin/rm -rf "$HOME"/.dotfiles.backup && mkdir -p "$HOME"/.dotfiles.backup && echo "backed up previous backup as $HOME/.dotfiles.backup-$(date +"%Y%m%d-%H%M%S").tar.gz"
for f in $(config checkout 2>&1 | egrep "\s+\." | awk {'print $1'}); do
dir_name=$(dirname "$f")
rem_home=${dir_name#"$HOME"/}
mkdir -p "$HOME"/.dotfiles-backup/"$rem_home"
cp -r "$f" "$HOME"/.dotfiles-backup/"$rem_home"/ && /bin/rm -rf "$f" && echo "backed up $f"
done
config checkout -f
echo "Checked out config."
fi
# config reset --hard origin/master
# config pull origin master
echo "pulling alternative git config"
/bin/rm "$HOME"/.dotfiles.git/config && curl -Lks https://gist.githubusercontent.com/ameeno/d6aaa011a86e4741403fb97f028f8974/raw/dotfiles-gitconfig > "$HOME"/.dotfiles.git/config && printf "new git config file is \n"
cat "$HOME"/.dotfiles.git/config
printf "\n#####################\n"
echo "Switching to new Branch: $BRANCH_NAME "
config config --local user.name "$BRANCH_NAME"
config config --local user.email "$BRANCH_NAME"
config checkout -b "$BRANCH_NAME" || config checkout "$BRANCH_NAME"
# config commit -m "new branch generated"
echo "setting new branch and pushing upstream"
mv -f "$HOME"/.dotfiles.git/config "$HOME"/.dotfiles.gitconfig && echo -e "[include]\n\tpath = ~/.dotfiles.gitconfig" > "$HOME"/.dotfiles.git/config
config commit -m "new branch generated"
config push -u origin "$BRANCH_NAME" --force
config checkout -f
config fetch origin
echo "All Done, Enjoy"
else
echo "Cancelled Dotfiles Import. Exiting now ..."
exit
fi
export -f config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment