#!/usr/bin/env bash
ORG=$(pwd)
[ -r env ] && . ./env

[ -z "$TARGET_PATH" ] && echo "Check env values && exit 1
[ -z "$INSTALL_BIN_PATH" ] && echo "Check env values && exit 1
[ -z "$ARCHIVE_DIR_PATH" ] && echo "Check env values ARCHIVE_DIR_PATH" && exit 1

[ ! -d $TARGET_PATH ] && echo "$TARGET_PATH not found" && exit 1

ARCH=$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')
PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]' | sed -e 's/gnu//g' | sed -e 's,/,,g')

ARCHIVE_PATH="${PLATFORM}-${ARCH}-${APP_NAME}.tar.gz" 
[ -n "$BIN_ARCHIVES_DIR_PATH" ] && [ -d "$BIN_ARCHIVES_DIR_PATH" ] && ARCHIVE_PATH=$BIN_ARCHIVES_DIR_PATH/$ARCHIVE_PATH

cp env $TARGET_PATH
echo "Create pack archive ..."
if tar czf $ARCHIVE_PATH \
   $TARGET_PATH/nu* \
   $TARGET_PATH/install.sh \
   $TARGET_PATH/kcl-install.sh \
   $TARGET_PATH/LICENSE $TARGET_PATH/README \
   $TARGET_PATH/env $TARGET_PATH/$INSTALL_FILE;
then
   echo "$ARCHIVE_PATH generated !!!"
fi
rm $TARGET_PATH/env
