#!/bin/bash
###############################################################################
# $Id: gdsh.init.sh,v 1.18 2010/08/26 22:44:24 tw Exp $
# 
# Initialization for gdsh. Used as rcfile argument to bash
#
# Copyright (C) 2008 - 2010 Thomas Weidenfeller
# 
# This file is part of gds2000tools/gdsh.
# 
# gds2000tools/gdsh is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
# 
# gds2000tools/gdsh 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 gds2000tools/gdsh.  If not, see <http://www.gnu.org/licenses/>.
###############################################################################

export BASH_ENV=

#
# Define the shell's file descriptors to be used for communicating with
# oscilloscopes
#
export GDSH_FD_MIN=3	# skip stdin, stdout, stderr
export GDSH_FD_MAX=9	# fds > 9 are used internally by bash
export GDSH_FD=-1	# no current file descriptor

#
# Load command description file and gds-2000 communication
# functions
#
for i in functions commands
do
	f="$GDSH_LIBDIR/gdsh.$i"
	[ -r "$f" ] || { echo "gdsh: $f not found, installation error!"; exit 1; }
	. "$f"
done

#
# Load rc files
# Note: ~/.bashrc should not be used for GDS-2000 specific
#	interactive configurations.  Use ~/.gdshrc instead
#
for i in /etc/bash.bashrc ~/.bashrc ~/.gdshrc
do
	[ -f "$i" ] && . "$i"
done

unset i
unset f

if [ -z "$PS1" ] ; then
	if [ "0$GDSH_CONNECT_AT_START" -ne 0 -a -n "$GDSH_DEV" ] ; then
		gds-open >/dev/null
	elif [ "0$GDSH_WRAPPER" -ne 0 ] ; then
		echo "gdsh: no default device configured." >&2
		exit 1
	fi
	return
fi

###############################################################################
# Interactive mode only
###############################################################################
  BLACK=`tput setaf 0`
    RED=`tput setaf 1`
  GREEN=`tput setaf 2`
 YELLOW=`tput setaf 3`
   BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
   CYAN=`tput setaf 6`
  WHITE=`tput setaf 7`
   BOLD=`tput bold`
    OFF=`tput sgr0`

#
# Take over prompting
#
PROMPT_COMMAND=
case "$TERM" in
xterm*|rxvt*) echo -ne "\033]0;gdsh\007" ;;
esac
PS1="${BOLD}${BLUE}gdsh${BLACK}${OFF}@ "
#PS1='gdsh@ '
PS2='gdsh> '

#
# Say hello to user
#
cat <<!

GDSH v@version@ - ${BOLD}${BLACK}G${RED}D${GREEN}S${YELLOW}-${BLUE}2000${BLACK}${OFF} series (and others) oscilloscope shell

gdsh builds on bash. For gdsh the following applies

gdsh Copyright (C) 2008 - 2010 Thomas Weidenfeller
This program comes with ABSOLUTELY NO WARRANTY; for details
type \`gdsh-warranty'.
This is free software, and you are welcome to redistribute it
under certain conditions; type \`gdsh-conditions' for details.

!

unset BLACK
unset RED
unset GREEN
unset YELLOW
unset BLUE
unset MAGENTA
unset CYAN
unset WHITE
unset BOLD
unset OFF

#
# Check if connection at startup is desired
#
if [ "0$GDSH_CONNECT_AT_START" -ne 0 -a -n "$GDSH_DEV" ] ; then
	echo "[gdsh: Trying to open $GDSH_DEV]" >&2
	gds-open

	#
	# Check if date/time sync at startup is desired
	#
	if [ $? -eq 0 -a "0$GDSH_SYNC_DT_AT_START" -ne 0 ] ; then
		echo "[gdsh: Synching date/time]" >&2
		gds-sync
	fi
fi
