#!/usr/bin/env bash
#
# Copyright © Red Gate Software Ltd 2010-2022
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Detect Linux
linux=false
 case "`uname`" in
  Linux*) linux=true;;
 esac

# Dereference softlinks
THIS="$0"
 while [ -h "$THIS" ] ; do
  ls=`ls -ld "$THIS"`
  softlink=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$softlink" : '/.*' > /dev/null; then THIS="$softlink"; else THIS=`dirname "$THIS"`/"$softlink"; fi
 done
# Detect the installation directory
INSTALLDIR=`dirname "$THIS"`

if [ -x "$INSTALLDIR/jre/bin/java" ]; then
 JAVA_CMD=$INSTALLDIR/jre/bin/java
else
 # Use JAVA_HOME if it is set
 if [ -z "$JAVA_HOME" ]; then JAVA_CMD=java; else JAVA_CMD=$JAVA_HOME/bin/java; fi
fi

# Determine Flyway edition to use
POSITIONAL=()
while [[ $# -gt 0 ]]; do
  POSITIONAL+=("$1")
  flag="$1"
  case $flag in
    -community)
    FLYWAY_EDITION=community
    ;;
    -pro)
    FLYWAY_EDITION=enterprise
    ;;
    -enterprise)
    FLYWAY_EDITION=enterprise
    ;;
    -teams)
    FLYWAY_EDITION=enterprise
    ;;
  esac
  shift
done
set -- "${POSITIONAL[@]}"
if [ -z "$FLYWAY_EDITION" ]; then
  CP="$CLASSPATH:$INSTALLDIR/lib/*:$INSTALLDIR/lib/aad/*:$INSTALLDIR/lib/oracle_wallet/*:$INSTALLDIR/lib/enterprise/*:$INSTALLDIR/drivers/*:$INSTALLDIR/drivers/gcp/*"
  "$JAVA_CMD" $JAVA_ARGS $EXTRA_ARGS -cp "$CP" org.flywaydb.commandline.Main "$@" -checkLicence -n >/dev/null 2>/dev/null
  ERROR_LEVEL=$?
  if [ $ERROR_LEVEL -eq 101 ]; then
    echo "----------------------------------------"
    echo "Your Flyway Teams License has expired; falling back to Community Edition. To force Teams Edition, please specify '-teams'. Please contact sales at sales@flywaydb.org to renew your license."
    echo "----------------------------------------"
  fi
  if [ $ERROR_LEVEL -eq 102 ]; then
    echo "----------------------------------------"
    echo "Your Flyway Teams Trial License has expired; falling back to Community Edition. Please contact sales at sales@flywaydb.org to purchase a licence, extend your Flyway Teams trial license via https://flywaydb.org/download/teams?ref=expiredTrial or specify -community to downgrade and stop this message appearing."
    echo "----------------------------------------"
  fi
  if [ $ERROR_LEVEL -eq 103 ]; then
    echo "----------------------------------------"
    echo "A Flyway Teams License was found but is unreadable; falling back to Community Edition. Please contact sales at sales@flywaydb.org to purchase a valid license, request a Flyway Teams trial license via https://flywaydb.org/download/teams?ref=invalidLicense or remove the 'licenseKey' configuration parameter to stop this message appearing."
    echo "----------------------------------------"
  fi
  if [ $ERROR_LEVEL -eq 0 ]; then
    FLYWAY_EDITION=enterprise
  else
    FLYWAY_EDITION=community
  fi
fi
if [ "$FLYWAY_EDITION" == "pro" ] || [ "$FLYWAY_EDITION" == "teams" ]; then
  FLYWAY_EDITION=enterprise
fi

if ! [[ "$FLYWAY_EDITION" =~ ^(community|pro|enterprise|teams|\[edition\])$ ]]; then
    echo invalid edition \"$FLYWAY_EDITION\"
    exit 1
fi

CP="$CLASSPATH:$INSTALLDIR/lib/*:$INSTALLDIR/lib/aad/*:$INSTALLDIR/lib/oracle_wallet/*:$INSTALLDIR/lib/$FLYWAY_EDITION/*:$INSTALLDIR/drivers/*:$INSTALLDIR/drivers/gcp/*"

EXTRA_ARGS=
if $linux; then
  EXTRA_ARGS=-Djava.security.egd=file:/dev/../dev/urandom
fi

if `command -v cygpath > /dev/null`; then CP=`cygpath -pw "$CP"`; fi
"$JAVA_CMD" $JAVA_ARGS $EXTRA_ARGS -cp "$CP" org.flywaydb.commandline.Main "$@"

# Exit using the same code returned from Java
exit $?