aboutsummaryrefslogtreecommitdiff
path: root/metadot
blob: 5a8f8b48cb327006cfc6f9b0d1c24eca25caddb6 (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
#!/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`"
DOT="$DEST/.dotfiles"
MODULES="$DOT/modules"
BACKUPS="$DEST/.backups/$DATE"
DEFAULT="git://git.sarava.org/rhatto/dotfiles.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
}

# Parsing.
if [ -z "$OPT" ]; then
  echo "usage: $BASENAME <option> [arguments]"
  exit 1
elif [ "$OPT" == "ls" ]; then
  ls -1 $MODULES
elif [ "$OPT" == "version" ]; then
  ( cd $DOT && git log -n 1 )
elif [ "$OPT" == "update" ]; then
  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
elif [ "$OPT" == "backup" ]; then
  shift
  metadot_backup $1
elif [ "$OPT" == "clone" ]; then
  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
elif [ "$OPT" == "create" ]; then
  shift
  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.sarava.org/metadot.git" >> 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

    git add .
  )
  echo "Metadot skeleton module $1 created at $MODULES/$1"
elif [ "$OPT" == "load" ]; then
  shift

  if [ -z "$1" ]; then
    echo "usage: $BASENAME load [module(s)|--all]"
  fi

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

  for module in $modules; do
    metadot_load $module
  done
  echo "Backups saved at $BACKUPS."
fi