#!/bin/bash
#
# Welcom to use Bigemap Server
# Copyright(c) 2020 BIGEMAP
#

SERVER_URL="http://downloads.bigemap.com"

if [[ "$OSTYPE" == "linux-gnu" ]]; then
  PLATFORM="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
  PLATFORM="mac"
else
  PLATFORM="linux"
fi

RED='\033[31m'
GREEN='\033[32m'
NC='\033[0m'

bm_echo() {
  command printf %s\\n "$*" 2>/dev/null
}

bm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(command uname -a)"
  local NVM_OS
  case "${NVM_UNAME}" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
    AIX\ *) NVM_OS=aix ;;
  esac
  bm_echo "${NVM_OS-}"
}

bm_get_arch() {
  local HOST_ARCH
  local NVM_OS
  local EXIT_CODE

  NVM_OS="$(bm_get_os)"
  if [ "_${NVM_OS}" = "_sunos" ]; then
    if HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install); then
      HOST_ARCH=$(bm_echo "${HOST_ARCH}" | command tail -1)
    else
      HOST_ARCH=$(isainfo -n)
    fi
  elif [ "_${NVM_OS}" = "_aix" ]; then
    HOST_ARCH=ppc64
  else
    HOST_ARCH="$(command uname -m)"
  fi

  local NVM_ARCH
  case "${HOST_ARCH}" in
    x86_64 | amd64) NVM_ARCH="amd64" ;;
    i*86) NVM_ARCH="i386" ;;
    aarch64) NVM_ARCH="arm64" ;;
    *) NVM_ARCH="${HOST_ARCH}" ;;
  esac
  bm_echo "${NVM_ARCH}"
}

BM_ARCH="$(bm_get_arch)"

self_signed() {
  OUTPATH=$1

  echo -e 'Generate CA'
  openssl genrsa -out $OUTPATH/ca-key.pem 1024
  echo -e 'Create certificate request'
  openssl req -new -key $OUTPATH/ca-key.pem -subj /C=CN/ST=SC/L=CD/O=Bigemap -out $OUTPATH/ca-req.csr
  echo -e 'Generate self-signed certificates'
  openssl x509 -req -in $OUTPATH/ca-req.csr -signkey $OUTPATH/ca-key.pem -out $OUTPATH/ca-cert.pem

  echo -e 'Generate server private key'
  openssl genrsa -out $OUTPATH/key.pem 1024
  echo -e 'Generate server public key'
  openssl rsa -in $OUTPATH/key.pem -pubout -out $OUTPATH/pub.pem

  echo -e 'Create server request'
  openssl req -new -key $OUTPATH/key.pem -subj /C=CN/ST=SC/L=CD/O=Bigemap/OU=bm-server -out $OUTPATH/req.csr
  echo -e 'Generate certificate'
  openssl x509 -req -CA $OUTPATH/ca-cert.pem -CAkey $OUTPATH/ca-key.pem -CAcreateserial -in $OUTPATH/req.csr -out $OUTPATH/cert.pem
}

parse_yaml() {
    local yaml_file=$1
    local prefix=$2
    local s
    local w
    local fs

    s='[[:space:]]*'
    w='[a-zA-Z0-9_.-]*'
    fs="$(echo @|tr @ '\034')"

    (
        sed -e '/- [^\“]'"[^\']"'.*: /s|\([ ]*\)- \([[:space:]]*\)|\1-\'$'\n''  \1\2|g' |

        sed -ne '/^--/s|--||g; s|\"|\\\"|g; s/[[:space:]]*$//g;' \
            -e "/#.*[\"\']/!s| #.*||g; /^#/s|#.*||g;" \
            -e "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
            -e "s|^\($s\)\($w\)${s}[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" |

        awk -F"$fs" '{
            indent = length($1)/2;
            if (length($2) == 0) { conj[indent]="+";} else {conj[indent]="";}
            vname[indent] = $2;
            for (i in vname) {if (i > indent) {delete vname[i]}}
                if (length($3) > 0) {
                    vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
                    printf("%s%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, conj[indent-1],$3);
                }
            }' |

        sed -e 's/_=/+=/g' |

        awk 'BEGIN {
                FS="=";
                OFS="="
            }
            /(-|\.).*=/ {
                gsub("-|\\.", "_", $1)
            }
            { print }'
    ) < "$yaml_file"
}

sed_d() {
  local TGTFILE=$1; shift
  while [ $# != 0 ]; do
    local LN=$(cat -n $TGTFILE | grep $1 | awk '{print $1}')
    local i=0
    for lnum in ${LN[@]}; do
      local lnum=$[lnum-i]
      sed -i "${lnum}d" $TGTFILE
      ((i+=1))
    done
    shift
  done
}