#!/bin/sh
# 25-06-2019 JvW Removed selection logic for m32r/m64r executable, 16.x supports 64 bit only
# 27-05-2018 JvW Add selection logic for m32r/m64r executable selection based on OS version
# 13-05-2018 JvW Add unzip for the DFSee.app native ZIP; removed dfsquery symlink
# 01-08-2017 JvW Split up linux and mac scripts, each their own setup as well
# 27-07-2017 JvW Add dfspalet to show 88/256 colors and dfswget to run wget in an xterm
# 24-07-2017 JvW Add dfshexedit and dfsbrwsimz scripts for dfspuppy start in xterm
# 25-06-2017 JvW Add dfseesmall and dfseelarge scripts for dfspuppy start in xterm
# 21-08-2016 JvW Use filemode 755 to allow execution by anyone (like from /opt)
# 19-08-2016 JvW Made symlink creation optional, and configurable with linkdir parameter
# 14-08-2016 JvW Added dfs startup script using sudo, make symlinks in /usr/local/bin
# 08-05-2010 JvW Set attributes to 744 (root only) on each script spcecifically
# 05-05-2005 JvW Set workable attributes for the standard distribution (7.04)
# 13-11-2004 JvW Install check by Erik P. Olsen (6.16)
# 
# Resolve installation directory, MUST be the current directory, where dfsee lives

echo

if [ "$1" = "-?" ] || [ "$1" = "-h" ]  || [ "$1" = "--help" ] ; then

  echo Usage: $0  [ symlink-directory \| "-" \| none \| "-?" \| "-h" \| "--help" ]
  echo
  echo Setup DFSee scripts to be executable, mode 755, unpack the DFSee.app applet
  echo
  echo Create symbolic links in a specified symlink-directory, when that exists
  echo Default when not specified is '/usr/local/bin'. It should be in the PATH
  echo Specifying any non-existing value/directory, skips the symlink creation 
  echo including creation of a working DFSee.app launcher icon.

else

  if [ -f "dfsm64r" ]; then
    dfs_dir="`pwd`"

    chmod 755 dfsm64r
    chmod 755 dfsmwget
    chmod 755 dfspalet
    chmod 755 dfscheck
    chmod 755 dfsdisk
    chmod 755 dfsedit
    chmod 755 dfsquery
    chmod 755 dfsquick
    chmod 755 dfstart
    chmod 755 dfs
    chmod 755 dfstrun

    echo
    echo Exectable mode 755 set for executable and scripts in the DFSee distribution
    echo

    os_vers_minor=$(sw_vers -productVersion | awk -F '.' '{print $2}')

    echo "Selecting the 64-bit DFSee executable (32-bit not supported anymore in 16.x)"
    dfs_exe="dfsm64r"

    echo Creating a symbolic link 'dfsee' to the selected DFSee exeutable, this needs 
    echo administrative privileges, so you may need to supply the SUDO password:
    echo
    # create generic executable 'dfsee' as symbolic link in install directory (abs paths!)
    sudo ln -s -f -v $dfs_dir/$dfs_exe $dfs_dir/dfsee

    if [ $# -eq 0 ]; then
        linkdir="/usr/local/bin"
    else
        linkdir="$1"
    fi

    if [ -d "$linkdir" ]; then
        echo Symbolic links are needed to run DFSee from any directory, and from DFSee.app
        echo
        echo Creating symbolic links to dfsee scripts in "$linkdir", again, this needs 
        echo administrative privileges, so you may need to supply the SUDO password:
        echo
        sudo ln -s -f -v $dfs_dir/dfsedit  $linkdir/dfsedit
        sudo ln -s -f -v $dfs_dir/dfs      $linkdir/dfs
        echo

        sudo unzip -o DFSee.app.zip
        echo DFSee launcher app native ZIP unpacked to $dfs_dir/DFSee.app
        echo

	echo ===================================================================================
        echo Tight security in later macOS releases like High Sierra may hinder running a simple
        echo program like the DFSee.app from an unidentified developer like Fsys Software is.
        echo 
        echo To allow running it by clicking the DFSee.app icon, from the Dock or your Desktop,
        echo an exception will now be registred by running the DFSee.app once, with APP launch 
        echo security disabled for just a second, and enabling it again automatically.
	echo ===================================================================================
        echo
        echo This needs administrative privileges, so you need to supply the SUDO password.
        echo
        echo When you get the security popup, you need to confirm it by clicking [Open]
        echo and then most likely supply the SUDO password again in the Terminal window.
        echo
        echo After DFSee has started, just use the \<F3\> key several times to close the DFSee 
        echo program, and depending on your preferences set for the Terminal program itself,
        echo you may have to close the terminal window manually.
        echo 
        read -t 9 -n 1 -s -r -p "Press a key to launch the DFSee.app, or wait for the timeout . . ."

        echo
        echo Disable global application security for a second.
        sudo spctl --master-disable
        echo Start DFSee.app to trigger the security exception ...
        echo
        open DFSee.app
        sleep 1
        sudo spctl --master-enable
        echo Global application security in macOS has been restored!

        echo
        echo Using the Finder, you can drag/copy $dfs_dir/DFSee.app to the Dock, 
        echo to your Desktop or even the Applications folder and use that to 
        echo start a new Terminal session with the DFSee program running. 

    else
        echo "$linkdir" is not an existing directory, symlinks are NOT created
        echo and the DFSee.app launch icon will NOT work until they are ...
    fi
    echo
    echo DFSee setup done, you can start DFSee now using the command: dfs
    echo
  else
    echo The DFSee executable file 'dfsm64r' was not found in the current directory!
    echo This setup script MUST be run from the directory where dfsee was installed,
    echo which would be '~/dfsee/mac/' when the standard procedure was followed,
    echo or any other place where you moved/unpacked it.
  fi
fi
echo