aboutsummaryrefslogtreecommitdiff
path: root/metadot
blob: de18148f70dd48ff7e8bc4e2fae7adb081ac99f4 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/bash
#
# Metadot: a dotfile management system.
#
# Copyright (C) 2013 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Set real home folder
if [ ! -z "$PREFIX" ]; then
  if [ ! -d "$PREFIX" ]; then
    echo "Destination folder not found: $PREFIX"
  else
    DEST="$PREFIX"
  fi
else
  DEST="$HOME"
fi

# Parameters
OPT="$1"
DATE="`date +%Y%m%d%I%M%S`"
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
DOT="$DEST/.dotfiles"
MODULES="$DOT/modules"
BACKUPS="$DEST/.backups/$DATE"
DEFAULT="https://git.fluxo.info/rhatto/dotfiles"

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

# Backup a file
function metadot_backup {
  local file="$DEST/$1"

  if [ -e "$file" ] || [ -h "$file" ]; then
    local folder="$BACKUPS/`dirname $1`"

    #echo "Backing up `basename $1`..."
    mkdir -p $folder
    mv $file $folder
  fi
}

# Find contents of a module
function __metadot_find {
  local module="$1"
  ( cd $MODULES/$module && find . -name '*.link' -or -name '*.dot.link' ) | grep -v '.git/modules' | sed -e 's|./||'
}

# Load a module
function metadot_load {
  local module="$1"
  local destname
  local dirname

  if [ -d "$MODULES/$module" ]; then

    echo "Loading module $module..."

    for file in `__metadot_find $module`; do
      echo "Processing $file..."

      # Get the dirname, replacing string.dot with .string
      dirname="`echo $file | sed -e 's|\([^/]*\).dot/|.\1/|g'`"
      dirname="`dirname $dirname`"

      if echo $file | grep -q '.dot.link'; then
        destname=".`basename $file .dot.link`"
      else
        destname="`basename $file .link`"
      fi

      if [ "$dirname" != "." ]; then
        #echo "Creating $DEST/$dirname..."
        mkdir -p $DEST/$dirname
      else
        dirname=""
      fi

      metadot_backup "$dirname/$destname"

      #echo "Installing symlink $dirname/$destname..."
      ln -s $MODULES/$module/$file $DEST/$dirname/$destname

    done
  else
    echo "No such module $module"
  fi
}

# Load a module
function metadot_deps {
  local module="$1"
  local destname
  local dirname
  local deps_to_install

  if [ -d "$MODULES/$module" ]; then
    echo "Resolving dependencies for $module.."

    if [ -e "/etc/debian_version" ] && [ -e "$MODULES/$module/dependencies/debian" ]; then
      deps="`grep -v '^#' $MODULES/$module/dependencies/debian | xargs`"

      # Remove installed packages from list
      for dep in $deps; do
        #if ! dpkg-query -W -f='${Status}' $dep | grep -q '^install ok'; then
        if ! dpkg -l $dep 2> /dev/null | grep -q "^ii"; then
          deps_to_install="$deps_to_install $dep"
        fi
      done

      if [ ! -z "$deps_to_install" ]; then
        LC_ALL=C DEBIAN_FRONTEND=noninteractive sudo apt-get install -y $deps_to_install
      fi
    fi

  else
    echo "No such module $module"
  fi
}

# Create a new module
function metadot_create {
  mkdir -p $MODULES/$1
  (
    cd $MODULES/$1
    git init

    echo "# $1 dotfile module"                                 > README.md
    echo ""                                                   >> README.md
    echo "This is the repository for $1 configuration."       >> README.md
    echo "More information at https://git.fluxo.info/metadot" >> README.md

    #echo "TODO"                > TODO.md
    #echo "===="               >> TODO.md
    #echo ""                   >> TODO.md
    #echo "* Nothing here? :P" >> TODO.md

    echo "# As we are handling with config files, it might be better to"  > .gitignore
    echo "# use a paranoid config by default."                           >> .gitignore
    echo "#"                                                             >> .gitignore
    echo "# Comment that while in development."                          >> .gitignore
    echo '*'                                                             >> .gitignore

    cp $DIRNAME/LICENSE .

    git add -f .
  )
  echo "Metadot skeleton module $1 created at $MODULES/$1"
}

# Fetch dotfiles
function metadot_fetch {
  if [ -d "$DOT/.git" ]; then
    ( cd $DOT && $GIT fetch --all && $GIT log --show-signature -n 1 --remotes --branches=origin/master )
  fi
}

# Merge
function metadot_merge {
  if [ -d "$DOT/.git" ]; then
    (
    cd $DOT && $GIT merge origin/master                 && \
               $GIT submodule sync          --recursive && \
               $GIT submodule update --init --recursive #--recommend-shallow
    )
  fi
}

# Update your dotfiles
function metadot_update {
  echo "please run 'metadot fetch && metadot merge' instead"
  exit 1
  #if [ -d "$DOT/.git" ]; then
  #  ( cd $DOT && git pull origin master && git submodule update --init --recursive )
  #else
  #  for module in `ls $MODULES`; do
  #    ( cd $MODULES/$module && git pull origin master && git submodule update --init --recursive )
  #  done
  #fi
}

