#!/bin/sh
adb pull /data/data/com.android.providers.telephony/databases/mmssms.db mmssms.db > /dev/null 2>&1

startid=0

mkdir smsmms > /dev/null 2>&1
for thr in `sqlite3 -line mmssms.db 'select _id from threads where _id > '$startid';' | awk -F"= " '{print $2}'`; do
  subject=`sqlite3 -line mmssms.db 'select snippet from threads where _id='$thr' ;' | awk -F" = " '{print $2}'`
  #echo $subject
  mkdir "smsmms/thread_"$thr > /dev/null 2>&1
  
  # MMS 
  mids=`sqlite3 -line mmssms.db 'select _id from pdu where thread_id = '$thr'; ' | awk -F"= " '{print $2}'`" "
  for mid in $mids; do
    ids=`sqlite3 -line mmssms.db 'select _id from part where mid = '$mid';' | awk -F"= " '{print $2}'`" "
    from=`sqlite3 -line mmssms.db 'select address from addr where msg_id='$mid' and type=137;' | awk -F"= " '{print $2}'`
    tofrom="From"
    if [[ $from == "insert-address-token" ]]; then 
      tofrom="To"
      from=`sqlite3 -line mmssms.db 'select address from addr where msg_id='$mid' and type=151;' | awk -F"= " '{print $2}'`
    fi
    epoch=`sqlite3 -line mmssms.db 'select date from pdu where _id='$mid' ;' | awk -F"= " '{print $2}'`
    date=`perl -e 'print scalar(localtime('$epoch'))'`
    messageid=`sqlite3 -line mmssms.db 'select m_id from pdu where _id='$mid' ;' | awk -F"= " '{print $2}'`
    subject=`sqlite3 -line mmssms.db 'select sub from pdu where _id='$mid' ;' | awk -F"= " '{print $2}'`
    mkdir "smsmms/thread_"$thr"/"$epoch"000_mms" > /dev/null 2>&1
    echo $tofrom": "$from > "smsmms/thread_"$thr"/"$epoch"000_mms/message_details.txt"
    echo "Date: "$date >> "smsmms/thread_"$thr"/"$epoch"000_mms/message_details.txt"
    echo "Message-ID: "$messageid >> "smsmms/thread_"$thr"/"$epoch"000_mms/message_details.txt"
    echo "Subject: "$subject >> "smsmms/thread_"$thr"/"$epoch"000_mms/message_details.txt"
    echo "" >> "smsmms/thread_"$thr"/"$epoch"000_mms/message_details.txt"
    for id in $ids; do
      name=`sqlite3 -line mmssms.db 'select name from part where _id = '$id';' | awk -F"= " '{print $2}'`
      cl=`sqlite3 -line mmssms.db 'select cl from part where _id = '$id';' | awk -F"= " '{print $2}'`
      data=`sqlite3 -line mmssms.db 'select _data from part where _id = '$id';' | awk -F"= " '{print $2}'`
      if [[ $name == "" ]]; then name=$cl ; fi
      adb pull $data "smsmms/thread_"$thr"/"$epoch"000_mms/"$name > /dev/null 2>&1     
    done
  done
  
  # SMS
  mids=`sqlite3 -line mmssms.db 'select _id from sms where thread_id = '$thr'; ' | awk -F"= " '{print $2}'`" "
  for mid in $mids; do
    type=`sqlite3 -line mmssms.db 'select type from sms where _id='$mid';' | awk -F"= " '{print $2}'`
    tofrom="From"
    if [[ $type == "2" ]]; then tofrom="To" ; fi
    from=`sqlite3 -line mmssms.db 'select address from sms where _id='$mid';' | awk -F"= " '{print $2}'`
    epoch=`sqlite3 -line mmssms.db 'select date from sms where _id='$mid' ;' | awk -F"= " '{print $2}'`
    eplen=$((${#epoch} - 3))
    sepoch=${epoch:0:$eplen}
    date=`perl -e 'print scalar(localtime('$sepoch'))'`
    servicecenter=`sqlite3 -line mmssms.db 'select service_center from sms where _id='$mid' ;' | awk -F"= " '{print $2}'`
    body=`sqlite3 -line mmssms.db 'select body from sms where _id='$mid' ;'`
    mkdir "smsmms/thread_"$thr"/"$epoch"_sms" > /dev/null 2>&1
    echo $tofrom": "$from > "smsmms/thread_"$thr"/"$epoch"_sms/body.txt"
    echo "Date: "$date >> "smsmms/thread_"$thr"/"$epoch"_sms/body.txt"
    echo "Service Center: "$servicecenter >> "smsmms/thread_"$thr"/"$epoch"_sms/body.txt"
    echo "" >> "smsmms/thread_"$thr"/"$epoch"_sms/body.txt"
    echo ${body/body = /} >> "smsmms/thread_"$thr"/"$epoch"_sms/body.txt"
  done
done

rm mmssms.db> /dev/null 2>&1

exit 0

