#!/bin/bash

BIN=$PREFIX/bin
#SIGNING_IDENTITY='ad-team@anaconda.com'
SIGNING_IDENTITY='Apple Development: AD Team'

if [[ $(uname) == Darwin ]]; then
    mv anaconda_navigator/app/Navigator.app $PREFIX/navigatorapp
    sed -i '' 's/0.01/$PKG_VERSION/' $PREFIX/navigatorapp/Contents/Info.plist
    find $PREFIX/navigatorapp -name '__init__.py' | xargs rm

    cp $PREFIX/bin/python $PREFIX/navigatorapp/Contents/MacOS

    AE=$PREFIX/navigatorapp/Contents/MacOS/run.py
    cat <<EOF >$AE
#!${PREFIX}/Anaconda-Navigator.app/Contents/MacOS/python
from subprocess import call

call(["$PREFIX/bin/anaconda-navigator"])
EOF
    chmod +x $AE

    # song and dance for catalina
    cp -r $PREFIX/navigatorapp $PREFIX/Anaconda-Navigator.app
    rm -rf $PREFIX/navigatorapp
    find $PREFIX/Anaconda-Navigator.app/ -name 'python' | xargs codesign --verbose -s 'Apple Development: AD Team' --keychain /Users/builder/Library/Keychains/anaconda-installer.keychain
    #find $PREFIX/Anaconda-Navigator.app/ -name '*\.py*' | xargs codesign --verbose -s 'Apple Development: AD Team' --keychain /Users/builder/Library/Keychains/anaconda-installer.keychain
    codesign -s 'Apple Development: AD Team' --keychain /Users/builder/Library/Keychains/anaconda-installer.keychain $PREFIX/Anaconda-Navigator.app --verbose
    codesign -d $PREFIX/Anaconda-Navigator.app -vvv

    POST_LINK=$BIN/.anaconda-navigator-post-link.sh
    PRE_UNLINK=$BIN/.anaconda-navigator-pre-unlink.sh
    cp $RECIPE_DIR/osx-post.sh $POST_LINK
    cp $RECIPE_DIR/osx-pre.sh $PRE_UNLINK
    chmod +x $POST_LINK $PRE_UNLINK

else # not Darwin
    rm -rf anaconda_navigator/app/Navigator.app
fi

$PYTHON setup.py install --old-and-unmanageable
