#!/bin/bash
# set -x

printf "\nThis is the old mkjs9app script. Please run the script in js9app/build,\nwhich utilizes electron-forge. See: https://github.com/ericmandel/js9app.\n\n"
exit 1

PLATFORM=`uname -s`
PKG="package.json"
CDIR=`pwd`
DORENAME=true
DOICON=false

error() {
    echo "ERROR: $1"
    exit 1
}

case $PLATFORM in
    Darwin)
    EXE="Electron.app"
    # generate a local, clean app directory
    echo "generate local copy of $EXE ..."
    SBASE="$HOME/Applications"
    SDIR="$SBASE/$EXE"
    if [ ! -d $SDIR ]; then
        SBASE="/Applications"
        SDIR="$SBASE/$EXE"
        if [ ! -d $SDIR ]; then
          error "can't find Electron directory: $SDIR"
        fi
    fi
    DBASE="`pwd`/.."
    DDIR="$DBASE/$EXE"
    if [ -d $DDIR ]; then
      rm -rf $DDIR
    fi
    rm -f "$DBASE/js9.app.zip"
    cd $SBASE  && tar cf - $EXE | (cd $DBASE && tar xf -)
    # install js9 into Electron.app
    echo "install JS9 files ..."
    RESDIR="$DDIR/Contents/Resources"
    APPDIR="$DDIR/Contents/Resources/app"
    BINDIR="$DDIR/Contents/Resources/app/bin"
    mkdir -p $BINDIR
    cd $CDIR 
    cp -p images/js9logo/mac/js9logo.icns $RESDIR/js9.icns
    make WEBDIR="$APPDIR" install-webdirs install-webfiles
    make BIN_INSTALL_DIR="$BINDIR" install-scripts
    sed  's/"main":.*/"main": "js9Electron.js",/' < $PKG > $APPDIR/$PKG
    sed '3a\
test -d /Applications/js9.app/Contents/Resources/app && APPDIR=/Applications/js9.app/Contents/Resources/app; test -d $HOME/Applications/js9.app/Contents/Resources/app && APPDIR=$HOME/Applications/js9.app/Contents/Resources/app;

' < $BINDIR/js9 | sed 's#"/Users/eric/.*"#"$APPDIR"#g' > $BINDIR/njs9
    mv $BINDIR/njs9 $BINDIR/js9 && chmod +x  $BINDIR/js9
    # rename application
    if [ $DORENAME = true ]; then
        JEXE="js9.app"
	echo "rename app from $EXE to $JEXE ..."
        # rename the app contents to js9
        cd $DDIR || error "can't cd to dest Electron app directory"
	# https://electronjs.org/docs/tutorial/application-distribution
        PFILE="./Contents/Info.plist"
        sed 's/[eE]lectron/js9/g' < "$PFILE" > "$PFILE"-new
        mv "$PFILE"{,-orig} && mv "$PFILE"{-new,}
	cp -p $CDIR/js9AppWrapper.sh ./Contents/MacOS/js9
	chmod +x ./Contents/MacOS/js9
        PFILE="./Contents/Frameworks/Electron Helper.app/Contents/Info.plist"
        sed 's/[eE]lectron/js9/g' < "$PFILE" > "$PFILE"-new
        mv "$PFILE"{,-orig} && mv "$PFILE"{-new,}
        # rename the app to js9
        cd $DDIR/..
        if [ -d $JEXE ]; then
          rm -rf $JEXE
        fi
        mv $EXE $JEXE
        # codesign
	echo "code sign $JEXE ..."
        sudo codesign --force --deep --sign - $JEXE/
    else
	echo "code sign $EXE ..."
        sudo codesign --force --deep --sign - $EXE/
    fi
    # change the icon manually, if necessary
    if [ $DOICON = true ]; then
      open $CDIR/images/js9logo/mac/js9logo.icns
      echo "copy the Preview image using Cmd-A Cmd-C"
      echo "then Cmd-I the new js9.app, click the icon in the UL, and Cmd-V"
      echo " "
    fi
    echo "Generate zip file using 'Compress' option in Finder"
    echo 'Then: zip -d js9.app.zip "__MACOSX/*" to clean up'
    ;;

    Linux|Microsoft)
    # we use Microsoft's Windows Subsystem for Linux to create the Win10 app
    # (currently, there is no Linux app)
    uname -a | egrep Microsoft >/dev/null 2>&1
    if [ $? != 0 ]; then
      error "mkjs9app on Linux platform currently targets Windows 10 only"
    fi
    EXE="electron"
    # generate a local, clean app directory
    echo "generate local copy of $EXE ..."
    SBASE="$HOME"
    SDIR="$SBASE/$EXE"
    if [ ! -d $SDIR ]; then
        error "can't find electron directory: $SDIR"
    fi
    DBASE="`pwd`/.."
    DDIR="$DBASE/$EXE"
    if [ -d $DDIR ]; then
      rm -rf $DDIR
    fi
    cd $SBASE  && tar cf - $EXE | (cd $DBASE && tar xf -)
    # install js9 into Electron.app
    echo "install JS9 files ..."
    RESDIR="$DDIR/resources"
    APPDIR="$DDIR/resources/app"
    BINDIR="$DDIR/resources/app/bin"
    mkdir -p $BINDIR
    cd $CDIR
    cp -p images/js9logo/mac/js9logo.icns $RESDIR/js9.icns
    make WEBDIR="$APPDIR" install-webdirs install-webfiles
    make BIN_INSTALL_DIR="$BINDIR" install-scripts
    sed  's/"main":.*/"main": "js9Electron.js",/' < $PKG > $APPDIR/$PKG
    # install wget executable
    if [ -d "/mnt/c/Program Files (x86)/GnuWin32/bin" ]; then
      GNUDIR="$DDIR/resources/app/GnuWin32/bin"
      mkdir -p $GNUDIR
      echo "install wget.exe ..."
      cp -p "/mnt/c/Program Files (x86)/GnuWin32/bin"/* $GNUDIR/.
    fi
    # rename application
    if [ $DORENAME = true ]; then
        JEXE="js9.app"
	echo "rename app from $EXE to $JEXE ..."
        # rename the app contents to js9
        cd $DDIR || error "can't cd to dest Electron app directory"
	# https://electronjs.org/docs/tutorial/application-distribution
	mv electron.exe js9app.exe
        # rename the app to js9
        cd $DDIR/..
        if [ -d $JEXE ]; then
          rm -rf $JEXE
        fi
        mv $EXE $JEXE
	echo "create zip file for $JEXE ..."
	zip -r js9.app.zip $JEXE
    fi
    ;;

    *)
    error "support for $PLATFORM not yet implemented"
    exit
    ;;
esac
