#!/bin/sh

# yes/no

yesno() {
	key=""

	while true; do
		printf "$1 [y/n] : "
		read key
		case "$key" in
			[Yy] | [Yy][Ee][Ss] )
				break
			;;
			[Nn] | [Nn][Oo] ) 
				if [ "$2" ]; then
					quit "$2"
				else
					quit
				fi
			;;
		esac
	done
}
display_info() {
cat << __EOF__
####################################################################
## MochaSoft TN5250 Install Program :                             ##
##                                                                ##
## Mocha TN5250 is sold as shareware.  It is possible to  run     ##
## Mocha  TN5250  without  a license key. This should only be     ##
## done in a trial periode, and the program must not be  part     ##
## of any production environment.                                 ## 
##                                                                ##
## http://www.mochasoft.dk                                        ##
##                                                                ##
####################################################################
__EOF__

yesno "Do you accept all the terms of the license agreement?" "Sorry, If you don't accept our license agreement, you can not use this product!"
echo ""
}

# success message
success_msg() {
cat << __EOF__


to run mocha tn5250, type at a shell:

/usr/bin/tn5250

Have fun!

__EOF__
}

quit() {
cat << __EOF__
$1

Stop processing...

__EOF__
exit 1
}

copy() {
	printf "Copying file '$1' to '$2' : "
	if cp -p "$1" "$2"; then
		echo "OK"
	else
		echo "FAIL"
		echo "ERROR : Something wrong. Maybe the user has not access to the installation directory."
		quit
	fi
}

# Install
install() {
	echo "-----------------------------------------------------"
	echo "Step 1 : Copying Files."
	echo "-----------------------------------------------------"

        copy "tn5250" "/usr/bin/tn5250"
        mkdir /usr/man 2> /dev/null 
        mkdir /usr/man/man1 2> /dev/null 
        copy "tn5250.1" "/usr/man/man1/tn5250.1"
	
	echo ""
	echo "--------------------------------------------------------------------"
	echo "Step 2 : Adjusting Permissions."
	echo "--------------------------------------------------------------------"
        chmod 755 /usr/bin/tn5250  	

	echo ""

	echo "Successfully completed."
	printf "Press ENTER to continue..."
	read x
	echo ""
}

display_info
install
success_msg
/usr/bin/tn5250
exit 0
