{ stdenv
, lib
, fetchzip
, freeglut
, libXmu
, libXi
, libX11
, libICE
, libGLU
, libGL
, libSM
, libXext
, gcc-unwrapped
, glibc
, lua
, glfw
, luabind
, dialog
, openssl
, makeWrapper
, cfgs ? []
}:
#, gcc_debug
#, libgccjit

let
  #hostDir = "/home/magoose/.dotfiles/user/app";
  lpath = lib.makeLibraryPath [ libXmu libXi libX11 freeglut libICE libGLU libGL libSM libXext gcc-unwrapped glibc lua glfw luabind openssl ];
in

stdenv.mkDerivation rec {
  pname = "IceSL";
  version = "2.5.3-beta5";

  src =  if stdenv.hostPlatform.system == "x86_64-linux" then fetchzip {
    url = "https://icesl.loria.fr/assets/other/download.php?build=${version}&version=beta&os=amd64";
    sha256 = "sha256-db4qj7hg0UnrlCnWAijvMWMgncUWPacnip3jdKU6Vl4=";
    extension = "zip";
  } else if stdenv.hostPlatform.system == "i686-linux" then fetchzip {
    url = "https://icesl.loria.fr/assets/other/download.php?build=${version}&version=beta&os=i386";
    sha256 = "";
    extension = "zip";
  } else throw "Unsupported architecture";

  buildInputs = [ makeWrapper ];

  #preInstall = ''
  #'';

  installPhase = ''
    cp -r ./ $out
    #rm $out/bin/*.so #removed this step and copied the libraries located alongside the IceSL binary in /bin
    mkdir $out/oldbin
    mkdir -p $out/icesl-printers/fff/ARES_RED_CUSTOM/{materials,profiles}
    #mkdir -p $out/{icesl-plugins,icesl-printers,icesl-themes}
    touch $out/icesl-printers/fff/ARES_RED_CUSTOM/{end.g,features.lua,printer.lua,start.g,startpre.g,swap.g,swappre.g,wait.g}
    touch $out/icesl-themes/mymonochrome.icss
    #cp -r $out/icesl-plugins $out/cfgs
    #cp -r $out/icesl-plugins $out/cfgs
    #cp -r $out/icesl-printers/fff $out/cfgs/icesl-printers/fff
    #cp -r $out/icesl-themes $out/cfgs
    cp $out/bin/liblua.so $out/oldbin/liblua.so
    cp $out/bin/libluabind.so $out/oldbin/libluabind.so
    mv $out/bin/IceSL-slicer $out/oldbin/IceSL-slicer
    runHook postInstall
  '';

  #added $out/oldbin to rpath to reference liblua.so and libluabind.so

  postInstall = ''
    #cp -r $out/icesl-printers/fff $out/cfgs/icesl-printers/fff
    #cp -r $out/icesl-themes $out/cfgs
    #for cfg in ${toString cfgs}; do
    #  ln -s $cfg/cfgs/* $out/cfgs/
    #  ln -s $cfg/icesl-themes/* $out/icesl-themes
    #done
   patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
      --set-rpath "$out/oldbin:${lpath}" \
      $out/oldbin/IceSL-slicer
    makeWrapper $out/oldbin/IceSL-slicer $out/bin/icesl --prefix PATH : ${dialog}/bin
 '';

  #dontPatchELF = true; #uncomment if IceSL does not find what it needs
  #dontStrip = true; #same as above ^; trying any one of these or the combination

  # user.homeConfigurations.username = { userSettings, config, ... }: {
  # xdg.configFile = {
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-effects".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-effects";
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-infillers".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-infillers";
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-libs".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-libs";
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-models".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-models";
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-plugins".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-plugins";
  #     #"${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-printers".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-printers";
  #     "${config.home.homeDirectory}/.dotfiles/user/app/icesl/result/icesl-themes/mymonochrome.icss".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-themes/mymonochrome.icss";
  #   };
  # };
  #user.homeConfigurations.username = { userSettings, config, ... }: {
  #  home.file.".icesl/icesl-plugins".source = "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-plugins";
  #  home.file.".icesl/icesl-printers".source = "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-printers";
  #  home.file.".icesl/icesl-themes".source = "${config.home.homeDirectory}/.dotfiles/user/app/icesl/config/icesl-themes";
  #};

  meta = with lib; {
    description = "IceSL is a GPU-accelerated procedural modeler and slicer for 3D printing.";
    homepage = http://shapeforge.loria.fr/icesl/index.html;
    license = licenses.inria-icesl;
    sourceProvenance = with sourceTypes; [ binaryNativeCode ];
    platforms = [ "i686-linux" "x86_64-linux" ];
    maintainers = with maintainers; [ mgttlinger magoose];
  };
}
