#!/bin/sh


cd /root

hciconfig hci0 up

#hcitool -i hci0 scan

bluetoothd

sleep 3

# headset
hciconfig hci0 class 280404
#hciconfig hci0 name 'sif headset'
hciconfig hci0 name 'hark headset'

# scan
hciconfig hci0 piscan

# voice path to HCI
#hcitool -i hci0 cmd 3f 1d 00 >& /dev/null

# voice path to PCM
hcitool -i hci0 cmd 3f 1d 01 >& /dev/null

# PCM master
#hcitool -i hci0 cmd 3f 07 02 >& /dev/null

# PCM slave
hcitool -i hci0 cmd 3f 07 00 >& /dev/null

# daemon動作 16K->8K ダウンサンプリングあり
#./bt -d -r

# daemon動作 16K->8K ダウンサンプリングあり 常時音声出力
./bt -d -r -a 

# daemon動作 16K->8K ダウンサンプリングあり 音声認識起動
#./bt -d -r -w

#echo 'bluetooth start...'

## Gadget RASP-MX Enable

# Tablet Mic Mode
modprobe g_raspmx mic_mode=0

# Cascade Mic Mode
#modprobe g_raspmx mic_mode=1


## USB(HOST Side) Reset
echo 0 > /sys/bus/usb/devices/usb1/authorized
usleep 200000	# 200ms
echo 1 > /sys/bus/usb/devices/usb1/authorized
usleep 200000	# 200ms

## Multi CPU Enable
echo 1 > /sys/devices/system/cpu/cpu1/online
usleep 200000	# 200ms
echo 1 > /sys/devices/system/cpu/cpu2/online
usleep 200000	# 200ms
echo 1 > /sys/devices/system/cpu/cpu3/online
usleep 200000	# 200ms

## LED Control
# CPU_LED0 (RED)
echo 102 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio102/direction
echo 1 > /sys/class/gpio/gpio102/value
hciconfig | grep "DOWN" > /dev/null
if [ $? -eq 0 ] ; then
echo 0 > /sys/class/gpio/gpio102/value
fi
usleep 200000	# 200ms
# CPU_LED0 (GREEN)
echo 103 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio103/direction
echo 0 > /sys/class/gpio/gpio103/value
usleep 200000	# 200ms

## SSH authorized keys setup
if [ ! -d /root/.ssh ]; then
  tar -xvf /root/ssh_keys.tar -C /root/
else
  if [ ! -e /root/.ssh/id_rsa ]; then
    tar -xvf /root/ssh_keys.tar -C /root/
  fi
fi

## Demo
#/hark/hark --daemon --config /hark/hark-wav.conf --stanby

## HARK Auto Start
#/hark/hark --daemon --config /hark/hark-wav.conf

##/hark/hark --daemon --config /hark/hark-f8_demo.conf

# TSP
#/hark/hark --daemon --config /hark/hark-ody_tsp.conf
# MTSP
#/hark/hark --daemon --config /hark/hark-ody_mtsp.conf

#for demo
if [ -e /dev/obd ]; then
  /root/vsserver -d /dev/obd &
fi
if [ ! -d /media/usb0 ]; then
  /hark/hark --daemon --config /hark/hark-ody_tsp.conf
else
  /hark/hark --daemon --config /hark/hark-ody_tsp_usb.conf
fi

echo "nameserver 192.168.8.1" > /tmp/resolv.conf
route add default gw 192.168.8.1
usleep 1000000
date -s "$(curl -s --head http://www.google.co.jp | grep ^Date | cut -b 12- | sed s/Jan/01/g | sed s/Feb/02/g | sed s/Mar/03/g | sed s/Apr/04/g | sed s/May/05/g | sed s/Jun/06/g | sed s/Jul/07/g | sed s/Aug/08/g | sed s/Sep/09/g | sed s/Oct/10/g | sed s/Nov/11/g | sed s/Dec/12/g | awk '{print $3"-"$2"-"$1" "$4}')"
echo "JST-0900" > /etc/TZ
#export TZ="JST-0900"
#export TZ="JST-9"

#exec 3<>/dev/tcp/www.iij.ad.jp/80
#echo -e 'HEAD / HTTP/1.0\r\n\r' >&3
#DATE=$(grep '^Date:' <&3)
#date -s "${DATE#Date:}"

