#!/bin/sh
#
#
#	***	***	     /usr/bin/hf  		***	***
#	***					 		***
#	***	    	 Main Start Script for 	 		***
#	***	        /usr/bin/hfkernel_user	     		***
#	***	         and /usr/bin/hfterm			***
#	***	        by Guenther Montag DL4MGE		***
#	***					 		***
#	***	***	***	***	***	***	***	***	
#
# There is a system-wide config file: /etc/hf.conf
# I will prepare the same defaults here and there, 
# they will work in most cases.
# After this I will include the config file,
# so that the defaults can be overwritten.
# (Especially for KNOPPIX linux users, it is good to have /etc/hf.conf
# because if the system runs from cd, you have no write access here.)

## There is a lot of documentation in English and German!
## See comments in /etc/hf.conf, 
## See README or LIESMICH in doc subdirectory of hf package,
## See F1 in the hfterm window!
## To configure, just remove the single comment signs in the lines you need.

## Prepare Debug Function: ####################################################
function debug {
clear
echo "Hello, $startuser, I am $0, the main start script for hfkernel and hfterm,"
echo "Tom Sailer's ham radio RTTY - AMTOR - PACTOR program package ..."
echo "If something hangs, see and edit /etc/hf.conf."
echo "There is help in English and German: /usr/share/doc/hf/README "
echo "If you start 'hfterm' under X, you find the help with F1."
echo "The homepage of hf is http://hfterm.sf.net !"
echo "Feel free to mail to our mailing list: hfterm-hackers@lists.sf.net "
echo "and to subscribe it: http://lists.sf.net/lists/listinfo/hfterm-hackers "
echo Your cpu frequency is $cpumhz.
echo Your configuration from config file $config_file: 
echo sound_starter: $sound_starter, alsa_prefix: $alsa_prefix
echo unix domain socket: $socket, root-workaround: $root_trick
echo rdtsc_option: $rdtsc_option, time correction: $time_correction, soundcard clock correction: $sound_correction
echo other options: $kerneloptions, serial ptt: $serial 
if fuser -s $serial; then 
    echo -n " is in use!"
    fuser -v $serial
    echo "please kill the processes using $serial or use another serial port."
    exit
    else echo "this is at least not used by one of $startuser 's processes..."; fi
};
echo -- options to hfterm: mailbox host: $mailbox_host, mailbox port: $mailbox_port

## Prepare Start Function: ####################################################
function startkernel {
$x_prefix $alsa_prefix $root_prefix /usr/bin/hfkernel_user \
-c $socket -p $serial -m $cpumhz $rdtsc_option \
-t $time_correction -s $sound_correction $kerneloptions
};

## Presetting Defaults: #######################################################
serial=/dev/ttyS1
sound_starter=
alsa_prefix=
x_prefix=
socket=/var/run/hfapp
root_trick=suid
rdtsc_option=
time_correction=1  
sound_correction=1
kerneloptions=
config_file=/etc/hf.conf
cpumhz=`cat /proc/cpuinfo | grep MHz | cut -c 11-`

## include config file: #######################################################
. $config_file

## prepare prefix for calling root-right-program as user ######################
if [ $root_trick = "su1"  -o  $root_trick = "sudo" ]; then
    root_prefix=$root_trick
fi

## start oss driver, if needed: ###############################################
$sound_starter

## see if xterm or console: ###################################################
if [ $TERM = "xterm" ]; then 
    x_prefix="xterm -e"
fi

## see who has started me
startuser=`whoami`

## clean the board and talk with user #########################################
clear
debug

## and then the real work #####################################################

## if root, start the hfkernel only: ##########################################
if  [ $startuser = "root" ]; then
echo "For you, root, I will start only hfkernel_user for test purposes."
echo "If you want to run hfterm, start me under X as user!"
startkernel
fi

## if not root as user, but text console, start the hfkernel only: ############
if  [ $startuser != "root" -a  $TERM = "linux" ]; then 
echo "I start hfkernel_user from console for test. To run hfterm, start under X!"
startkernel
fi

## if not root, and if not on text console, try to start everything:  ########
# prepare hfterm: 
if  [ $startuser != "root" -a $TERM = "xterm" ]; then 
#echo "You called me under X, and not as root."
#echo "So I will start hfkernel in a xterm in the background,"
#echo "and then I will start hfterm."
    x_prefix="xterm -e"
# if not yet there, make a directory for the program's files
# and fill it with examples
cd 
if !(test -d hf); then 
    mkdir hf; 
    cp -v /usr/share/doc/hf/hf-examplefiles/* hf; 
    echo "I made a hf directory in your home, and filled it with example files."
fi
# start the hfkernel on xterm in the background ###############################
#echo "Now I will hand over to /usr/bin/hfkernel_user."
startkernel &
sleep 1
# start hfterm, connected with the unix domain socket hfapp
# and, if you want, with the options for the mailbox host and port.
/usr/bin/hfterm -k /var/run/hfapp -h $mailbox_host -p $mailbox_port
fi

## Cleanup ####################################################################
# kill hfkernel_user silently after hfterm is finished
$root_prefix /usr/bin/hfkernel_user -k > /dev/null 2>&1
