#!/bin/sh
#-------------------------------------------------------------------------------
# This file is part of the WSJT application
#
# File Name:    wsjt
# Description:  Shell script wrapper to run WSJT
# 
# Copyright (C) 2001-2016 Joseph Taylor, K1JT
# License: GNU GPL v3
#
# 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 3 of the License, or (at your option) any later
# version.
#
# 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.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#-------------------------------------------------------------------------------

# Problem:
# With the current directory installation, we can run WSJT without copying
# all the files to the users $RUNDIR however, WSJT-X and WSJT conflict one
# another with jt65code and jt4code in the /bin directory and manpages.
# To get around this problem, the runtime script creates it's own bin, lib,
# share directories, then exports those paths. See START MAIN SCRIPT.
#
# Run Multiple Instances:
# Using the -n [NAME] option at atart up allows the user to create / use
# multiple instances of WSJT. Using this option creates seperate directories
# for each name, example: [ wsjt -n wsjt-vhf ]  and [ wsjt -n wsjt-uhf ] would
# create the following:
#
# ~/.local/share/wsjt-vhf
# ~/.local.share/wsjt-uhf
# 
# Both folders are completely isolated, WSJT.INI, CALL3.TXT, logs, decodes,
# user ID, etc, basically anything you can configure will be separated.

# exit on errors
set -e

# foreground colours
C_R='\033[01;31m'		# red
C_G='\033[01;32m'		# green
C_Y='\033[01;33m'		# yellow
C_C='\033[01;36m'		# cyan
C_NC='\033[01;37m'		# no color

# local user directories for wsjt.desktop and icon files
APPD="$HOME/.local/share/applications"
ICOND="$HOME/.local/share/icons"

# file array
file_array=('CALL3.TXT' 'wsjtrc')

################################################################################
# FUNCTIONS
################################################################################
cli_help() {
clear ||:
echo '--------------------------------------------'
echo -e ${C_G}" COMMAND LINE OPTIONS"${C_NC}
echo '--------------------------------------------'
echo ''
cat <<EOF
 To run multiple instances of WSJT, use
 the [-n] operator. To designate a custom
 runtime location use the [-p] operator.

 Usage ....: wsjt [-h] [-n] [-p]
 Example ..: wsjt -n wsjt1 -p $HOME/Desktop

 OPTIONS:
	-h	displays this help message

	-n	Sets the runtime folder name [ -n NAME ] 
		Default folder name is ..: WSJT

	-p	Sets the runtime folder path [ -p PATH ]
		Defualt is ..: $HOME/.local/share

 NOTES
  1. You must setup each instance separately.
     See Setup >> Options from the WSJT main application menu.
 
  2. All decoded data, CALL3.TXT, logs etc are separated by [-n NAME].

EOF

}


################################################################################
# PROCESS COMMAND-LINE OPTIONS
################################################################################
while getopts "hn:p:" opt; do
	case $opt in
		# dispalay the help message
		h) cli_help ; exit 0 ;;
		
		# add user defined name		
		n)	N_OPT=$OPTARG ;;
		
		# add user defined path
		p) P_OPT=$OPTARG ;;
		'?')
			echo "Invalid Option" 1>&2
			read -rp " Press [ Enter ] to view the help message"
			cli_help
			exit 1
		;;
	esac
done
shift $((OPTIND -1))

# set folder name if -n [NAME] is used
if [ ! -z $N_OPT ] ; then
	N_NAME="$N_OPT"
fi

# set default folder name if -n [NAME] is not used
if [ -z $N_OPT ] ; then
	N_NAME="WSJT"
fi

# set path if -p [PATH] is used
if [ ! -z $P_OPT ] ; then
	P_PATH="$P_OPT"
fi

# set default path if -p [PATH] is not used
if [ -z $P_OPT ] ; then
	P_PATH="$HOME/.local/share"
fi

# combine final runtime path
RUNDIR="$P_PATH/$N_NAME"


################################################################################
# START MAIN SCRIPT
################################################################################
# clear and run the script
clear ||:
echo '--------------------------------------------'
echo -e ${C_G}" RUNNING WSJT"${C_NC}
echo '--------------------------------------------'
echo ''
echo " Instance Name ..: $N_NAME"
echo " Script Name ....: /usr/bin/$(basename $0)"
echo " Run Directory ..: $RUNDIR"
echo ''

# set the paths to WSJT package files
PATH=$PATH:/usr/bin:/usr/lib:/usr/share ; export PATH

# make dirs and copy / update files
if [ ! -d $RUNDIR ] ; then mkdir -p $RUNDIR ; fi

# WSJT wants to have CALL3.TXT and wsjtrc in the same directory
# as the main script wsjt.py, but if the user has updated
# CALL3.TXT, we dont want to overwrite their changes.
for f in "${file_array[@]}"
do
    if [ ! -e "$f" ] ; then cp /usr/share/wsjt/"$f" $RUNDIR ; fi
done

# add .desktop file for local users
if [ ! -d "$APPD" ] ; then mkdir -p "$APPD" ; fi
cp -u /usr/share/wsjt/applications/wsjt.desktop "$APPD"/ 

# add wsjt.xpm icon
if [ ! -d "$ICOND" ] ; then mkdir -p "$ICOND"/ ; fi
cp -u /usr/share/wsjt/pixmaps/wsjt.xpm "$ICOND"/

# use links rather than copying, only if they do not exist
if [ ! -L $RUNDIR/RxWav/Samples ] ; then
    mkdir -p $RUNDIR/RxWav
    ln -s /usr/share/doc/wsjt/RxWav/Samples $RUNDIR/RxWav/Samples
fi

# run paths are set by configure.ac, do not edit manually
cd $RUNDIR
env PYTHONPATH=/usr/lib/wsjt /usr/bin/python3 -O /usr/share/wsjt/wsjt.py
