#!/bin/bash
cross_compile=true
TARGET_IP="192.168.3.220"
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 [ -n "$1" ]; then
        TARGET_IP=$1
    fi
    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() {
    if [ -n "$1" ]; then
        TARGET_IP=$1
    fi
    echo "deploy libs to target $TARGET_IP..."
    BOOST_LIBDIR=/opt/aarch64-v01c01-linux-gnu-gcc/lib/boost_1_84_0/lib
    chmod 600 resources/ssh_host_rsa_key_ok
    ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "mount -o remount rw /;mount -o remount rw /system/"
    echo "put ${base_path}/resources/authorized_keys /system/.ssh" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    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}
    ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "mkdir -p /data/sdcard/PassengerStatistics/lib"
    echo "put /opt/aarch64-v01c01-linux-gnu-gcc/lib/ZLMediaKit/lib/libmk_api.so /data/sdcard/PassengerStatistics/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    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_date_time* /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_regex.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_log_setup.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_log.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_atomic.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_chrono.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_container.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_filesystem.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_thread.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_url.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${BOOST_LIBDIR}/libboost_json.so.1.84.0 /system/lib" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    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"
    ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "echo 'export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/system/lib:/data/sdcard/PassengerStatistics/lib' >> /etc/profile"
}

function deploy() {
    if [ -n "$1" ]; then
        TARGET_IP=$1
    fi
    echo "deploy to target $TARGET_IP, path: ${TARGET_PATH} ..."
    echo "put ${build_path}/Main/PassengerStatistics ${TARGET_PATH}" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${build_path}/Tools/VideoRecoder/VideoRecoder ${TARGET_PATH}" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    echo "put ${build_path}/Tools/LeakTracer/LeakTracer ${TARGET_PATH}" | sftp -i resources/ssh_host_rsa_key_ok root@${TARGET_IP}
    ssh -i resources/ssh_host_rsa_key_ok root@${TARGET_IP} "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 $@


