#!/bin/sh
#
#    CDplayer 2.0 - command line Audio CDplayer
#    Copyright (C) 1993,1994 Mark Buckaway (mark@datasoft.com)
# 
#    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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Configure script. Asks user various questions and generates the 
# Makefile, etc. If /bin/sh is not bash, this may not work as bash
# requires -n and -e on echo to get things to work correctly. This
# script was hand written as I did not want to figure out how to make
# GNU's autoconf ask the user questions. Parts of this script were written
# by Udo Munk.
#
#
# Run a couple sanity checks
#
export PATH || (echo "Opps. Your sh is not a Bourne/Korn Shell compatible. Please run
this script using the correct shell. GNU bash is a good alternative.";exit)
#
# Determine how to suppress newline on echo command
# (This section by Udo Munk)
(echo "hi there\c" ; echo " ") >.echotmp
if grep c .echotmp >/dev/null 2>&1 ; then
    n='-n'
    c=''
else
    echo -e "hoho" >.echotmp
    if grep e .echotmp >/dev/null 2>&1 ; then
	n=''
    else
	n='-e'
    fi
    c='\c'
fi
rm .echotmp
#
# Define the defaults
INSTALLDIR="/usr/local/bin"
MANDIR="/usr/local/man"
LIBDIR="/usr/local/lib/tkcd"
BITMAPDIR="$LIBDIR/bitmap"
RCFILE="$LIBDIR/tkcdrc"
WORKMANDIR="$LIBDIR"
DEV_CDROM="/dev/mcd"
TKBUGS="0"
OSTYPE="LINUX"
OS="Linux"
CC="gcc"
CFLAGS="-O2"
LEX="flex"
YACC="bison -y"
LEXLIB=""
USE_FLEX="-DUSE_FLEX"
NECCD="NOTNEC"
#
# Internal Variables
#
DEBUG=0
NEC=0
#
# Findfile function - tests if a file is in the path and runs one command if true
# or another if false
# $1 = command to find
# $2 = command to run if true (recommend this be quoted)
# $3 = command to run if false
#
# Function returns setting FINDPATH to the location of the file
#
findfile () {
	FILE="$1"
	IFTRUE="$2"
	IFFALSE="$3"
	OLDIFS="$IFS"
	IFS=":"	
	set $PATH
	IFS="$OLDIFS"
	unset OLDIFS
	FOUNDIT=0
	for FINDPATH in "$@"
	do
		if [ -f "$FINDPATH/$FILE" ]
		then
			FOUNDIT=1
			break
		fi
	done
	if [ "$FOUNDIT" = "1" ]	
	then
		eval $IFTRUE
	else
		eval $IFFALSE
	fi
}
#
# Pathfix - fixes a pathname to escape the / characters
# This is necessary otherwise sed barfs in the sed scripts
# configure generates
#
pathfix () {
	echo "$1" | sed -e 's/\//\\\//g'
}
#
# Main code starts here
#
echo $n "CDplayer/TKCD Configure

This configure script will ask a few questions about your system
and how CDplayer should be configured. This information will
be used in compiling CDplayer.

Currently Linux and Cohernet 4.2 are supported. Others SHOULD work as well.

Press <ENTER> to continue.$c"
read JUNK
#
# Check the OS Type
#
UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown
UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown

case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
	i[34]86:*:*COHERENT)
		echo "Hmmm...looks like a Coherent system."
		OSTYPE="COHERENT"
		OS="$UNAME_VERSION" ;;
	i[34]86:Linux:*:*)
		echo "Hmmm...looks like a Linux system."
		OSTYPE="LINUX"
		OS="$UNAME_SYSTEM" ;;
	*)
		echo "Hmmm...don't know what system this is. Assuming this is not Linux."
		OSTYPE="$UNAME_SYSTEM"
		OS="$UNAME_SYSTEM" ;;
esac
echo
echo "Checking for programs in the path..."
findfile "gcc" "echo 'Found gcc.'" "CC=cc;echo 'gcc not found. Using cc instead.'"
findfile "flex" "echo 'Found flex.'" "LEX=lex;echo 'flex not found. Using lex instead.'"
findfile "bison" "echo 'Found bison.'" "YACC=yacc;echo 'bison not found. Using yacc instead.'"
findfile sed "echo 'Found sed.'" "echo 'Opps...can't find sed in your path. Please correct.';exit"
findfile ar "echo 'Found ar.'" "echo 'Opps...can't find ar in your path. Please correct.';exit"
echo $n "What compiler options should I use? [$CFLAGS]: $c"
read REPLY
if [ -n "$REPLY" ]
then
	CFLAGS="$REPLY"
