#!/bin/bash

#set -x
set -e

#=============================================
# JREs to use
URL_JRE_FOLDER="http://downloads.gvsig.org/download/gvsig-desktop-testing/pool/jre"

GVSPKG_JRE_darwin_x86_64="gvSIG-desktop-2.3.0-jre-1.8.0-72-testing-darwin_macos_10.11-x86_64-j1_8.gvspkg"
GVSPKG_JRE_lin_x86_64="gvSIG-desktop-2.6.0-jre-1.8.0-311-testing-lin-x86_64-j1_8.gvspkg"
GVSPKG_JRE_lin_x86="gvSIG-desktop-2.3.0-jre-1.8.0-92-testing-lin-x86-j1_8.gvspkg"
GVSPKG_JRE_win_x86_64="gvSIG-desktop-2.6.0-jre-1.8.0-311-testing-win-x86_64-j1_8.gvspkg"
GVSPKG_JRE_win_x86="gvSIG-desktop-2.3.0-jre-1.8.0-73-testing-win-x86-j1_8.gvspkg"

#=============================================
# self extractors to use
URL_SELFEXTRACT_FOLDER="http://downloads.gvsig.org/download/gvsig-desktop/runtimes/selfextract"

#=============================================
# Where to find izpack installers
URL_DISTS="http://downloads.gvsig.org/download/gvsig-desktop-testing/dists"



#=============================================
# for wget >= 1.16
WGET_SHOW_PROGRESSBAR="--show-progress"
WGET_SHOW_PROGRESSBAR=""

mkexec_unx() {
  OS=$1
  ARCH=$2
  INSTALL_JAR="$3"
  WORKINGFOLDER="$4"
  OSFamily="${OS/_*/}"

  INSTALL_JAR_BASENAME=$(basename "$INSTALL_JAR")
  GVSPKG_JRE="GVSPKG_JRE_${OSFamily}_${ARCH}"
  GVSPKG_JRE="${!GVSPKG_JRE}"
  SELF_EXTRACT="selfextract-${OSFamily}-${ARCH}"
  DATA_FOLDER="$WORKINGFOLDER/data-${OS}-$ARCH"
  DATA_ARCHIVE="$WORKINGFOLDER/data-${OS}-$ARCH.tar"

  cd "$WORKINGFOLDER"

  echo "Making selfextract for ${OS} ${ARCH} using:"
  echo "  selfextract: ${SELF_EXTRACT}"
  echo "  jre: ${GVSPKG_JRE}"
  echo "  java installer: ${INSTALL_JAR_BASENAME}"
  echo "  working folder: ${WORKINGFOLDER}"


  echo "Remove previous temporary files"
  rm -f "$DATA_ARCHIVE"
  rm -rf "$DATA_FOLDER"

  if [ ! -f ${SELF_EXTRACT} ] ; then
    echo "Downloading selfextract program ${SELF_EXTRACT}"
    wget  -q $WGET_SHOW_PROGRESSBAR "$URL_SELFEXTRACT_FOLDER/${OSFamily}-$ARCH/${SELF_EXTRACT}"
    chmod a+x ${SELF_EXTRACT}
  fi

  if [ ! -f "${GVSPKG_JRE}" ] ; then
    echo "Downloading JRE $URL_JRE_FOLDER/${GVSPKG_JRE}"
    wget  -q $WGET_SHOW_PROGRESSBAR "$URL_JRE_FOLDER/${GVSPKG_JRE}"
  fi

  echo "Creating temp folder"
  mkdir -p "$DATA_FOLDER"
  cd "$DATA_FOLDER"

  # -------------------------------------------------------------
  # Preparamos en $DATA_FOLDER los ficheros que vamos a meter en el tar
  # que queremos que sea autoextraible y autoejecutable.
  # - La carpeta jre
  # - El instalador del izpack
  # - El autorun.sh
  #
  echo "Extracting JRE"
  unzip -q  "../${GVSPKG_JRE}" 2>&1 | sed  '/warning:  stripped absolute path spec/ d'
  chmod a+x "jre/bin/java"

  echo "Coping $INSTALL_JAR"
  cp "$INSTALL_JAR" .

  echo "#!/bin/sh
exec ./jre/bin/java -jar \"$INSTALL_JAR_BASENAME\"
" > autorun.sh
  chmod a+x "autorun.sh"

  # -------------------------------------------------------------
  # Creamos el tar
  echo "Creating archive file (tar)"
  tar cf "$DATA_ARCHIVE" .

  # -------------------------------------------------------------
  # Creamos con el fichero tar generado el autoextraible
  echo "Making selfextract with archive file"
  {
    cat ${WORKINGFOLDER}/${SELF_EXTRACT}
    echo ";!@selfextract@!"
    cat "$DATA_ARCHIVE"
  } > "${INSTALL_JAR%.jar}.run"
  echo "Created ${INSTALL_JAR%.jar}.run"
  echo ""
}


