#!/bin/bash
# configure - home-grown configuration script for bsd-games

# This will only ask some questions if the previous answers are appropriate.
# It will substitute values in the directories built in, and the main
# Makefile.

subst_vars=srcdir

srcdir="`pwd`"

ask () {
    long_query="$1"
    query_var="$2"
    default="$3"
    echo -n "$long_query [$default] "
    read input
    case "$input" in
	'')
	    input="$default"
	    ;;
	*)
	    ;;
    esac
    eval $query_var=\"\$input\"
    subst_vars="$subst_vars $query_var"
}

ask_yn () {
    eval $2=
    eval answer=\$$2
    while test "x$answer" = x; do
	ask "$1" $2 $3
	eval answer=\$$2
	case "$answer" in
	    [yY]*)
		answer=y
		;;
	    [nN]*)
		answer=n
		;;
	    *)
		answer=
		echo "Please answer y or n"
		;;
        esac
    done
    eval $2=\$answer
}

askperms () {
    filetype=$1
    var_prefix=$2
    def_owner=$3
    def_group=$4
    def_perms=$5
    if [ $do_chown = y ]; then
	ask "$filetype owner" ${var_prefix}_owner $def_owner
	ask "$filetype group" ${var_prefix}_group $def_group
    fi
    ask "$filetype permissions" ${var_prefix}_perms $def_perms
    if [ $do_chown = y ]; then
	eval install_$var_prefix=\"install -c -m \$${var_prefix}_perms -o \$${var_prefix}_owner -g \$${var_prefix}_group\"
    else
	eval install_$var_prefix=\"install -c -m \$${var_prefix}_perms\"
    fi
    subst_vars="$subst_vars install_$var_prefix"
}

building_in () {
    echo "$build_dirs" |grep -w "$1" >/dev/null
}

game_ask () {
    game="$1"
    if building_in "$game"; then
	ask "$2" "$3" "$4"
    fi
}

echo "For normal usage the installation prefix will be empty.  If you wish"
echo "to install everything in another directory to that in which it will"
echo "finally be located (so that your packaging system can then move it"
echo "there) you should name that directory here.  This is most likely to"
echo "be the case if you are packaging bsd-games for a Linux distribution."
ask "Installation prefix" install_prefix ''

def_build_dirs=
for file in *; do
    if [ -e "$file/Makefile" ]; then
	def_build_dirs="$def_build_dirs $file"
    fi
