summaryrefslogtreecommitdiffstats
path: root/packaging/macos/jhb/usr/bin/run-parts
blob: ff0470cb14e3472eb1b69ef1799426f797fb8889 (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
#!/usr/bin/env bash

### description ################################################################

# A partial replacement for Debian's "run-parts" tool. Execute files in a
# directory in their lexical order with the specialty of being able to
# use symlinks to create an order without executing the files twice.

### shellcheck #################################################################

# Nothing here.

### dependencies ###############################################################

# Nothing here.

### variables ##################################################################

# Nothing here.

### functions ##################################################################

function main
{
  local mode=$1
  local dir=$2

  local file_pattern

  if [ -d "$dir" ]; then
    # reconstruct to make it proper (e.g. remove superfluous slashes)
    dir=$(dirname "$dir")/$(basename "$dir")
  else
    # split into directory and file
    file_pattern=$(basename "$dir")
    dir=$(dirname "$dir")
  fi

  file_pattern=${file_pattern:-*}   # default pattern is "*"

  local linked_files
  linked_files=$(find "$dir" -type l -exec readlink {} +)

  for file in "$dir"/$file_pattern; do   # requires 'shopt -s nullglob'
    # Only process files that have no symlinks (in that same directory) pointing
    # at them.
    if [[ "$linked_files" != *$(basename "$file")* ]]; then
      case "$mode" in
        list)
          echo "$file"
          ;;
        run)
          $file
          ;;
      esac
    fi
  done
}

### main #######################################################################

set -e

shopt -s nullglob   # in case no files are found

case "$1" in
  list)
    main "list" "$2"
    ;;
  *)
    main "run" "$1"
    ;;
esac