#!/bin/bash

cp /data/edgeBox/pposlink/pposlink /usr/local/bin/
cp /data/edgeBox/pposmirouter/pposmirouter /usr/local/bin/
cp /data/edgeBox/frpc/frpc /usr/local/bin/
cp /data/edgeBox/pposlink/pposlink.service /etc/systemd/system/
cp /data/edgeBox/pposmirouter/pposmirouter.service /etc/systemd/system/

chmod 755 /usr/local/bin/pposlink
chmod 755 /usr/local/bin/frpc
chmod 755 /usr/local/bin/pposmirouter

subDomain=$(ip link show | awk '/ether/ {print $2; exit}' | sed 's/://g')

echo "edge box id: $subDomain"

nginxInstall(){
  if [ -f /etc/nginx/conf.d/passwd.db ]; then
    echo "nginxInstall is done"
    return 1
  fi
  apt install -y nginx && cp -r /data/edgeBox/openresty/conf.d/* /etc/nginx/conf.d && systemctl restart nginx && echo "nginxInstall is doing success"
}



cat >/etc/systemd/system/frpc.service<< EOF
[Unit]
Description=frpc

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/frpc http -s 47.110.147.31 -P 7000 --tls_enable -t ed2c4592529 -l 8080 -n $subDomain.edge2.papa.com.cn --sd $subDomain

Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target
EOF


nginxInstall && systemctl daemon-reload && systemctl start pposlink.service && systemctl enable pposlink.service && systemctl start frpc.service && systemctl enable frpc.service  && systemctl start pposmirouter.service && systemctl enable pposmirouter.service && echo "install success"



