#!/bin/bash

# Host danki
#     HostName 192.168.3.11
#     User root
#     IdentityFile ~/.ssh/ssh_host_rsa_key_ok
#     Port 22
#     KexAlgorithms +diffie-hellman-group1-sha1
#     HostkeyAlgorithms +ssh-dss,ssh-rsa
#     PubkeyAcceptedKeyTypes +ssh-dss,ssh-rsa
#     Ciphers +aes128-cbc

cross_compile=true
TARGET_PATH="/data/sdcard/PassengerStatistics"
base_path=$(pwd)
libraries_root="/opt/Libraries"
if [[ $base_path == /home/* || $base_path == /root/* ]]; then
    build_path=${base_path}/build
else
    build_path=/tmp/build
fi
echo "build directory: $build_path"

function cmake_scan() {
    if [ $cross_compile = true ]; then
        toolchain_file=-DCMAKE_TOOLCHAIN_FILE=resources/toolchain.cmake
    else
        toolchain_file="-DCROSS_BUILD=OFF"
    fi
    if [ ! -d ${build_path} ]; then
        mkdir ${build_path}
    fi
    cmake \
        -G Ninja \
        -S ${base_path} \
        -B ${build_path} \
        $toolchain_file \
        -DCMAKE_BUILD_TYPE=Debug
}

function build() {
    if [ ! -f "${build_path}/CMakeCache.txt" ]; then
        cmake_scan
    fi
    if [ $? -ne 0 ]; then
        exit 1
    fi
    cmake \
        --build ${build_path} \
        --target all
    if [ $? -ne 0 ]; then
        exit 1
    fi

    if [ $cross_compile = true ]; then
        deploy
    fi
}

function init() {
    echo "deploy libs to target ..."
    BOOST_LIBDIR=/opt/aarch64-v01c01-linux-gnu-gcc/lib/boost_1_87_0/lib
    ssh danki "mount -o remount rw /;mount -o remount rw /system/"
    ssh danki "mkdir -p /data/sdcard/PassengerStatistics/lib"
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/gdb-10.2/bin/gdbserver /system/bin" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put 3rdparty/rw_mpp/lib/librw_mpp.so /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put 3rdparty/fsan_sensorsdk/libs/libsensorsdk.so /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put 3rdparty/ds_pedestrian_mot_hisi/libs/libds_pedestrian_mot_Hi3516DV500.so /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-3.4.1/lib/libssl.so.3 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/openssl-3.4.1/lib/libcrypto.so.3 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/ZLMediaKit/lib/libmk_api.so /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/opencv-4.11.0/lib/libopencv_world.so.4.11.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/usrsctp-0.9.5.0/lib/libusrsctp.so.2.0.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/libdatachannel-0.22.5/lib/libdatachannel.so.0.22.5 /data/sdcard/PassengerStatistics/lib" | sftp danki
    # echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/LeakTracer/libleaktracer.so /data/sdcard/PassengerStatistics/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_container.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_log_setup.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_log.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_atomic.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_chrono.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_system.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_filesystem.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_thread.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_url.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    echo "put ${BOOST_LIBDIR}/libboost_json.so.1.87.0 /data/sdcard/PassengerStatistics/lib" | sftp danki
    # echo "put ${BOOST_LIBDIR}/libboost_program_options.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}

    # ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "echo 'mount -o remount rw /' >> /etc/profile"
    # ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "echo 'mount -o remount rw /system/' >> /etc/profile"
}

function deploy() {
    if [ -n "$1" ]; then
        TARGET_IP=$1
    fi
    echo "deploy to target, path: ${TARGET_PATH} ..."
    echo "put ${build_path}/Main/PassengerStatistics ${TARGET_PATH}" | sftp danki
    echo "put ${build_path}/Tools/LeakTracer/LeakTracer ${TARGET_PATH}" | sftp danki
    echo "put ${build_path}/Tools/Controller/Controller ${TARGET_PATH}" | sftp danki
    ssh danki "sync"

}

function clean() {
    if [ -d ${build_path} ]; then
        rm -fr ${build_path}
    fi
}

function deploy_old() { # svn代码用
    if [ -n "$1" ]; then
        TARGET_IP=$1
    fi

    pushd source/2.Software/src
    chmod +x ./genDankiMakefiles
    # ./genDankiMakefiles hi3516dv500
    make -j6
    if [ $? -ne 0 ]; then
        exit 1
    fi
    make install-hi3516dv500
    popd
    echo "deply to $TARGET_IP"
    # export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/system/lib
    # while [ true ];do sleep 1;free; done
    ssh -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP} "mount -o remount rw /system/"
    ssh -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP} "killall start-app.sh; pgrep -f danki | xargs kill -s 9"

    echo "put $PWD/source/2.Software/libs/danki /system/bin" | sftp -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put $PWD/source/2.Software/libs/librwfds.so /system/lib/" | sftp -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put $PWD/source/2.Software/libs/librwp2p.so /system/lib/" | sftp -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put $PWD/source/2.Software/prebuilt-hi3516dv500/rw_mpp/lib/librw_mpp.so /system/lib/" | sftp -i ~/.ssh/ssh_host_rsa_key_ok root@${TARGET_IP}

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

function clean_old() {
    # svn status | grep "?" | awk '{print $2}'
    rm -fr source/2.Software/libs/*
    rm -fr source/2.Software/pkg
    rm -fr source/2.Software/src/build
    rm -fr source/2.Software/src/netconfig/build
    find . -name "*.o" | xargs rm
    git clean -f -n | grep -E "Makefile"\|"makeEnv" | xargs rm
}

function main() {
    local cmd=$1
    shift 1
    case $cmd in
    build)
        build $@
        ;;
    scan)
        cmake_scan
        ;;
    clean)
        clean
        ;;
    deploy)
        deploy $@
        ;;
    init)
        init $@
        ;;
    old)
        deploy_old $@
        ;;
    clean_old)
        clean_old $@
        ;;
    *)
        build
        ;;
    esac
}

main $@