done
def_build_dirs=${def_build_dirs## }

ask "Games to build" build_dirs "$def_build_dirs"
ask "Games directory" gamesdir /usr/games
if building_in hunt; then
    echo
    echo "Hunt includes a daemon for coordinating games with multiple players"
    ask "Daemon directory" sbindir /usr/sbin
else
    sbindir=
    subst_vars="$subst_vars sbindir"
fi
if building_in fortune; then
    echo
    echo "Fortune includes a non-game utility strfile"
    ask "Non-game binary directory" usrbindir /usr/bin
else
    usrbindir=
    subst_vars="$subst_vars usrbindir"
fi

hidegame=:
subst_vars="$subst_vars hidegame"
if building_in dm; then
    echo
    echo "You may wish to restrict the use of games by user, terminal, load,"
    echo "etc..  This can be done by the use of dm.  If you use this"
    echo "configuration, then games will be kept in a non-world-searchable"
    echo "directory such as /usr/libexec/dm and replaced by symlinks to dm."
    echo "Even if you don't choose this option, you will still be asked"
    echo "for the directory name, since you are building dm, and can change"
    echo "manually later."
    ask_yn "Use dm and hide games" use_dm n
    ask "Directory for hidden games" libexecdir /usr/libexec/dm
    if [ "$use_dm" = y ]; then
	hidegame=$srcdir/hide-game
    fi
fi
ask "Section 6 manpage directory" man6dir /usr/man/man6
if building_in dm || building_in fortune; then
    ask "Section 8 manpage directory" man8dir /usr/man/man8
else
    man8dir=
    subst_vars="$subst_vars man8dir"
fi
if building_in dm; then
    ask "Section 5 manpage directory" man5dir /usr/man/man5
else
    man5dir=
    subst_vars="$subst_vars man5dir"
fi
# arch-dependent data is a bad thing, and we should change the games to
# avoid it where possible, but until then this is needed.
ask "Library directory for constant data
	(architecture dependent)" usrlibdir /usr/lib/games
# We use /usr/share for this by the not-yet-released FHS, at least according
# to the discussion on debian-devel
ask "Library directory for constant data
	(architecture independent)" sharedir /usr/share/games
ask "Library directory for variable data" varlibdir /var/lib/games
ask_yn "Set owners/groups on installed files" do_chown y

echo
echo "For scorefiles there are at least two possible security policies if you"
echo "want them to work.  You can make the files world-writable, and then"
echo "anyone who wants can put anything in them, which may not be desirable if"
echo "you think people might cheat this way.  Or you can make the games that"
echo "use them setgid games, and give the files permissions 0664.  Note,"
echo "however, that the games may well be insecure when this is done and"
echo "malicious users can probably still overwrite anything writable by"
echo "group games, since the games were probably not designed with security in"
echo "mind.  The default is neither of these: it creates scorefiles with"
echo "permissions 0644 and gives the games no special privileges, which is"
echo "more secure but means that the games will fail when trying to write"
echo "to their scorefiles."

askperms "Binary" binary root root 0755
askperms "Game with scorefile" score_game root root 0755 # or root games 2755?
if building_in hunt; then
    askperms "Daemon" daemon root root 0755
fi
if building_in dm; then
    askperms "Directory for hidden games" dmdir root games 0750
    install_dmdir=`echo "$install_dmdir"| sed 's/install -c/install -d/'`
    askperms "dm" dm root games 2755
fi
askperms "Manpage" manpage root root 0644
askperms "Constant data" constdata root root 0644
askperms "Variable data" vardata root root 0644 # or 0666?

use_dot_so=
while test x$use_dot_so = x; do
    ask "Use .so or symlinks for manpages" use_dot_so .so
    case "$use_dot_so" in
        .so)
    	;;
        syml*)
    	use_dot_so=symlinks
    	;;
        *)
    	use_dot_so=
	echo "Please answer \`.so\' or \`symlinks\'"
    	;;
    esac
done
subst_vars="$subst_vars use_dot_so"

ask_yn "Gzip manpages" gzip_manpages y

# What we do with manpages is a bit complicated.  If either ppt or morse is
# being built, we must also install the bcd manpage, even if bcd isn't being
# built.  We then need to do either .so or symlink.  This is all handled
# by the install-man.in script.

ask_yn "Strip installed binaries" strip_install y

# Save old install_binary as install_script
install_script="$install_binary"
subst_vars="$subst_vars install_script"

if [ $strip_install = y ]; then
    for type in binary score_game daemon; do
	eval install_$type=\"\$install_$type -s\"
    done
fi

echo
echo "It is presumed in some places by the Makefiles that the compiler"
echo "will be some form of gcc."
ask "Compiler" cc gcc
ask "Optimize flags" optimize_flags "-g -O2"
echo
echo "The default warning flags should give a compile with few warnings."
ask "Compiler warning flags" warning_flags "-Wall -W -Wstrict-prototypes -Wmissing-prototypes"
echo
echo "You probably want the default here, or could use -lncurses_g for"
echo "debugging ncurses.  Use -lcurses -ltermcap if you want to try that,"
echo "but note that this is no longer supported and may not work."
ask "Ncurses library" ncurses_lib -lncurses
echo
echo "If you built ncurses with --disable-overwrite, you may need"
echo "-I/usr/include/ncurses here"
ask "Ncurses includes" ncurses_includes ''

# Should we try to detect the libc version?
echo
echo "Libc versions other than 5 or 6 may not work."
ask "Libc version" libc_version 5
case "$libc_version" in
    4*|5*)
    libc_version=5
    def_bsd_sig=-D__USE_BSD_SIGNAL
    ;;
    *)
    libc_version=6
    def_bsd_sig=
    ;;
