#!/bin/bash
#
TARGET_IP="192.168.8.108"

function build() {
    if [ -n "$1" ]; then
        TARGET_IP=$1
    fi

    make PLATFORM=rv1109 server_protocol
    make PLATFORM=rv1109
    if [ $? -ne 0 ]; then
        exit 1
    fi
    echo "deply to $TARGET_IP"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "mount -o remount rw /system/"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "killall start-app.sh; pkill -f GateFace"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "pgrep -f netconfig | xargs kill -s 9"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "pgrep -f voucher_verify | xargs kill -s 9"
    scp -r -i ~/Projects/ssh_host_rsa_key_ok src/web/php/*.php root@${TARGET_IP}:/system/www/web/
    scp -i ~/Projects/ssh_host_rsa_key_ok ./build/GateFace root@${TARGET_IP}:/system/bin
    scp -i ~/Projects/ssh_host_rsa_key_ok ./build/voucher_verify root@${TARGET_IP}:/system/bin
    scp -i ~/Projects/ssh_host_rsa_key_ok ./src/netlink/libRwNetlink.so root@${TARGET_IP}:/system/lib
    scp -i ~/Projects/ssh_host_rsa_key_ok ./build/netconfig root@${TARGET_IP}:/system/bin
    scp -i ~/Projects/ssh_host_rsa_key_ok ./3rdparty/arm-linux-gnueabihf/rwStageProtocol/lib/librwSrvProtocol.so root@${TARGET_IP}:/system/lib/

    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "sync"

    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "echo 1 > /dev/watchdog"
}

function build_debug() {
    make PLATFORM=rv1109 server_protocol
    scp -r -i ~/Projects/ssh_host_rsa_key_ok 3rdparty/arm-linux-gnueabihf/rwStageProtocol/lib/librwSrvProtocol.so root@${TARGET_IP}:/system/lib/
    make PLATFORM=rv1109
    echo "deply to /sdcard"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "mount -o remount rw /system/"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "pkill -f start-app.sh & pkill -f GateFace & pkill -f dk_uart_id_reader"
    scp -r -i ~/Projects/ssh_host_rsa_key_ok src/web/php/*.php root@${TARGET_IP}:/system/www/web/
    scp -i ~/Projects/ssh_host_rsa_key_ok ./build/GateFace root@${TARGET_IP}:/sdcard
    scp -i ~/Projects/ssh_host_rsa_key_ok ./build/ryxconnector root@${TARGET_IP}:/sdcard
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "sync"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "amixer sset 'Master' 80%"
}

function copy_ssh() {
    TARGET_IP=$1
    SSH_KEY=$(cat ~/.ssh/id_rsa.pub)
    echo "ssh copy id to ${TARGET_IP} ..."
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "mount -o remount rw /oem/; mount -o remount rw /"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "if [ ! -f /usr/bin/scp ]; then cp /oem/bin/scp /usr/bin/; else echo 'scp exist'; fi"
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "echo ${SSH_KEY} >> /oem/.ssh/authorized_keys"
    scp -i ~/Projects/ssh_host_rsa_key_ok /mnt/e/Documents/gdb-rk root@${TARGET_IP}:/sdcard
    scp -i ~/Projects/ssh_host_rsa_key_ok /opt/toolchains/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/arm-linux-gnueabihf/libc/usr/bin/gdbserver root@${TARGET_IP}:/sdcard
    ssh -i ~/Projects/ssh_host_rsa_key_ok root@${TARGET_IP} "sync"
}

function main() {
    local cmd=$1
    shift 1
    case $cmd in
    build)
        build $@
        ;;
    debug)
        build_debug
        ;;
    ssh)
        copy_ssh $@
        ;;
    *)
        build
        ;;
    esac
}

main $@
