#!/bin/bash

CUR_DIR=$( cd $( dirname $0 ) && pwd )
VERSION=$(grep -E '^version' ${CUR_DIR}/../Cargo.toml | awk '{print $3}' | sed 's/"//g')

## Disable macos ACL file
if [[ "$(uname -s)" == "Darwin" ]]; then
    export COPYFILE_DISABLE=1
fi

targets=()

while getopts "t:" opt; do
    case $opt in
        t)
            targets+=($OPTARG)
            ;;
        ?)
            echo "Usage: $(basename $0) [-t <target-triple>]"
            ;;
    esac
done

if [[ "${#targets[@]}" == "0" ]]; then
    targets=(
        "x86_64-unknown-linux-musl"
        "x86_64-unknown-linux-gnu"

        "x86_64-pc-windows-gnu"

        "arm-unknown-linux-gnueabihf" # armhf, hard
        "arm-unknown-linux-musleabi"  # arm, soft
        "aarch64-unknown-linux-gnu"

        "mips-unknown-linux-musl"     # big endian
        "mipsel-unknown-linux-musl"   # little endian
    )
fi

[[ -z $upx ]] && upx="echo pending"
if [[ $upx == "echo pending" ]] && hash upx 2>/dev/null; then
	upx="upx -9"
fi

function build() {
    cd "$CUR_DIR/.."

    TARGET=$1
    echo "* Building ${TARGET} package ${VERSION} ..."

    RELEASE_DIR="target/${TARGET}/release"

    if [[ "$TARGET" == *"-linux-"* || "$TARGET" == *"-darwin" ]]; then
        EXTRA_FEATURES+=" local-redir"
    fi

    cross build --target "${TARGET}" \
                --features "${EXTRA_FEATURES}" \
                --release

    if [[ $? != "0" ]]; then
        exit $?
    fi

    PKG_DIR="${CUR_DIR}/release"
    mkdir -p "${PKG_DIR}"

    if [[ "$TARGET" == *"-linux-"* ]]; then
        PKG_NAME="shadowsocks-v${VERSION}.${TARGET}.tar.xz"
        PKG_PATH="${PKG_DIR}/${PKG_NAME}"

        cd ${RELEASE_DIR}

        if [[ "$TARGET" == "mips"* ]]; then
            # Enable upx for MIPS.
            $upx sslocal ssserver ssurl ssmanager #>/dev/null
        fi

        echo "* Packaging XZ in ${PKG_PATH} ..."
        tar -cJf ${PKG_PATH} \
            "sslocal" \
            "ssserver" \
            "ssurl" \
            "ssmanager"

        if [[ $? != "0" ]]; then
            exit $?
        fi

        cd "${PKG_DIR}"
        shasum -a 256 "${PKG_NAME}" > "${PKG_NAME}.sha256"
    elif [[ "$TARGET" == *"-windows-"* ]]; then
        PKG_NAME="shadowsocks-v${VERSION}.${TARGET}.zip"
        PKG_PATH="${PKG_DIR}/${PKG_NAME}"

        echo "* Packaging ZIP in ${PKG_PATH} ..."
        cd ${RELEASE_DIR}
        zip ${PKG_PATH} \
            "sslocal.exe" \
            "ssserver.exe" \
            "ssurl.exe" \
            "ssmanager.exe"

        if [[ $? != "0" ]]; then
            exit $?
        fi

        cd "${PKG_DIR}"
        shasum -a 256 "${PKG_NAME}" > "${PKG_NAME}.sha256"
    fi

    echo "* Done build package ${PKG_NAME}"
}

for target in "${targets[@]}"; do
    build "$target";
done
