#!/bin/sh
# Thesis Key Logging
# !!!Do not modify!!!
# Keep this in /usr/local/bin

readonly SERVER="keylog@tf2.csc.calpoly.io"
readonly PROG_NAME=$(basename $0)
readonly ORIG_PROG=$(which -a $PROG_NAME | tail -n-1)
readonly LOG_DIR="/var/log/thesis"
readonly KEY_FILE=~/.ssh/keylog_rsa
readonly OUT_FILE="$LOG_DIR/$PROG_NAME-log-$(date '+%Y%m%d%H%M%S').log"
for arg in $@; do
    uid="$(echo $arg | cut -d@ -f1 -s)"
    if [ "$uid" = "" ]; then
	break;
    fi
done
uid=$(echo "$uid" | sha256sum | cut -d' ' -f1)
$ORIG_PROG "$SERVER" "$uid" 2> /dev/null &

promptSubmit() {
  echo "Do you aprove this data to be submitted for this study? [y|n]" 
  read REPLY 

  if [ $REPLY = "y" ]; then
    scp -i "$KEY_FILE" "$OUT_FILE" "$SERVER":"$uid"
  elif [ $REPLY != "n" ]; then
    echo "Invalid response"
    promptSubmit
  fi
}

mkdir -p $LOG_DIR

echo "Reminder not to enter passwords into this SSH session."
strace -ttt -e trace=read -e read=0 -s200 -o $OUT_FILE \
       $ORIG_PROG $@

sed -i '2,${ /read([0-9], ".*", 16384)/!d }' "$OUT_FILE" 

less "$OUT_FILE"
promptSubmit
