-
Notifications
You must be signed in to change notification settings - Fork 281
/
fpp
executable file
·101 lines (94 loc) · 3.34 KB
/
fpp
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
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# get the directory of this script so we can execute the related python
# http://stackoverflow.com/a/246128/212110
SOURCE=$0
# resolve $SOURCE until the file is no longer a symlink
while [ -h "$SOURCE" ]; do
BASEDIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to
# the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$BASEDIR/$SOURCE"
done
BASEDIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
PYTHONCMD="python3"
NONINTERACTIVE=false
# Setup according to XDG/Freedesktop standards as specified by
# https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
if [ -z "$FPP_DIR" ]; then
if [ -z "$XDG_CACHE_HOME" ]; then
FPP_DIR="$HOME/.cache/fpp"
else
FPP_DIR="$XDG_CACHE_HOME/fpp"
fi
fi
function doProgram {
# process input from pipe and store as pickled file
$PYTHONCMD "$BASEDIR/src/process_input.py" "$@"
# if it failed, just fail now and exit the script
# this works for the looping -ko case as well
if [[ $? != 0 ]]; then exit $?; fi
# now close stdin and choose input...
exec 0<&-
$PYTHONCMD "$BASEDIR/src/choose.py" "$@" < /dev/tty
# Determine if running from within vim shell
IFLAG=""
if [ -z "$VIMRUNTIME" -a "$NONINTERACTIVE" = false ]; then
IFLAG="-i"
fi
# execute the output bash script. For zsh or bash
# shells, we delegate to $SHELL, but for all others
# (fish, csh, etc) we delegate to bash.
#
# We use the following heuristics from
# http://stackoverflow.com/questions/3327013/
# in order to determine which shell we are on
if [ -n "$BASH" -o -n "$ZSH_NAME" ]; then
if [ -e "${SHELL}" ]; then
$SHELL $IFLAG "$FPP_DIR/.fpp.sh" < /dev/tty
else
(>&2 echo "Your SHELL bash variable ${SHELL} does not exist, please export it explicitly");
$BASH $IFLAG "$FPP_DIR/.fpp.sh" < /dev/tty
fi
else
/bin/bash $IFLAG "$FPP_DIR/.fpp.sh" < /dev/tty
fi
}
# we need to handle the --help option outside the python
# flow since otherwise we will move into input selection...
for opt in "$@"; do
if [ "$opt" == "--debug" ]; then
echo "Executing from '$BASEDIR'"
elif [ "$opt" == "--version" ]; then
VERSION="$($PYTHONCMD "$BASEDIR/src/version.py")"
echo "fpp version $VERSION"
exit 0
elif [ "$opt" == "--python2" ]; then
echo "Python 2 is no longer supported. Please use Python 3."
exit 1
elif [ "$opt" == "--help" -o "$opt" == "-h" ]; then
$PYTHONCMD "$BASEDIR/src/print_help.py"
exit 0
elif [ "$opt" == "--record" -o "$opt" == "-r" ]; then
echo "Recording input and output..."
elif [ "$opt" == "--non-interactive" -o "$opt" == "-ni" ]; then
NONINTERACTIVE=true
elif [ "$opt" == "--keep-open" -o "$opt" == "-ko" ]; then
# allow control-c to exit the loop
# http://unix.stackexchange.com/a/48432
trap "exit" INT
while true; do
doProgram "$@"
# connect tty back to stdin since we closed it
# earlier. this also works since we will only read
# from stdin once and then go permanent interactive mode
# http://stackoverflow.com/a/1992967/948126
exec 0</dev/tty
done
fi
done
doProgram "$@"