2015-02-11 14:43:37 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
|
|
|
source "$CURRENT_DIR/helpers.sh"
|
|
|
|
source "$CURRENT_DIR/variables.sh"
|
2015-02-12 00:31:56 +00:00
|
|
|
source "$CURRENT_DIR/shared.sh"
|
2015-02-11 14:43:37 +00:00
|
|
|
|
2015-02-12 14:52:10 +00:00
|
|
|
supported_tmux_version_ok() {
|
2015-02-20 14:48:36 +00:00
|
|
|
"$CURRENT_DIR/check_tmux_version.sh" "$SUPPORTED_VERSION"
|
2015-02-12 14:52:10 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:42:18 +00:00
|
|
|
get_interval() {
|
|
|
|
get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default"
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_save_not_disabled() {
|
|
|
|
[ "$(get_interval)" -gt 0 ]
|
|
|
|
}
|
|
|
|
|
2015-02-11 14:43:37 +00:00
|
|
|
enough_time_since_last_run_passed() {
|
|
|
|
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")"
|
2015-02-12 14:42:18 +00:00
|
|
|
local interval_minutes="$(get_interval)"
|
2015-02-11 14:43:37 +00:00
|
|
|
local interval_seconds="$((interval_minutes * 60))"
|
|
|
|
local next_run="$((last_saved_timestamp + $interval_seconds))"
|
|
|
|
[ "$(current_timestamp)" -ge "$next_run" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_and_run_tmux_resurrect_save_script() {
|
|
|
|
local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")"
|
|
|
|
if [ -n "$resurrect_save_script_path" ]; then
|
2015-02-20 14:48:36 +00:00
|
|
|
"$resurrect_save_script_path" "quiet" >/dev/null 2>&1 &
|
2015-02-11 14:43:37 +00:00
|
|
|
set_last_save_timestamp
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:16:42 +00:00
|
|
|
acquire_lock() {
|
2019-07-08 13:58:42 +00:00
|
|
|
# Sometimes tmux starts multiple saves in parallel. We want only one
|
|
|
|
# save to be running, otherwise we can get corrupted saved state.
|
|
|
|
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
|
2019-07-09 08:46:56 +00:00
|
|
|
# The following implements a lock that auto-expires after 100...200s.
|
2019-07-13 12:16:42 +00:00
|
|
|
local lock_generation=$((`date +%s` / 100))
|
2019-07-09 08:46:56 +00:00
|
|
|
local lockdir1="${lockdir_prefix}${lock_generation}"
|
2019-07-13 12:16:42 +00:00
|
|
|
local lockdir2="${lockdir_prefix}$(($lock_generation + 1))"
|
2019-07-08 13:58:42 +00:00
|
|
|
if mkdir "$lockdir1"; then
|
|
|
|
trap "rmdir "$lockdir1"" EXIT
|
|
|
|
if mkdir "$lockdir2"; then
|
|
|
|
trap "rmdir "$lockdir1" "$lockdir2"" EXIT
|
2019-07-13 12:16:42 +00:00
|
|
|
return 0
|
2019-07-04 17:35:47 +00:00
|
|
|
fi
|
2019-07-08 13:58:42 +00:00
|
|
|
fi
|
2019-07-13 12:16:42 +00:00
|
|
|
return 1 # Someone else has the lock.
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed && acquire_lock; then
|
|
|
|
fetch_and_run_tmux_resurrect_save_script
|
|
|
|
fi
|
2015-02-11 14:43:37 +00:00
|
|
|
}
|
|
|
|
main
|