summaryrefslogtreecommitdiff
path: root/bin/rcup
blob: 4c3529dae675cd7851d5372aa7d8ec83cb905aac (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
#!/bin/sh

#set -x

REPLACE_ALL=0

DEST_DIR=$HOME
DEBUG=:
PRINT=echo
PROMPT=echo_n
VERBOSE=:
MKDIR=mkdir
LN=ln
RM=rm

echo_n() {
  printf "%s " "$*"
}

link_file() {
  local src=$1
  local dest=$2

  $PRINT "linking $dest"
  if [ -h $dest ]; then
    rm -f $dest
  fi

  $LN -s $src $dest
}

replace_file() {
  local src=$1
  local dest=$2

  $DEBUG replace_file $1 $2
  
  $RM -rf $dest
  link_file $src $dest
}

is_nested() {
  echo $1 | sed "s:$DEST_DIR/::" | grep -q /
  return $?
}

is_identical() {
  diff -q -s $1 $2 > /dev/null
}

handle_dir() {
  local dest=$1

  $DEBUG handle_dir $1

  dirname $dest | xargs $MKDIR -p
}

handle_file() {
  local src=$1
  local dest=$2

  $DEBUG handle_file $1 $2

  if [ -e "$dest" ]; then
    if is_identical $src $dest; then
      $VERBOSE "identical $dest"
    elif [ $REPLACE_ALL -eq 1 ]; then
      replace_file $src $dest
    else
      $PROMPT "overwrite ${dest}? [ynaq]"
      read overwrite
      case $overwrite in
        a) REPLACE_ALL=1
           replace_file $src $dest
           ;;
        y) replace_file $src $dest ;;
        q) exit 1 ;;
        *) $VERBOSE "skipping $dest" ;;
      esac
    fi
  else
    link_file $src $dest
  fi
}

handle_command_line() {
  arg_tags=""
  verbosity=0
  version=0
  dotfiles_dirs=
  LS_ARGS=
  while getopts Vqvt:d: opt; do
    case "$opt" in
      t)
        arg_tags="$arg_tags $OPTARG"
        LS_ARGS="$LS_ARGS -t $OPTARG"
        ;;
      v) verbosity=$(($verbosity + 1));;
      q) verbosity=$(($verbosity - 1));;
      d)
        dotfiles_dirs="$dotfiles_dirs $OPTARG"
        LS_ARGS="$LS_ARGS -d $OPTARG"
        ;;
      V) version=1
    esac
  done
  shift $(($OPTIND-1))

  if [ $version -eq 1 ]; then
    version rcup
    exit 0
  elif [ $verbosity -ge 2 ]; then
    DEBUG=echo
    VERBOSE=echo
    PRINT=echo
  elif [ $verbosity -eq 1 ]; then
    DEBUG=:
    VERBOSE=echo
    PRINT=echo
  elif [ $verbosity -eq 0 ]; then
    DEBUG=:
    VERBOSE=:
    PRINT=echo
  else
    DEBUG=:
    VERBOSE=:
    PRINT=:
  fi

  if [ "x$arg_tags" != "x" ]; then
    TAGS=$arg_tags
  fi

  if [ "x$dotfiles_dirs" != "x" ]; then
    DOTFILES_DIRS=$dotfiles_dirs
  fi

  FILES=$@
  LS_ARGS="$LS_ARGS $FILES"
}

if [ -e $HOME/.rcrc ]; then
  . $HOME/.rcrc
fi

. `dirname $0`/../share/rcm/rcm.sh
handle_command_line $*

for dest_and_src in `lsrc $LS_ARGS`; do
  dest=`echo $dest_and_src | sed 's/:.*//'`
  src=`echo $dest_and_src | sed 's/.*://'`

  if is_nested $dest; then
    handle_dir $dest
  fi

  handle_file $src $dest
done