aboutsummaryrefslogtreecommitdiff
path: root/files/sync-media-init
blob: b32e88c0e058701c343fc23c1fe5273654e38091 (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
#!/bin/bash
#
# Add git-annex remotes to repository in removable media or local cache.
#

# Parameters
BASENAME="`basename $0`"
VOLUME="$1"
TYPES="$2"
DOMAIN="`facter DOMAIN`"
HOST="`facter hostname`"
CACHES=""
VOLUMES=""
BOXES=""

# Fix identity
function sync_media_identity {
  if [ -z "`git config --local user.email`" ] || [ -z "`git config --local user.name`" ]; then
    repo="$(basename `pwd`)"
    git config user.name "${repo^} Asset Manager"
    git config user.email "$repo@assset.manager"
  fi
}

# Syntax check
if [ -z "$VOLUME" ]; then
  echo "usage: $BASENAME <volume> [<caches|volumes|boxes> [remotes]]"
  exit 1
fi

# Determine media folder
if [ "$VOLUME" == "$HOST" ] || [ "$VOLUME" == "localhost" ]; then
  MEDIA="/var/cache/media"
else
  MEDIA="/media/$VOLUME/media.$DOMAIN"
fi

# Determine remote type
shift 2
if [ "$TYPES" == 'caches' ]; then
  # Remotes are caches of local boxes
  CACHES="$*"
elif [ "$TYPES" == 'volumes' ]; then
  # Remotes are removable media
  VOLUMES="$*"
elif [ "$TYPES" == 'boxes' ]; then
  # Remotes are remote boxes
  BOXES="$*"
fi

# Process each repository
for file in `ls $MEDIA`; do
  if [ -d "$MEDIA/$file/.git" ]; then
    (
    echo Processing "$MEDIA/$file..."
    cd $MEDIA/$file
    sync_media_identity

    if git remote | grep -q "^origin$"; then
      echo "Removing origin..."
      git remote rm origin
    fi

    if [ ! -d "$MEDIA/$file/.git/annex" ]; then
      git annex init $VOLUME
    fi

    for remote in $CACHES; do
      echo "Adding /var/cache/$remote/media/$file remote..."
      git remote add $remote /var/cache/$remote/media/$file
    done

    for remote in $VOLUMES; do
      echo "Adding /media/$remote/media.$DOMAIN/$file..."
      git remote add $remote /media/$remote/media.$DOMAIN/$file
    done

    for remote in $BOXES; do
      if ! echo $remote | grep -q '\.'; then
        host="$remote.$DOMAIN"
      else
        host="$remote"
      fi

      echo "Adding ssh://$host/var/cache/media/$file..."
      git remote add $remote ssh://$host/var/cache/media/$file
    done

    echo ""
    )
  fi
done