-
Notifications
You must be signed in to change notification settings - Fork 18
/
mpvc-autostart
executable file
·89 lines (78 loc) · 2.7 KB
/
mpvc-autostart
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
#!/usr/bin/env sh
#
# @file mpvc-autostart
# @description mpvc auto pause/resume audio based on device presence
# @author gmt4 <gmt4 at github.com> (c) Copyright 2022 GPLv2+
# @url github.com/gmt4/mpvc
# SPDX-License-Identifier: GPL-2.0-or-later
#
PROGNAME=${0##*/}
PROGVERSION="v1.5"
PROGAUTHOR=gmt4
PROGURL="https://github.com/gmt4/mpvc"
set -euf
mpvc_defaults()
{
ARGS=${ARGS:-}
}
mpvc_pingcheck() { ping -c 5 "$@" | awk 'NR == 2 && /time=/ { sub(":.*time=",": time="); print }'; }
mpvc_btcheck() { hcitool scan | awk "/$@/ {print}"; }
# monitor presence of your phone (eg LAN IP/BT): if device is present resume audio, otherwise, pause audio.
mpvc_autoscan()
{
target="$1"
CHECK=${CHECK:-mpvc_pingcheck}
PERIOD=${PERIOD:-300}
NCHECK=${NCHECK:-3}
MPVC_HOURMIN=${MPVC_HOURMIN:-6}
MPVC_HOURMAX=${MPVC_HOURMAX:-20}
ncheck=0
echo "# $PROGNAME $@ CHECK=$CHECK PERIOD=$PERIOD NCHECK=$NCHECK"
while sleep ${PERIOD}
do
hour=$(date +%_H)
pause=$(mpvc -q get pause || true)
check=$($CHECK $target)
nsign=$(if [ -n "$check" ]; then echo +; else echo -; fi)
ncheck=$(( ( $ncheck $nsign 1 ) % ( $NCHECK + 1 ) ))
if [ "$hour" -ge ${MPVC_HOURMIN} -a "$hour" -lt ${MPVC_HOURMAX} ]
then
if [ "$ncheck" -gt 0 ]; then mpvc -q resume || true; fi # n>0 pings up: resume
if [ "$ncheck" -le -$NCHECK ]; then mpvc -q pause || true; fi # n<0 pings down: pause
fi
echo "# $(date) PERIOD=$PERIOD NCHECK=$NCHECK pause=$pause c=${#check} n=${ncheck} $check" # $@
done
}
optflags="hi:n:p:m:M:"
usage()
{
echo "usage: $PROGNAME -[$optflags] args # @version $PROGVERSION (c) $PROGAUTHOR $PROGURL"
echo " -i : The IP of the device to detect ping presence for $PROGNAME (*required)"
echo " -p : The number of seconds between ping presence checks"
echo " -n : The number of failed ping presence checks after which to pause mpvc"
echo " -m : The minimum hour when to start ping presence (for example morning: 7 AM)"
echo " -M : The maximum hour when to stop ping presence (for example night: 20 PM)"
echo "*tips: If unsure where to begin, run: $PROGNAME -i 192.168.0.2 # your tablet/phone LAN IP"
exit;
}
main()
{
[ $# -lt 1 ] && usage
mpvc_defaults
while getopts "$optflags" flag;
do
case "$flag" in
h) usage ;;
i) ARGS="$ARGS $OPTARG"; ;;
p) PERIOD="$OPTARG"; ;;
n) NCHECK="$OPTARG"; ;;
m) MPVC_HOURMIN="$OPTARG" ;;
M) MPVC_HOURMAX="$OPTARG" ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[ -z "${ARGS:-}" ] && usage "Error: No -i option provided (required)"
mpvc_autoscan "$ARGS"
}
main "$@"