-
Notifications
You must be signed in to change notification settings - Fork 49
/
force_update_emacsd.sh
executable file
·82 lines (70 loc) · 2.64 KB
/
force_update_emacsd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
# Time-stamp: <2016-11-03 13:16:50 kmodi>
# Usage : ./force_update_emacsd.sh <YOUR-EMACSD-PATH>
# Example: ./force_update_emacsd.sh ~/.emacs.d
# <YOUR-EMACSD-PATH>, if already existing, will be copied to the below backup dir
emacs_autobkp_dir="~/.emacs.d.bkpauto"
if [[ "$0" == "sh" ]] # called by source (source foo.sh)
then
# http://stackoverflow.com/a/8912075/1219634
script_file_name=$(readlink -f "${BASH_SOURCE[0]}")
else # called by direct execution of the script (./foo.sh)
script_file_name=$(readlink -f "$0")
fi
script_dir=$(dirname "${script_file_name}")
# echo "Script file name: ${script_file_name}"
# echo "Script dir: ${script_dir}"
if [[ -z "$1" ]] # If the first argument is missing or an empty string
then
echo "ERROR: Missing a mandatory argument."
echo " source $0 <YOUR-EMACSD-PATH>"
echo " Example: source $0 ~/.emacs.d"
exit 1
else
emacs_config_dir="$1"
fi
echo "The emacs config will be saved to ${emacs_config_dir}"
echo "Press Ctrl-C within 10 seconds if you don't want that .."
sleep 10
echo ''
start_dir=$(pwd)
if [[ ! -d "${emacs_config_dir}" ]]
then
echo "Cloning .emacs.d to ${emacs_config_dir} .."
git clone http://www.github.com/kaushalmodi/.emacs.d ${emacs_config_dir}
else
cd ${emacs_config_dir}
git_remote_origin_url=$(git config --get remote.origin.url)
git_remote_origin_url_check=$(echo "${git_remote_origin_url}" | \sed 's/.*\(github\.com.*\.emacs\.d\).*/\1/')
# Ensure that the we are not overriding a different git managed dir
if [[ ${git_remote_origin_url_check} == "github.com/kaushalmodi/.emacs.d" ]]
then
if [[ -d "${emacs_autobkp_dir}" ]]
then
\rm -rf ${emacs_autobkp_dir} # Delete old backup if present
fi
echo "Backing up existing ${emacs_config_dir} to ${emacs_autobkp_dir} .."
\cp -rf ${emacs_config_dir} ${emacs_autobkp_dir}
echo "Force updating .emacs.d to ${emacs_config_dir} .."
# Below will overwrite ALL GIT-MANAGED files
# Files created locally/not git managed will remain untouched
git fetch --all
git reset --hard origin/master
else
echo "ERROR: The remote URL for this git repo is '${git_remote_origin_url}'."
echo "Please try again using a different directory argument for downloading this .emacs.d."
exit 1
fi
fi
echo ''
cd ${emacs_config_dir}
git submodule init
git submodule update --force
cd ${start_dir}
# Self-destruct if the script is present in a path beginning with "/tmp"
if [[ ${script_dir} == /tmp* ]]
then
# echo "Self-deleting ${script_file_name} ..\n"
\rm -f ${script_file_name}
fi
echo "Done!!"