#!/bin/csh -f
#for android with shellms apk installed

#parameters
#port
set myport=4444
#hostname/IP
set phone=btsamsung
if ( x$1 == x-p ) then
	set phone=$2
	shift
	shift
endif

#directory with contacts 
set contacts=~/.vcf

#first-time phone setup
#adb usb
#adb devices
#adb tcpip 4444
#adb connect  vpnsamsung:4444
#adb devices
#disconnect usb

set daemon=`ps -elf | grep  'adb fork-server' | grep -cv grep`
if ( $daemon == 0 ) adb connect ${phone}:${myport}
set deviceok=`adb devices|awk '$2=="device"'|wc -l`

if($deviceok != 1) then
echo ADB connection not OK
adb devices
exit 1
endif

#find contact
set isnumber=`echo $1|awk '{if($1 ~ "^[+0-9][+0-9]*$") print 1; else print 0;}'`
if ($isnumber == 1) then
	set number=$1
else
	if (! -e $contacts/$1.vcf) then
		echo Unknown contact: $1
		exit 1
	endif
	set number=`grep 'TEL;CELL' $contacts/$1.vcf|awk -F: '{print $2}'|head -1`
endif

#read input
awk >/tmp/sms$$.tmp '{if($0==".") exit; else print $0;}'

#split to pieces
split -a 2 -b 160 /tmp/sms$$.tmp /tmp/sms$$.

#send
foreach i (/tmp/sms$$.??)
	set text=`cat $i`
	adb shell am startservice --user 0 -n com.android.shellms/.sendSMS -e contact $number -e msg "$text"
end

rm -f /tmp/sms$$.*
