# BSD 3-Clause License

# Copyright (c) 2019, Binder Project
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.

# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

jupyter contrib nbextension install --user
jupyter nbextension install --sys-prefix hide_code
jupyter nbextension enable --sys-prefix hide_code
#jupyter serverextension enable --sys-prefix jupyter_server_proxy #not necessary for export to HTML or PDF
jupyter serverextension enable --sys-prefix hide_code

# Try to fix the install based on https://github.com/kirbs-/hide_code/issues/29#issuecomment-274800019 ; is necessary or despite all the 'install' steps
# above hide_code.js won't be made.
# Note that I used that even though different because old code listed at that link gave error saying
# no install attriibute & only thing with install I saw when I ran `!python -c "import hide_code.hide_code as hc;print(dir(hc));"`
python -c "import hide_code.hide_code as hc;hc.install_bootstrapped_files()" 


# Implement install fix described at https://stackoverflow.com/a/39169844/8508004 to get hide_code
# showing in under `View` > `cell toolbar` menu and across toolbar proper.
# To make general, going to try avoiding hardcoding in the Python version since hoping
# to down the road combine with jupyter-rise and that install produces Python 3.6 at the moment.
# Python version assignment based on https://stackoverflow.com/questions/14735366/cannot-assign-the-output-of-python-v-to-a-variable-in-bash and sending multiple
# lines of text to output based on https://www.unix.com/shell-programming-and-scripting/31827-echo-block-lines.html
# and redirect.
pyver=$(python -c "import sys; print (f'{sys.version_info.major}.{sys.version_info.minor}')")
cd ~/.local/share/jupyter/nbextensions/
cp -r hide_input hide_code
cd hide_code/
cp /srv/conda/envs/notebook/lib/python$pyver/site-packages/notebook/static/custom/hide_code.js ./main.js
rm hide-input.yaml 
cat <<EOF > hide-code.yaml
Type: IPython Notebook Extension
Compatibility: 3.x, 4.x, 5.x
Main: main.js
Name: Hide code
Icon: icon.png
Description: "toggle display of selected code cell's input"
Link: readme.md
EOF
cat <<EOF > readme.md
Hide Code
==========

This extension allows hiding of all or specified, individual code cells in a notebook. See
[here](https://github.com/kirbs-/hide_code#hide_code).
EOF
# since it is now listed in the nbextensions like the others in `contrib nbextension` enable
# it like them, based on python-markdown enabling in https://github.com/binder-examples/jupyter-extension/blob/1fecbd818ef84980ec1043e4b57edebf7053d1f9/postBuild
jupyter nbextension enable hide_code/main

cd ~