#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#
# install and start polymake jupyter interface

# usage: polymake --script jupyter [--force] ...
# using --force allows reinstallation of the interface, skipping the existence check
# all other options are passed on to the 'jupyter notebook' command


sub install_jupymake {
   my $pconf_path = shift;
   # installation of jupyter
   # the $pconf_path is prepended to the $ENV{PATH} variable to make sure it is built
   # with the correct polymake installation
   print <<".";
This function installs the jupyter-polymake kernel
and the python libpolymake interface JuPyMake for the
current user (into ~/.local).
You need to have a running version of the jupyter
notebook for python3 installed.

Are you sure you want to continue with the installation? [y/N] 
.
   local $ENV{PATH}="$pconf_path:".$ENV{PATH};
   chomp ($_=<STDIN>);
   my $extraflags = $^O eq "darwin" ? " --prefix=" : "";
   if (/^[Yy](?:es)?$/) {
      my $dir = new Tempdir;
      my $cmd = <<".";
( cp -RLp $Polymake::Resources/jupyter-polymake $dir && \\
 cp -RLp $Polymake::Resources/JuPyMake $dir && \\
 cd $dir/JuPyMake/ && python3 setup.py install --user $extraflags && \\
 cd $dir/jupyter-polymake/ && python3 setup.py install --user $extraflags ) 2>&1
.
      my $install_jupymake = `$cmd`;
      if ($?==0) {
         print << ".";
Installation complete.
Do you want to start a jupyter notebook now? [y/N]
.
         chomp ($_=<STDIN>);
         if (/^[Yy](?:es)?$/) {
            undef $dir;
            start_jupymake(@_);
         }
      } else {
         die "Installation failed, please check the output:\n$install_jupymake";
      }
   }
}

sub start_jupymake {
   # starts jupyter notebook
   # currently no polymake kernel is running on start up
   exec("jupyter","notebook",@_);
}


my $force_install = 0;
if (@ARGV > 0 && $ARGV[0] eq '--force') {
   $force_install = 1;
   shift @ARGV;
}
my $pconf_path = Cwd::abs_path($0) =~ s/\/polymake$//r;

if (!-x "$pconf_path/polymake-config") {
   die <<"."
This script must be run from an installed polymake
version which was built with the callable library.
.
}

# check if the user has a polymake kernel in jupyter installed,
# if not install it else run 'jupyter notebook'
my $out=`jupyter kernelspec list` ;
if (!$force_install && $out =~ /^\s+polymake/m){
   start_jupymake(@ARGV);
} else {
   install_jupymake($pconf_path,@ARGV);
}
    


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
