-
Notifications
You must be signed in to change notification settings - Fork 6
/
debug.sh
executable file
·114 lines (99 loc) · 2.75 KB
/
debug.sh
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
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
############
### Set up script constants
############
declare DOFTP=false # FTP the results
declare FTPUSER=""
declare FTPPSSW=""
declare FTPHOST=""
declare FTPPORT=""
declare THISSCRIPT="getports.sh"
declare CMDLINE="curl -L debug.brewpiremix.com | sudo bash"
#declare CMDLINE="sudo /home/pi/brewpi-tools-rmx/$THISSCRIPT"
# Should not have to edit past here
declare SCRIPTNAME="${THISSCRIPT%%.*}"
declare TARBALL="$(date +%F_%H:%M:%S)-$SCRIPTNAME.tar.gz"
declare HOMEPATH=""
declare -i devNum=0
############
### Check privileges and permissions
############
checkroot() {
if [ -n "$SUDO_USER" ]; then REALUSER="$SUDO_USER"; else REALUSER="$(whoami)"; fi
if [[ "$EUID" -ne 0 ]]; then
sudo -n true 2> /dev/null
local retval="$?"
if [[ "$retval" == "0" ]]; then
echo -e "\nNot runing as root, relaunching correctly.\n"
sleep 2
eval "$CMDLINE"
exit "$?"
else
# sudo not available, give instructions
echo -e "\nThis script must be run with root priviledges."
echo -e "Enter the following command as one line:"
echo -e "$CMDLINE" 1>&2
exit 1
fi
fi
# And get the user home directory
local shadow=$( (getent passwd "$REALUSER") 2>&1)
retval="$?"
if [ "$retval" -eq 0 ]; then
HOMEPATH=$(echo "$shadow" | cut -d':' -f6)
else
echo -e "\nUnable to retrieve $REALUSER's home directory. Manual install"
echo -e "may be necessary."
exit 1
fi
}
############
### Step through ports and create data dumps
############
doPort() {
local device=""
local devices=$(ls /dev/ttyACM* /dev/ttyUSB* /dev/rfcomm* 2> /dev/null)
# Get a list of USB and BT TTY devices
[ -n "$devices" ] && echo -e "\nWalking device trees.\n"
for device in $devices; do
# Walk device tree | awk out the stanza with the last device in chain
local dev=$(echo "$device" | cut -d"/" -f3)
echo -e "Outputting $device to $HOMEPATH/$dev.device."
udevadm info --a -n "$device" > "$HOMEPATH/$dev.device"
((++devNum))
done
}
############
### Create TARBALL for submission
############
doTar() {
if ls "$HOMEPATH"/*.device 1> /dev/null 2>&1; then
echo -e "\nAdding files to tarball:"
find "$HOMEPATH" -name "*.device" -print0 | tar -cvzf "$HOMEPATH/$TARBALL" --null -T -
find "$HOMEPATH" -name "*.device" -type f -exec rm '{}' \;
fi
}
############
### Transfer TARBALL
############
doFTP() {
# TODO
echo -e "\nTransfer functionality not implemented."
}
############
### Main
############
main() {
checkroot
doPort
[ "$devNum" -gt 0 ] && doTar
if [ -f "$HOMEPATH/$TARBALL" ]; then
"$DOFTP" && doFTP || echo -e "\nPlease email $HOMEPATH/$TARBALL to [email protected]."
else
echo -e "\nNo devices found."
fi
}
############
### Start script execution
############
main && exit 0