aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/seance
blob: 9c708cf735e1eafc0b7d4cc498c4a8ebf016d6ec (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
#!/bin/sh -eu

program=$(basename "$0")
directory=$HOME/.config/seance
session=$directory/session
spirits=$directory/spirits

help() {
printf \
"
Usage: %s [FLAGS]... [ARGUMENT]...

The program $program shall save and restore
running programs in a desktop session according
to a specified configuration file.

Add a program string, one per line,
to $spirits to
save programs in current session.

Session:       $session
Configuration: $spirits

  Command List:

   $program commit      Saves current running programs to session.
   $program list        List saved programs.
   $program config      View current configuration.
   $program search      Search for a running program.
   $program start       Restore previous session.
   $program -h --help   Show this help menu.

" "$program";
}

{ [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ] || [ "$#" = 0 ]; } && help && exit;

mkdir --parents "$directory"

touch "$spirits"

commit() {
  ps aux |
  awk '$1 == "'"$(whoami)"'" { $1=$2=$3=$4=$5=$6=$7=$8=$9=$10=""; print $0 }' |
  awk '!deduplicate[$0]++ { $1=$1; printf "%s &\n", $0 }'
}

[ "${1:-}" = "commit" ] &&
  { commit | grep --file="$spirits" > "$session" || printf "Warning empty or failed session file!\n"; } &&
  printf 'Session %s canonized.\n' "$session" &&
  exit

[ "${1:-}" = "list" ] && cat "$session" && exit;

[ "${1:-}" = "config" ] && cat "$spirits" && exit;

[ "${1:-}" = "search" ] && commit |  grep --ignore-case -- "$2" | head --lines=1 && exit;

[ "${1:-}" = "start" ] && /bin/sh "$session" && exit;

help && printf "Error: Unknown argument '%s'.\n\n" "$@" && exit 1;