esac
echo
echo "With libc5 -D__USE_BSD_SIGNAL is needed for BSD signal semantics if not"
echo "using libbsd or _BSD_SOURCE, but with libc6 the default signal semantics"
echo "are BSD and no such define is needed."
ask "Defines for BSD signal semantics" bsd_signal_defs "$def_bsd_sig"
ask "Other CFLAGS" other_cflags ''
ask "Other LDFLAGS" other_ldflags ''
if building_in atc; then
    ask "Yacc program" yacc "bison -y"
fi
if building_in atc; then
    ask "Lex program" lex flex
    ask "Lex library" lex_lib -lfl
fi

echo
ask "Pager" pager /usr/bin/less

if building_in fortune; then
    echo
    echo "Fortune comes with some potentially offensive fortunes.  You should"
    echo "only install these if you are sure that your users want them."
    ask_yn "Install offensive fortunes" offensive_fortunes n
    if [ $offensive_fortunes = y ]; then
	fortune_type=real
    else
	fortune_type=fake
    fi
    subst_vars="$subst_vars fortune_type"
fi

echo
echo "You can configure the exact names used for data files by various"
echo "of the games.  Most probably the defaults are OK."
game_ask atc "Directory for atc static data" atc_dir "$sharedir/atc"
game_ask atc "Score file for atc" atc_scorefile "$varlibdir/atc_score"
game_ask battlestar "Score file for battlestar" battlestar_scorefile "$varlibdir/battlestar.log"
# Bog has some other configuration
game_ask boggle "Directory for boggle static data" boggle_dir "$sharedir/boggle"
game_ask canfield "Score file for canfield" canfield_scorefile "$varlibdir/cfscores"
game_ask cribbage "File for cribbage instructions" cribbage_instrfile "$sharedir/cribbage.instr"
game_ask cribbage "Score file for cribbage" cribbage_scorefile "$varlibdir/criblog"
game_ask dm "Configuration file for dm" dm_configfile /etc/dm.conf
game_ask dm "File to disable games playing" dm_nogamesfile /etc/nogames
game_ask dm "Log file for dm" dm_logfile "$varlibdir/games.log"
game_ask fish "File for fish instructions" fish_instrfile "$sharedir/fish.instr"
game_ask hangman "Words file for hangman" hangman_wordsfile "$sharedir/hangman-words"
# Hunt has some other configuration
game_ask monop "File for monop cards" monop_cardsfile "$usrlibdir/monop-cards.pck"
game_ask phantasia "Directory for phantasia variable data" phantasia_dir "$varlibdir/phantasia"
game_ask quiz "Directory for quiz static data" quiz_dir "$sharedir/quiz"
game_ask robots "Score file for robots" robots_scorefile "$varlibdir/robots_roll"
game_ask rogue "Score file for rogue" rogue_scorefile "$varlibdir/rogue.scores"
game_ask sail "Score file for sail" sail_scorefile "$varlibdir/saillog"
game_ask snake "Score file for snake" snake_scorefile "$varlibdir/snake.log"
game_ask snake "Raw score file for snake" snake_rawscorefile "$varlibdir/snakerawscores"
game_ask tetris "Score file for tetris" tetris_scorefile "$varlibdir/tetris-bsd.scores"
game_ask wump "File for wump info" wump_infofile "$sharedir/wump.info"

# Generate simplistic substitution script.
# This does not allow escaped @s in the substituted file, will fail on
# newline or % in filenames, etc..  The justification is that proper insertion
# of arbitrary 8-bit data in different files requires too much knowledge
# of escapes specific to the particular file type, so these things would
# probably fail anyway.
: >subst.sed
for var in $subst_vars; do
    eval echo \""s%@$var@%\$$var%g"\" >> subst.sed
done

substitute () {
    for file in "$@"; do
	dir="${file%%/*}"
	if building_in $dir || [ "$file" = "$dir" ]; then
	    source_file="${file}.in"
	    echo "Extracting $file from $source_file"
	    cp "$source_file" "$file" # set permissions
	    sed -f subst.sed <"$source_file" >"$file"
	fi
    done
}

subst_files="`cat substfiles`"

if [ x"$use_dm" = xy ]; then
    subst_files="$subst_files hide-game"
fi

substitute $subst_files