mkexec_win() {
  OSFamily="win"
  OS="$1"
  ARCH=$2
  INSTALL_JAR="$3"
  WORKINGFOLDER="$4"

  INSTALL_JAR_BASENAME=$(basename "$INSTALL_JAR")
  GVSPKG_JRE="GVSPKG_JRE_${OSFamily}_${ARCH}"
  GVSPKG_JRE="${!GVSPKG_JRE}"
  SELF_EXTRACT="7zSD-${OSFamily}-${ARCH}.sfx"
  DATA_FOLDER="$WORKINGFOLDER/data-${OSFamily}-$ARCH"
  DATA_ARCHIVE="$WORKINGFOLDER/data-${OSFamily}-$ARCH.7z"

  cd "$WORKINGFOLDER"

  echo "Making selfextract for ${OSFamily} ${ARCH} using:"
  echo "  selfextract: ${SELF_EXTRACT}"
  echo "  jre: ${GVSPKG_JRE}"
  echo "  java installer: ${INSTALL_JAR_BASENAME}"
  echo "  working folder: ${WORKINGFOLDER}"

  echo "Remove previous temporary files"
  rm -f "$DATA_ARCHIVE"
  rm -rf "$DATA_FOLDER"

  if [ ! -f ${SELF_EXTRACT} ] ; then
    echo "Downloading selfextract program"
    wget  -q $WGET_SHOW_PROGRESSBAR "$URL_SELFEXTRACT_FOLDER/${OSFamily}-$ARCH/$SELF_EXTRACT"
  fi

  if [ ! -f "${GVSPKG_JRE}" ] ; then
    echo "Downloading JRE $URL_JRE_FOLDER/${GVSPKG_JRE}"
    wget  -q $WGET_SHOW_PROGRESSBAR  "$URL_JRE_FOLDER/${GVSPKG_JRE}"
  fi

  echo "Creating temp folder"
  mkdir -p "$DATA_FOLDER"
  cd "$DATA_FOLDER"


  # -------------------------------------------------------------
  # Preparamos en $DATA_FOLDER los ficheros que vamos a meter en el 7zip
  # que queremos que sea autoextraible y autoejecutable.
  # - La carpeta jre
  # - El instalador del izpack
  #
  echo "Extracting JRE"
  unzip -q  "../${GVSPKG_JRE}" 2>&1 | sed  '/warning:  stripped absolute path spec/ d'

  echo "Copying $INSTALL_JAR"
  cp "$INSTALL_JAR" .

  # -------------------------------------------------------------
  # Creamos el 7zip
  echo "Creating archive file (7zip)"
  7z a "$DATA_ARCHIVE" . >/dev/null

  # -------------------------------------------------------------
  # Creamos con el fichero 7zip generado el autoextraible
  echo "Making selfextract with archive file"

    SFX_CONFIG=";!@Install@!UTF-8!
RunProgram=\".\\jre\\bin\\javaw.exe -jar .\\${INSTALL_JAR_BASENAME}\"
GUIMode=\"1\"
ExtractTitle=\"gvSIG Desktop installation.\"
Title=\"gvSIG Desktop installation.\"
GUIFlags=\"2+4\"
;!@InstallEnd@!"

    echo "SFX config
==========
${SFX_CONFIG}
=========="

  {
    cat ${WORKINGFOLDER}/${SELF_EXTRACT}
    echo "${SFX_CONFIG}"
    cat "$DATA_ARCHIVE"
  } > "${INSTALL_JAR%.jar}.exe"

  echo "Created ${INSTALL_JAR%.jar}.exe"
  echo ""
}


mkexec() {
  OS="$1"
  ARCH="$2"
  INSTALL_JAR="$3"
  WORKINGFOLDER="$4"
  OSFamily="${OS/_*/}"
  mkdir -p "$WORKINGFOLDER"
  if [ "${OSFamily}" == "lin" ] ; then
    mkexec_unx "$OS" "$ARCH" "$INSTALL_JAR" "$WORKINGFOLDER"
  elif [ "${OSFamily}" == "darwin" ] ; then
    mkexec_unx "$OS" "$ARCH" "$INSTALL_JAR" "$WORKINGFOLDER"
  else
    mkexec_win "$OS" "$ARCH" "$INSTALL_JAR" "$WORKINGFOLDER"
  fi

}

main() {
  if [ "$#" != "5" ] ; then
    echo "$0: Usage:
  $0 gvsig-version gvsig-buildnumber gvsig-state gvsig-distribution folder" 2>&1
    exit 1
  fi
  GVSIG_VERSION="$1"
  GVSIG_BUILDNUMBER="$2"
  GVSIG_STATE="$3"
  GVSIG_DISTRIBUTION="$4"
  ROOTFOLDER="$5"

  echo "
Making selfextract:
  root folder=$ROOTFOLDER
"
  mkdir -p "$ROOTFOLDER"

  for os_arch in "lin x86_64" "win x86_64" "lin x86" "win x86" "darwin_macos_10.11 x86_64" "lin_ubuntu_14.04 x86" "lin_ubuntu_14.04 x86_64"
  do
    cd "$ROOTFOLDER"
    OS="${os_arch/ */}"
    ARCH="${os_arch/* /}"
    INSTALL_JAR_BASENAME="gvSIG-desktop-${GVSIG_VERSION}-${GVSIG_BUILDNUMBER}-${GVSIG_STATE}-${OS}-${ARCH}-${GVSIG_DISTRIBUTION}.jar"
    INSTALL_JAR="$ROOTFOLDER/$INSTALL_JAR_BASENAME"
    if [ ! -f "${INSTALL_JAR}" ] ; then
      echo "Downloading installer ${INSTALL_JAR_BASENAME}"
      wget  -q $WGET_SHOW_PROGRESSBAR "$URL_DISTS/${GVSIG_VERSION}/builds/${GVSIG_BUILDNUMBER}/${INSTALL_JAR_BASENAME}"
    fi
    mkexec "$OS" "$ARCH" "$INSTALL_JAR" "$ROOTFOLDER/mkexec.tmp"
  done
  echo "removing temp folder $ROOTFOLDER/mkexec.tmp"
  rm -rf "$ROOTFOLDER/mkexec.tmp"
}

main $@

