1#!/system/bin/sh
2
3# TODO: restructure this to keep bugreports entirely on internal storage
4
5# Do not allow bugreports on user builds unless USB debugging
6# is enabled.
7if [ "x$(getprop ro.build.type)" = "xuser" -a \
8     "x$(getprop init.svc.adbd)" != "xrunning" ]; then
9  exit 0
10fi
11
12# Build emulated storage paths when appropriate
13# See storage config details at http://source.android.com/tech/storage/
14if [ -n "$EMULATED_STORAGE_SOURCE" ]; then
15  writePath="$EMULATED_STORAGE_SOURCE/0"
16  readPath="$EMULATED_STORAGE_TARGET/0"
17else
18  writePath="$EXTERNAL_STORAGE"
19  readPath="$EXTERNAL_STORAGE"
20fi
21
22tmpPath="/data/local/tmp"
23bugreportPath="bugreports"
24screenshotPath="Pictures/Screenshots"
25
26# Create directories if needed
27if [ ! -e "$writePath/$bugreportPath" ]; then
28  mkdir "$writePath/$bugreportPath"
29fi
30if [ ! -e "$writePath/$screenshotPath" ]; then
31  mkdir "$writePath/$screenshotPath"
32fi
33
34timestamp=`date +'%Y-%m-%d-%H-%M-%S'`
35
36# take screen shot
37# we run this as a bg job in case screencap is stuck
38/system/bin/screencap -p "$writePath/$screenshotPath/Screenshot_$timestamp.png" &
39
40# run bugreport
41/system/bin/dumpstate -o "$tmpPath/bugreport-$timestamp" $@
42
43# copy finished bugreport into place for sending
44cp "$tmpPath/bugreport-$timestamp.txt" "$writePath/$bugreportPath/bugreport-$timestamp.txt"
45# clean up any remaining files
46rm $tmpPath/bugreport*
47
48# invoke send_bug to look up email accounts and fire intents
49# make it convenient to send bugreport to oneself
50/system/bin/send_bug "$readPath/$bugreportPath/bugreport-$timestamp.txt" "$readPath/$screenshotPath/Screenshot_$timestamp.png"
51