{ stdenv, requireFile, SDL2, makeWrapper,
  mesa_glu,
  unzip, ... }:

let
  originalArchive = "gog_starbound_2.13.0.14.sh";
in
  stdenv.mkDerivation rec {
    name = "starbound-${version}";
    version = "2.13.0.14";

    src = ../../../nixrc-src-games/gog_starbound_2.13.0.14.sh;

    phases = "unpackPhase installPhase";

    unpackCmd = "${unzip}/bin/unzip -qq $src || true";

    sourceRoot = "data";

    libPath = stdenv.lib.makeLibraryPath [
      mesa_glu
      stdenv.cc.cc.lib
      SDL2
    ];

    buildInputs = [
      makeWrapper
    ];

    installPhase = ''
      mkdir -p $out
      mv noarch/game/* $out/
      mkdir -p $out/bin

      chmod +x $out/linux/starbound

      cat > $out/linux/sbinit.config << EOF
      {
        "assetDirectories" : [
          "../assets/",
          "../mods/"
        ],

        "storageDirectory" : "/home/sven/.starbound-storage/"
      }
      EOF

      patchelf \
        --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \
        $out/linux/starbound

      for lib in linux/*.so{,.*}; do
        patchelf --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) $lib
      done

      makeWrapper strace $out/bin/starbound-strace                             \
        --add-flags -f                                                         \
        --add-flags $out/linux/starbound                                       \
        --prefix LD_LIBRARY_PATH : /run/opengl-driver/lib:$libPath:$out/linux

      makeWrapper $out/linux/starbound $out/bin/starbound                      \
        --prefix LD_LIBRARY_PATH : /run/opengl-driver/lib:$libPath:$out/linux
    '';
  }
