Reworked original crummy disconnect script
[dotfiles-framework.git] / disconnect.sh
1 #!/bin/bash
2 #
3 # You're about to give your sysadmin account to some newbie, and
4 # they'd just be confused by all this efficiency.  This script freezes
5 # your dotfiles in their current state and makes everthing look
6 # normal.  Note that this will delete your ~/.dotfiles directory, and
7 # strip the .dotfiles portion from your ~/.bashrc file.
8
9 # See if the bashrc file is involved with .dotfiles at all
10 if [ -e _bashrc ]; then
11     BASHRC="yes"
12 else
13     BASHRC="no"
14 fi
15
16 # For each file in this directory.
17 for file in _*; do
18     # Create .dotfile version.
19     dotfile=.${file/_/}
20     
21     # Replace symlinks with their target
22     if [ -h ~/$dotfile ]; then
23         echo -n ""
24         #rm -f ~/$dotfile
25         #mv $file ~/$dotfile
26     fi
27 done
28
29 if [ $BASH_RC == "yes" ]; then
30     # We may have a dotfiles section in ~/.bashrc.  Strip it out.
31     BC="### ---- begin .dotfiles section ---- (keep this magic comment)"
32     EC="### ---- end .dotfiles section ---- (keep this magic comment)"
33     AWKSCRIPT="BEGIN{copy=1}{"
34     AWKSCRIPT="$AWKSCRIPT if(\$0 == \"$BC\"){copy=0};"
35     AWKSCRIPT="$AWKSCRIPT if(\$0 == \"$EC\"){copy=1};"
36     AWKSCRIPT="$AWKSCRIPT if(copy==1 && \$0 != \"$EC\"){print \$0}"
37     AWKSCRIPT="$AWKSCRIPT}"
38     
39     awk "$AWKSCRIPT" ~/.bashrc > bashrc_stripped
40     
41     # see if the stripped file is any different
42     DIFF=`diff ~/.bashrc bashrc_stripped` || exit 1
43     if [ -n "$DIFF" ]; then
44         rm -f ~/.bashrc
45         cp bashrc_stripped ~/.bashrc
46     fi
47 fi