fi
echo "Setting compiler options to \"$CFLAGS\"."
echo
echo $n "Looking for a cdrom device...$c"
find /dev -print >.$$.tmp
FOUNDIT=0
for device in mcd cdrom cdrom1 mitsumi_cd sr0
do
	if grep "\/dev\/$device" .$$.tmp >/dev/null
        then
           FOUNDIT=1
	   echo
           echo $n "Hmmm...found /dev/$device. Should I use this device? [y/n] $c"
           read REPLY JUNK
           case $REPLY in
              [yY]*) echo "OK. Using /dev/$device."
                     break ;;
	      [nN]*) FOUNDIT=0
		     break ;;
		  *) FOUNDIT=0
		     break ;;
           esac
        fi
done
rm -f .$$.tmp
if [ "$FOUNDIT" = "1" ]
then
	DEV_CDROM="/dev/$device"
else
	done=0
	while [ "$done" != "1" ]
        do
		echo "What device (e.g. /dev/sr0) should I use for the CD?"
		echo $n ": $c"
		read REPLY JUNK
		if [ -z "$REPLY" ]
		then
			continue
		fi
		if [ ! -b $REPLY -a ! -c $REPLY ]
                then
			echo "$REPLY does not exist or is not a CDROM device" 
   		else
			done=1
		fi
	done
	DEV_CDROM="$REPLY"
	echo "Ok. Using $REPLY for the CD."
fi
echo $n "
NEC CDROM drives are strange and the method of accessing the audio
functions is NOT the same as other CDROM drives. For an NEC CDROM
to work with CDplayer, a special function is called to send raw SCSI
commands to the NEC CDROM drive. This pertains to most CDR-XX type
drives. Newer ones should be compatible without the use of this
special function. If you have a CDR-XX type NEC CDROM, answer yes
below; otherwise, answer no.

Do you have an NEC CDROM drive? [y/n]: $c"
read REPLY JUNK
case $REPLY in
	[yY]*) NEC=1 ;;
esac
echo $n "NEC CDROM code will $c"
if [ "$NEC" = "1" ]
then
	echo "be used."
	NECCD="NEC_CDROM"
else
	echo "NOT be used."
fi
echo
done=0
while [ "$done" != "1" ]
do
	echo "Where should CDplayer and TkCD be installed? [$INSTALLDIR]"
	echo $n ": $c"
	read REPLY JUNK
	if [ -z $REPLY ]
	then
		REPLY="$INSTALLDIR"
	fi
	if [ ! -d $REPLY ]
        then
		echo "$REPLY does not exist or is not a directory"
   	else
		done=1
		INSTALLDIR=$REPLY
	fi
done
echo "CDplayer will be installed in $INSTALLDIR"
done=0
while [ "$done" != "1" ]
do
	echo "Where should CDplayer and TkCD's manpage be installed? [$MANDIR]"
	echo $n ": $c"
	read REPLY JUNK
	if [ "$REPLY" = "" ]
	then
		break
	fi
	if [ ! -d $REPLY ]
        then
		echo "$REPLY does not exist or is not a directory"
   	else
		done=1
		MANDIR=$REPLY
	fi
done
echo "CDplayer's manpage will be installed in $MANDIR/man1"
echo
echo "Configuring TkCD..."
findfile wish 'WISH=$FINDPATH/wish' 'unset WISH'
if [ -z "$WISH" ]
then
	echo $n "Hmmm...you don't seem to have Tk/tcl installed or wish, the Tk/tcl
interpreter, is not in your path. If you wish to use TkCD, please install
Tk/tcl or put wish in your path and re-run this script. TkCD will not be
configured and will not be unavailable.

TkCD configuration skipped. Please <ENTER> to continue.$c"
	read JUNK
	CFG_TKCD=0
else
	echo "Wish found in $WISH. TkCD will use this."
	#
	# Define the bitmap directory is required
	#
	done=0
	while [ "$done" != "1" ]
	do
		echo "TkCD requires a directory to place it's rc file and bitmaps."
		echo "Where should the TkCD directory be located? [$LIBDIR]"
		echo $n ": $c"
		read REPLY JUNK
		if [ -z "$REPLY" ]
		then
			REPLY="$LIBDIR"
		fi
		if [ ! -d $REPLY ]
	        then
			echo $n "\"$REPLY\" does not exist. Should I create it? [y/n]: $c"
			read ANS JUNK
			case $ANS in
				[yY]*) mkdir $REPLY
				       if [ -d $REPLY ]
				       then
				           echo "$REPLY created."
					   LIBDIR=$REPLY
					   done=1
				       else
				           echo "ERROR: Unable to create directory"
				       fi
			esac	
		else
			done=1
		fi
	done
	echo "TkCD's global rc file and bitmaps will be installed in \"$LIBDIR\"
