aboutsummaryrefslogtreecommitdiff
path: root/infection
blob: 353128cfcac54a5d84fe32a1ed228196b19b6016 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
#
# The infection.
#

# Parameters
BASENAME="`basename $0`"
EXCLUDES="--exclude=local --exclude=stowpkg/tree"
DATE="`date +%Y%m%d%I%M%S`"
BACKUPS="$HOME/.backups/$DATE"

# Make sure we're running git directly and not any existing wrapper
GIT="/usr/bin/git"

# Usage
function infection_usage {
  echo "usage: $BASENAME <action> [options]"
  exit 1
}

# Initialize
function infection_init {
  # Set dir and base names depending on the way the program was called (symlink or direct)
  if [ "$(cd `dirname $0` &> /dev/null && basename `pwd`)" == "inception" ]; then
    DIRNAME="$(cd `dirname $0/..` &> /dev/null && pwd)"
  else
    DIRNAME="$(cd `dirname $0` &> /dev/null && pwd)"
  fi

  SHORTNAME="$(echo $DIRNAME | sed -e "s|$HOME/||")"

  (
    # Check if we were called correctly. Program should be called
    # from the toplevel repository symlink and not from it's own
    # repository
    #if [ ! -d "$DIRNAME/inception" ]; then
    #  echo "Please call me using $DIRNAME/infection"
    #  exit 1
    #fi

    # Check if repo is inside $HOME
    if [ "$DIRNAME" != "$HOME/$SHORTNAME" ]; then
      echo "Please make sure $DIRNAME is moved to $HOME"
      exit 1
    fi

    cd $DIRNAME &> /dev/null

    echo "Initializing submodules..."
    $GIT submodule sync          --recursive
    $GIT submodule update --init --recursive

    # Convert to new dotfiles layout
    #
    # This way we can easily check the integrity of our dotfiles
    # once the integrity of the repo is checked
    if [ -e "$DIRNAME/dotfiles" ]; then
      if [ -e "$HOME/.dotfiles" ]; then
        if [ ! -h "$HOME/.dotfiles" ] || [ "`readlink $HOME/.dotfiles`" != "$SHORTNAME/dotfiles" ]; then
          mkdir -p $BACKUPS
          mv $HOME/.dotfiles $BACKUPS/
          ( cd $HOME &> /dev/null && ln -s $SHORTNAME/dotfiles .dotfiles )
        fi
      else
        ( cd $HOME &> /dev/null && ln -s $SHORTNAME/dotfiles .dotfiles )
      fi
    fi

    #if [ ! -e "$HOME/.dotfiles" ]; then
    #  echo "Cloning default dotfiles..."
    #  ./metadot/metadot clone default
    #else
    #  echo "Updating dotfiles..."
    #  ./metadot/metadot fetch
    #fi

    #echo "Checking latest tag..."
    #cd $HOME/.dotfiles
    #$DIRNAME/utils-git/git-check-tag

    #echo "Checking out the latest tag..."
    #$DIRNAME/utils-git/git-checkout-tag

    #echo "Checking dotfiles..."
    #$DIRNAME/metadot/metadot version
  )

  #echo ""
  #echo "Please manually verify dotfiles version and tag from the above output."
  #echo "If everything is fine, proceed running this command again with the 'load' parameter."

  #cd -
}

# Load
function infection_load {
  (
    cd $DIRNAME &> /dev/null

    BUNDLE="$3"
    DEPENDENCIES="$4"

    if [ "$BUNDLE" == "--all" ]; then
      echo "Loading all dotfiles..."
      ./metadot/metadot load --all

      if [ "$DEPENDENCIES" == "--deps" ]; then
        ./metadot/metadot deps --all
      fi
    elif [ ! -z "$BUNDLE" ]; then
      echo "Loading $BUNDLE dotfiles..."
      ./metadot/metadot load-bundle $BUNDLE

      if [ "$DEPENDENCIES" == "--deps" ]; then
        ./metadot/metadot deps-bundle $BUNDLE
      fi
    fi
  )

  echo "Done. Logout and login again to apply all changes."
}

# Install
function infection_install {
  #$0 init
  $0 load $*
}

# Deploy
function infection_deploy {
  if [ ! -z "$2" ]; then
    REMOTES="$*"

    for REMOTE in $REMOTES; do
      REMOTE="$2"
      MODULES="`$DIRNAME/metadot/metadot installed | xargs`"

      # Sync repo
      if [ -e "$HOME/$SHORTNAME" ]; then
        rsync -avz --delete $EXCLUDES $HOME/$SHORTNAME/ $REMOTE:$SHORTNAMR/
      fi

      # Sync dotfiles
      if [ -e "$HOME/.dotfiles" ]; then
        rsync -avz --delete $HOME/.dotfiles/ $REMOTE:.dotfiles/
      fi

      # Sync loaded modules
      ssh -T $REMOTE <<EOF
      ##### BEGIN REMOTE SCRIPT #####
      \$HOME/$SHORTNAME/infection load
      for module in $MODULES; do
        \$HOME/$SHORTNAME/metadot/metadot load \$module
      done
      ##### END REMOTE SCRIPT #######
EOF
    done
  fi
}

# Version information
function infection_version {
  echo "master branch:"
  echo "=============="
  echo ""
  ( cd $DIRNAME && $GIT log --show-signature -n 1 )

  echo ""
  echo "origin/master branch:"
  echo "====================="
  echo ""
  ( cd $DIRNAME && $GIT log --show-signature -n 1 --remotes --branches=origin/master )
}

# Fetch
function infection_fetch {
  ( cd $DIRNAME && $GIT fetch --all && $GIT log --show-signature -n 1 --remotes --branches=origin/master )
}

# Merge
function infection_merge {
  (
  cd $DIRNAME && $GIT merge origin/master                 && \
                 $GIT submodule sync          --recursive && \
                 $GIT submodule update --init --recursive
  )
}

# Repository status
function infection_status {
  ( cd $DIRNAME && git status --ignored && git submodule foreach --recursive git status --ignored )
}

# Check
if [ -z "$1" ]; then
  infection_usage
  exit 1
fi

# Initialize
infection_init

# Main
if [ "$1" == "load" ]; then
  infection_load $*
#elif [ "$1" == "init" ]; then
#  infection_init
elif [ "$1" == "install" ]; then
  infection_install $*
elif [ "$1" == "deploy" ]; then
  infection_deploy $*
elif [ "$1" == "version" ]; then
  infection_version
elif [ "$1" == "fetch" ]; then
  infection_fetch
elif [ "$1" == "merge" ]; then
  infection_merge
elif [ "$1" == "status" ]; then
  infection_status
elif [ "$1" != "init" ]; then
  infection_usage
fi