Fix the service file overwriting issue on the common profile
[platform/core/connectivity/bluetooth-tools.git] / scripts / bt-hci-logdump.sh
1 #!/bin/sh
2
3 #--------------------------------------
4 #    bluetooth hci
5 #--------------------------------------
6
7 CHOWN="/bin/chown"
8
9 eval $(tzplatform-get TZ_USER_DOWNLOADS)
10
11 DUMP_BASE_DIR=$1
12 BLUETOOTH_DEBUG=${1}/bluetooth
13
14 PREV_PWD=${PWD}
15 #BT_DUMP_DIR=${TZ_USER_DOWNLOADS}/.bt_dump
16 BT_DUMP_DIR=/opt/usr/media/Downloads/.bt_dump
17
18 if [ "$1" = "syslog" ]
19 then
20         DUMP_BASE_DIR=${BT_DUMP_DIR}
21         BLUETOOTH_DEBUG=${DUMP_BASE_DIR}/bluetooth
22 fi
23
24 BT_DUMP_TMP_DIR=/tmp/bt_dump
25 LOG_FILE="bt_hcidump.log"
26
27 process_btdump()
28 {
29         if [ -e btmon.log.1 -a -e btmon.log ]
30         then
31                 /usr/bin/tail -c +17 btmon.log > btmon.log_bin
32                 /bin/cat btmon.log.1 btmon.log_bin > final.log
33                 /usr/bin/btsnoop -s final.log
34                 if [ $? -eq 0 ]
35                 then
36                         /bin/rm -f btmon.log_bin btmon.log.1 btmon.log final.log
37                         LOG_FILE="btsnoop_*.log"
38                 else
39                         /bin/rm -f btmon.log_bin btmon.log.1 btmon.log
40                         /bin/mv final.log btmon.log
41                         LOG_FILE=""
42                 fi
43         elif [ -e bt_hcidump.log.1 -a -e bt_hcidump.log ]
44         then
45                 /usr/bin/tail -c +17 bt_hcidump.log > bt_hcidump.log_bin
46                 /bin/cat bt_hcidump.log.1 bt_hcidump.log_bin > bt_hcidump.log
47                 /bin/rm -f bt_hcidump.log_bin bt_hcidump.log.1
48         elif [ -e btmon.log ]
49         then
50                 /usr/bin/btsnoop -s btmon.log
51                 if [ $? -eq 0 ]
52                 then
53                         /bin/rm -f btmon.log
54                         LOG_FILE="btsnoop_*.log"
55                 else
56                         LOG_FILE=""
57                 fi
58         fi
59
60         if [ "${LOG_FILE}a" != "a" ]
61         then
62                 /usr/bin/rename .log .cfa ${LOG_FILE}
63         fi
64 }
65
66 if [ ! -e ${BT_DUMP_DIR} ]
67 then
68         if [ "$1" = "syslog" ]
69         then
70                 /bin/mkdir -p ${BT_DUMP_DIR}
71                 /bin/chown 200:200 ${BT_DUMP_DIR}
72         else
73                 exit 0
74         fi
75 fi
76
77 if [ "$1" = "syslog" ]
78 then
79         cp /var/log/messages ${BT_DUMP_DIR}/messages_`date "+%b_%d_%H:%M:%S_%Y"`
80         cp /var/log/messages.0 ${BT_DUMP_DIR}/messages.0_`date "+%b_%d_%H:%M:%S_%Y"`
81         # Skip the orginal bt_dump extract part
82         exit 0
83 fi
84
85 if [ ${DUMP_BASE_DIR} ]
86 then
87         cd ${DUMP_BASE_DIR}
88 fi
89
90 /bin/mkdir -p ${BLUETOOTH_DEBUG}
91 ${CHOWN} 200:200 ${BLUETOOTH_DEBUG}
92
93 /bin/mkdir -p ${BT_DUMP_TMP_DIR}
94 /bin/cp -rf ${BT_DUMP_DIR}/* ${BT_DUMP_TMP_DIR}
95
96 cd ${BT_DUMP_TMP_DIR}
97 process_btdump
98
99 if [ -e "${BT_DUMP_TMP_DIR}/old_hcidump" ]
100 then
101         cd ${BT_DUMP_TMP_DIR}/old_hcidump
102         process_btdump
103 fi
104
105 cd ${BT_DUMP_TMP_DIR}
106 /bin/tar czf ${BLUETOOTH_DEBUG}/bt_dump.tar.gz *
107
108 cd ${BLUETOOTH_DEBUG}
109 ${CHOWN} 200:200 ${BLUETOOTH_DEBUG}/bt_dump.tar.gz
110 /bin/rm -rf ${BT_DUMP_TMP_DIR}
111
112 cd ${PREV_PWD}