#!/bin/bash

git_root_dir=$(dirname $(dirname $(dirname $(realpath $0))))
build_root_dir=${git_root_dir}/build.wfc
build_doc_dir=${build_root_dir}/docs/03-configuration
local_tars_rel_dir=${build_root_dir}/release/tars
local_debs_rel_dir=${build_root_dir}/release/debs
wfc_rel_dir_205=/mnt/public/release/omc/wfc
tars_rel_dir_205=${wfc_rel_dir_205}/tars/
debs_rel_dir_205=${wfc_rel_dir_205}/debs/
server_ip_205=192.168.1.205
passwd=admin123

usage() {
	echo "Usage: sh cpto205.sh {rel_date}"
	echo "  {rel_date} should be in the format YYYYMMDD"
}

if [ ! -z "$1" ]; then
	rel_date=$1
else
	rel_date=$(date +%Y%m%d)
fi

quickstart_file=${build_doc_dir}/03-WANFi软件快速安装.pdf
latest_tar_file=$(ls -t ${local_tars_rel_dir}/*${rel_date}* 2>/dev/null | head -n 1)
latest_deb_file=$(ls -t ${local_debs_rel_dir}/*${rel_date}* 2>/dev/null | head -n 1)

# Use expect to automatically answer yes and input password for rsync commands

if [ -n "${latest_deb_file}" ]; then
    expect <<EOF
    set timeout -1
    spawn rsync -avz ${latest_deb_file} agtuser@${server_ip_205}:${debs_rel_dir_205}
    expect {
        "yes/no" { 
            send "yes\n"
            exp_continue 
        }
        "password:" { send "$passwd\n" }
    }
    expect eof
EOF
fi

if [ -n "${latest_tar_file}" ]; then
    expect <<EOF
    set timeout -1
    spawn rsync -avz ${latest_tar_file} agtuser@${server_ip_205}:${tars_rel_dir_205}
    expect {
        "yes/no" { 
            send "yes\n"
            exp_continue 
        }
        "password:" { send "$passwd\n" }
    }
    expect eof
EOF
fi

if [ -f "${quickstart_file}" ]; then
    expect <<EOF
    set timeout -1
    spawn rsync -avz ${quickstart_file} agtuser@${server_ip_205}:${wfc_rel_dir_205}
    expect {
        "yes/no" { 
            send "yes\n"
            exp_continue 
        }
        "password:" { send "$passwd\n" }
    }
    expect eof
EOF
fi

