summaryrefslogtreecommitdiff
path: root/bin/rcdn
blob: 2c52be40596f51cde1a93a4cf11760a25a30f9b9 (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/sh

: ${RCM_LIB:=`dirname $0`/../share/rcm}
. $RCM_LIB/rcm.sh

remove_link() {
  local dest=$1
  local original=$2
  local sigil=$3

  if [ x$dest = "x/" ]; then
    $VERBOSE "not a symlink, skipping: $original"
  elif [ -L $dest -o "x$sigil" = "xX" ]; then
    $RM -rf $dest
    rmdir -p `dirname $original` 2>/dev/null
  else
    remove_link `dirname $dest` $original
  fi
}

handle_command_line() {
  local arg_tags=
  local verbosity=0
  local version=0
  local run_hooks=0
  local dotfiles_dirs=
  local files=
  local excludes=
  local includes=

  while getopts VqvI:x:t:d: opt; do
    case "$opt" in
      I) includes="$includes $OPTARG";;
      k) run_hooks=1 ;;
      K) run_hooks=0 ;;
      t) arg_tags="$arg_tags $OPTARG" ;;
      v) verbosity=$(($verbosity + 1));;
      q) verbosity=$(($verbosity - 1));;
      d) dotfiles_dirs="$dotfiles_dirs $OPTARG" ;;
      V) version=1 ;;
      x) excludes="$excludes $OPTARG" ;;
    esac
  done
  shift $(($OPTIND-1))

  handle_common_flags rcup $version $verbosity

  tags=${arg_tags:-$TAGS}
  dotfiles_dirs=${dotfiles_dirs:-$DOTFILES_DIRS}
  files=$@
  RUN_HOOKS=$run_hooks

  for tag in $tags; do
    LS_ARGS="$LS_ARGS -t $tag"
  done
  for dotfiles_dir in $dotfiles_dirs; do
    LS_ARGS="$LS_ARGS -d $dotfiles_dir"
  done
  for exclude in $excludes; do
    LS_ARGS="$LS_ARGS -x $exclude"
  done
  for include in $includes; do
    LS_ARGS="$LS_ARGS -I $include"
  done
  LS_ARGS="$LS_ARGS $files"

  $DEBUG "LS_ARGS: $LS_ARGS"
}

LS_ARGS=-F

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

handle_command_line $*
: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}

run_hooks pre down

for dest_and_src in `lsrc $LS_ARGS`; do
  saved_ifs=$IFS
  IFS=:
  set $dest_and_src
  IFS=$saved_ifs
  dest=$1
  sigil=$3

  remove_link $dest $dest $sigil
done

run_hooks post down