"
	if [ "$NEC" != "1" ]
	then
		echo $n "The Linux Kotobuki/Matsushita/Panasonic CR-5xx CDROM driver is
broken and will cause TkCD to not skip tracks correctly. TkCD has a workaround
to get around this problem. This option should also be used if you TkCD is not
skipping tracks when the skip track button is pressed. This workaround is only
required for a Linux system running a CR-5XX CDROM drive or systems not skipping
to the next track. Answer yes below is either is the case.

Should I use this workaround? [y/n]: $c"
		read REPLY JUNK
		case $REPLY in
			[Yy]*) TKBUGS="1"
		       	echo "TkCD will updated to correct minor problems with this driver." ;;
		esac
	fi
	CFG_TKCD=1
	FOUNDIT=0
	for WORKMAN in "$HOME/.workmandb" /usr/openwin/lib/workmandb /usr/local/workmandb
	do
		if [ -f "$WORKMAN" ]
		then
			if (echo "$WORKMAN" | grep "$HOME")
			then
				WORKMAN="~/.workmandb"
			fi 
			echo $n "
It appears you have used Workman at one point as the Workman database
file appears \"$WORKMAN\". TkCD can use the
Workman CD Database and add to it should you desire. Should I use
this file for TkCD? [y/n]: $c"
			read REPLY JUNK
			case $REPLY in
				[yY]*) WORKMANFILE="$WORKMAN"
				       FOUNDIT=1
				       break ;;
			esac
		fi
	done
	if [ "$FOUNDIT" = "0" ]
	then
		done=0
		while [ "$done" != "1" ]
		do
			echo "Where should workmandb, the CD Database file, be placed? [$WORKMANDIR]"
			echo $n ": $c"
			read REPLY JUNK
			if [ "$REPLY" = "" ]
			then
				break
			fi
			if [ ! -d $REPLY ]
       			then
				echo "$REPLY does not exist or is not a directory"
   			else
				done=1
				WORKMANDIR=$REPLY
			fi
		done
		WORKMANFILE="$WORKMANDIR/workmandb"
	fi
	echo "Ok. The workmandb file will be $WORKMANFILE."			
fi		
#
# All options configured. Now, run SED on the result.
#
# First modify src/Makefile.in
if [ "$CFG_TKCD" = "1" ]
then
	echo "Creating tkcd..."
	sed -e "s/@BITMAPDIR@/$(pathfix "$BITMAPDIR")/
s/@LIBDIR@/$(pathfix "$LIBDIR")/
s/@TKBUGS@/$(pathfix "$TKBUGS")/
s/@WORKMANFILE@/$(pathfix "$WORKMANFILE")/
s/@RCFILE@/$(pathfix "$RCFILE")/
s/@WISH@/$(pathfix "$WISH")/" src/tkcd.in > bin/tkcd
	chmod 755 bin/tkcd
	echo "Creating tkcdrc..."
sed -e "s/@BITMAPDIR@/$(pathfix "$BITMAPDIR")/
s/@LIBDIR@/$(pathfix "$LIBDIR")/
s/@TKBUGS@/$TKBUGS/
s/@WORKMANFILE@/$(pathfix "$WORKMANFILE")/" src/tkcdrc.in > bin/tkcdrc
fi
echo "Creating src/Makefile..."
sed -e "s/@CC@/$CC/
s/@LEX@/$LEX/
s/@LEXLIB@/$LEXLIB/
s/@YACC@/$YACC/
s/@OSTYPE@/$OSTYPE/
s/@OS@/$OS/
s/@CFLAGS@/$CFLAGS/
s/@USE_FLEX@/$USE_FLEX/
s/@DEV_CDROM@/$(pathfix "$DEV_CDROM")/
s/@NECCD@/$NECCD/" src/Makefile.in > src/Makefile
echo "Creating install.sh..."
sed -e "s/@INSTALLDIR@/$(pathfix "$INSTALLDIR")/
s/@CFG_TKCD@/$CFG_TKCD/
s/@MANDIR@/$(pathfix "$MANDIR")/
s/@LIBDIR@/$(pathfix "$LIBDIR")/
s/@BITMAPDIR@/$(pathfix "$BITMAPDIR")/
s/@RCFILE@/$(pathfix "$RCFILE")/" install.sh.in > install.sh
echo "Configuration complete."
