| 1 | #! /usr/bin/awk -f |
| 2 | |
| 3 | BEGIN { |
| 4 | FS = msg_fs ? msg_fs : "|" |
| 5 | OFS = msg_fs ? msg_fs : "|" |
| 6 | Kfs = key_fs ? key_fs : ":" |
| 7 | Module = "khatus_actuate_device_add_to_automount" |
| 8 | # Typically some file manager (thunar, pcmanfm, etc.), but can be whatever. |
| 9 | Execute_On_Mount = Execute_On_Mount ? Execute_On_Mount : "" |
| 10 | } |
| 11 | |
| 12 | $1 == "OK" && \ |
| 13 | $2 == "khatus_sensor_devices" && \ |
| 14 | $3 == "add" && \ |
| 15 | $4 ~ /[0-9]$/ { |
| 16 | mount_device($4) |
| 17 | } |
| 18 | |
| 19 | function mount_device(path, cmd, line, lines, line_count, status, i, |
| 20 | path_dev, path_mnt) { |
| 21 | cmd="udisksctl mount --block-device " path " --no-user-interaction; echo $?" |
| 22 | while(cmd | getline line) { |
| 23 | lines[++line_count] = line |
| 24 | } |
| 25 | close(cmd) |
| 26 | status = lines[line_count] |
| 27 | line_count-- |
| 28 | if (status == 0) { |
| 29 | for (i=1; i<=line_count; i++) { |
| 30 | line = lines[i] |
| 31 | if (line ~ /^Mounted /) { |
| 32 | split(line, parts, " +") |
| 33 | path_dev=parts[2] |
| 34 | path_mnt=line |
| 35 | sub("^Mounted " path_dev " at ", "", path_mnt) |
| 36 | sub("\.$", "", path_mnt) |
| 37 | alert("low", "successfully-mounted", path_dev " to " path_mnt) |
| 38 | if (Execute_On_Mount) { |
| 39 | system(Execute_On_Mount " '" path_mnt "'") |
| 40 | } |
| 41 | } else { |
| 42 | alert("hi", "unexpected-success-line", line) |
| 43 | } |
| 44 | } |
| 45 | } else { |
| 46 | alert("hi", "failed-to-mount-device", path) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | function alert(priority, subject, body) { |
| 51 | # priority : "low" | "med" | "hi" |
| 52 | # subject : no spaces |
| 53 | # body : anything |
| 54 | print("OK", Module, "alert", priority, subject, body) |
| 55 | } |