#!/bin/bash

if test "x$1" = "x-v" || test "x$1" = "x--version" ; then
  echo "Freeciv build script for Linux Manjaro version 1.06"
  exit
fi

if test "x$1" = "x" || test "x$2" = "x" ||
   test "x$1" = "x-h" || test "x$1" = "x--help" ; then
  echo "Usage: $0 <release> <gui> [main dir=freeciv-genbuild] [download URL]"
  echo "Supported releases are those of 2.6, and 3.0 major versions"  
  echo "Supported guis are 'gtk2', 'gtk3.22', 'gtk3', 'qt'"
  echo "URL must point either to tar.bz2 or tar.xz package"
  exit
fi

REL=$1
GUI=$2

if test "x$3" = "x" ; then
  MAINDIR="freeciv-genbuild"
else
  MAINDIR="$3"
fi

FREECIV_MAJMIN=$(echo $REL | sed 's/\./ /g' | (read MAJOR MINOR REST ; echo -n "$MAJOR.$MINOR"))
FREECIV_PATCH=$(echo $REL | sed 's/\./ /g' | (read MAJOR MINOR PATCH REST ; echo -n "$PATCH"))

if test "x$FREECIV_MAJMIN" != "x2.6" &&
   test "x$FREECIV_MAJMIN" != "x3.0" ; then
  echo "Release '$REL' from unsupported branch. See '$0 --help' for supported options" >&2
  exit 1
fi

if test "x$FREECIV_PATCH" = "x" || test $FREECIV_PATCH -gt 89 ; then
  if test "x$FREECIV_MAJMIN" = "x3.0" ; then
    EXTRA_CONFIG="--with-qt6 $EXTRA_CONFIG"
  else
    echo "This development snapshots with patch level >= 90 not supported" >&2
    exit 1
  fi
fi

if test "x$GUI" != "xgtk3.22" &&
   test "x$GUI" != "xgtk3" &&
   test "x$GUI" != "xgtk2" &&
   test "x$GUI" != "xqt" ; then
  echo "Unsupported gui '$GUI' given. See '$0 --help' for supported options" >&2
  exit 1
fi

if test $FREECIV_PATCH -gt 89 &&
   test "x$FREECIV_MAJMIN" = "x3.0" && test "x$GUI" = "xgtk2" ; then
  echo "gtk2 is not supported gui for freeciv-3.1" >&2
  exit 1
fi

echo "Install requirements (y/n)?"
echo "You need them, so answer 'n' only if you have them already"
echo "in place."
while test "$req_install" != "n" && test "$req_install" != "y"
do
  echo -n "> "
  read -n 1 req_install
  echo
  if test "$req_install" != "n" && test "$req_install" != "y" ; then
    echo "Please answer 'y' or 'n'"
  fi
done

if test "$req_install" != "n" ; then
echo "Installing requirements"
sudo pacman -Su --needed \
  gcc make sdl2_mixer pkg-config
fi

if test -d "$MAINDIR" ; then
  echo "There's already directory called '$MAINDIR'. Should I use it?"
  echo "y)es or no?"
  echo -n "> "
  read -n 1 ANSWER
  if test "x$ANSWER" != "xy" ; then
    echo "Didn't get definite yes for using existing directory. Aborting"
    exit 1
  fi
  echo
fi

if ! mkdir -p "$MAINDIR" ; then
  echo "Failed to create directory '$MAINDIR'" >&2
  exit 1
fi

if ! cd "$MAINDIR" ; then
  echo "Can't go to '$MAINDIR' directory" >&2
  exit 1
fi

export FREECIV_MAINDIR=$(pwd)

if ! test -f freeciv-$REL.tar.bz2 && ! test -f freeciv-$REL.tar.xz ; then
  echo "Downloading freeciv-$REL"
  if test "x$4" = "x" ; then
    if test "x$FREECIV_MAJMIN" = "x3.0" ; then
      URL="http://sourceforge.net/projects/freeciv/files/Freeciv $FREECIV_MAJMIN/$REL/freeciv-$REL.tar.xz"
    else
      URL="http://sourceforge.net/projects/freeciv/files/Freeciv $FREECIV_MAJMIN/$REL/freeciv-$REL.tar.bz2"
    fi
  else
    URL="$4"
  fi
  if ! wget "$URL" ; then
    echo "Can't download freeciv release freeciv-$REL." >&2
    exit 1
  fi
else
  echo "freeciv-$REL already downloaded"
fi

if ! test -d freeciv-$REL ; then
  echo "Unpacking freeciv-$REL"
  if test -f freeciv-$REL.tar.xz ; then
    if ! tar xJf freeciv-$REL.tar.xz ; then
      echo "Failed to unpack freeciv-$REL.tar.xz" >&2
      exit 1
    fi
  elif ! tar xjf freeciv-$REL.tar.bz2 ; then
    echo "Failed to unpack freeciv-$REL.tar.bz2" >&2
    exit 1
  fi
else
  echo "freeciv-$REL source directory already exist"
fi

if ! cd freeciv-$REL ; then
  echo "Failed to go to source directory freeciv-$REL" >&2
  exit 1
fi

if ! cd $FREECIV_MAINDIR ; then
  echo "Failed to return to main directory '$FREECIV_MAINDIR'" >&2
  exit 1
fi

if ! mkdir -p builds-$REL/$GUI ; then
  echo "Failed to create build directory 'builds-$REL/$GUI'" >&2
  exit 1
fi

if test -f install-$REL/$GUI ; then
  echo "Removing old $REL $GUI installation directory"
  if ! rm -Rf install-$REL/$GUI ; then
    echo "Failed to remove old installation directory" >&2
    exit 1
  fi
fi

if ! cd builds-$REL/$GUI ; then
  echo "Failed to go to directory builds-$REL/$GUI" >&2
  exit 1
fi

if test "x$GUI" = "xgtk2" ; then
  FCMP="gtk2"
elif test "x$GUI" = "xqt" ; then
  FCMP="qt"
else
  FCMP="gtk3"
fi

echo "configure"
if ! ../../freeciv-$REL/configure --prefix=$FREECIV_MAINDIR/install-$REL/$GUI --enable-client=$GUI --enable-fcmp=$FCMP $EXTRA_CONFIG ; then
  echo "Configure failed" >&2
  exit 1
fi

echo "make"
if ! make ; then
  echo "Make failed" >&2
  exit 1
fi

echo "make install"
if ! make install ; then
  echo "'Make install' failed" >&2
  exit 1
fi

echo
echo "freeciv-$REL $GUI installation is now at $FREECIV_MAINDIR/install-$REL/$GUI"
echo "The programs available there are:"
ls -1 "$FREECIV_MAINDIR/install-$REL/$GUI/bin" | sed "s|^|$FREECIV_MAINDIR/install-$REL/$GUI/bin/|"