# Basic usage
function metadot_usage {
  echo "usage: $BASENAME <option> [arguments]"
  echo ""
  echo "available actions:"
  echo ""
  grep "^function metadot_" $0 | cut -d ' ' -f 2 | sed -e 's/metadot_/\t/' -e 's/_/-/g' | sort
  echo ""
  exit 1
}

# List modules
function metadot_ls {
  ls -1 $MODULES
}

# Clone a module
function metadot_clone {
  shift
  if [ "$1" == "default" ]; then
    metadot_backup $DOT
    git clone --recursive $DEFAULT $DOT
    echo "Backups saved at $BACKUPS."
  elif [ "`basename $1 .git`" == "dotfiles" ]; then
    metadot_backup $DOT
    git clone --recursive $1 $DOT
    echo "Backups saved at $BACKUPS."
  else
    mkdir -p $MODULES
    git clone --recursive $1 $MODULES/`basename $1 .git`
  fi
}

# List installed modules
function metadot_installed {
  # Complete, slow version
  #find $HOME -lname '*.dotfiles*' -exec ls -la {} | \
  #grep .dotfiles/modules | sed -e 's|.*.dotfiles/||g' | cut -d '/' -f 2 | sort | uniq

  # Simple, incomplete version
  ls -ag $HOME/ | grep .dotfiles/modules | sed -e 's|.*.dotfiles/||g' | cut -d '/' -f 2 | sort | uniq
}

# Process modules
function __metadot_process_modules {
  if [ -z "$1" ]; then
    echo "usage: $BASENAME $OPT [module(s)|--all]"
  fi

  if [ "$1" == "--all" ]; then
    modules="`ls $MODULES`"
  else
    modules="$*"
  fi

  for module in $modules; do
    metadot_$OPT $module
  done

  if [ "$OPT" == "load" ]; then
    echo "Backups saved at $BACKUPS."
  fi
}

# Process bundle
function __metadot_process_bundle {
  local bundle="$1"
  local dependency
  local option

  if [ -z "$bundle" ]; then
    echo "usage: $BASENAME $OPT [bundle]"
    exit 1
  fi

  if [ ! -e "$DOT/bundles/$bundle" ]; then
    echo "$BASENAME: bundle not found: $bundle"
    exit 1
  fi

  cat $DOT/bundles/$bundle | while read item; do
    if echo $item | grep -q "^bundles/"; then
      option="$OPT"
      dependency="`echo $item | sed -e 's|bundles/||g'`"
    elif echo $item | grep -q "^modules/"; then
      option="`echo $OPT | sed -e 's|-bundle||g'`"
      dependency="`echo $item | sed -e 's|modules/||g'`"
    else
      echo "Skipping invalid item $item..."
      continue
    fi

    $DIRNAME/$BASENAME $option $dependency
  done
}

# Display bundle contents
function metadot_ls_bundle {
  local bundle="$1"

  if [ -z "$bundle" ] && [ -d "$DOT/bundles" ]; then
    ls $DOT/bundles
    exit
  fi

  if [ ! -e "$DOT/bundles/$bundle" ]; then
    echo "$BASENAME: bundle not found: $bundle"
    exit 1
  fi

  cat $DOT/bundles/$bundle
}

# Display version
function metadot_version {
  echo "master branch:"
  echo "=============="
  echo ""
  ( cd $DOT && $GIT log --show-signature -n 1 )

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

# Load a bundle
function metadot_load_bundle {
  __metadot_process_bundle $*
}

# Process bundle dependencies
function metadot_deps_bundle {
  __metadot_process_bundle $*
}

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

# Parsing
if [ -z "$OPT" ]; then
  metadot_usage
elif [ "$OPT" == "usage" ]; then
  metadot_usage
elif [ "$OPT" == "ls" ]; then
  metadot_ls
elif [ "$OPT" == "ls-bundle" ]; then
  shift
  metadot_ls_bundle $*
elif [ "$OPT" == "version" ]; then
  metadot_version
elif [ "$OPT" == "update" ]; then
  metadot_update
elif [ "$OPT" == "backup" ]; then
  shift
  metadot_backup $*
elif [ "$OPT" == "clone" ]; then
  metadot_clone $*
elif [ "$OPT" == "create" ]; then
  shift
  metadot_create $*
elif [ "$OPT" == "load" ] || [ "$OPT" == "deps" ]; then
  shift
  __metadot_process_modules $*
elif [ "$OPT" == "load-bundle" ]; then
  shift
  metadot_load_bundle $*
elif [ "$OPT" == "deps-bundle" ]; then
  shift
  metadot_deps_bundle $*
elif [ "$OPT" == "installed" ]; then
  metadot_installed
elif [ "$OPT" == "fetch" ]; then
  metadot_fetch
elif [ "$OPT" == "merge" ]; then
  metadot_merge
elif [ "$OPT" == "status" ]; then
  metadot_status
else
  metadot_usage
fi