#!/bin/sh
#
# operstats.conf generator. This script will only prompt for required
# informatation. If it is run with the -auto option, it is assumed that
# it is being run by the Makefile, and will ask if you want to run it.

# Nifty handy functions.

echo2 () {
	$ECHO2 "$*$ECHO2SUF"
}

ECHO2SUF=''
if [ "`echo -n a ; echo b`" = "ab" ] ; then
	ECHO2='echo -n'
elif [ "`echo 'a\c' ; echo 'b'`" = "ab" ] ; then
	ECHO2='echo' ; ECHO2SUF='\c'
elif [ "`printf 'a' 2>&1 ; printf 'b\n' 2>&1`" = "ab" ] ; then
	ECHO2='printf "%s"'
else
	ECHO2='echo'
fi
export ECHO2 ECHO2SUF

# Command line parsing

AUTO=
IGNORE_CACHE=
export AUTO IGNORE_CACHE

while [ $# -gt 0 ] ; do
    if [ "$1" = "-ignore-cache" ] ; then
	IGNORE_CACHE=flibble
    fi
    if [ "$1" = "-auto" ] ; then
	AUTO=flibble
    fi
    shift
done

# And now the defaults for the required options, as found in the conf.

DEST=flibble
DOMAIN=example.net
INFONAME=operstats.$DOMAIN
CONNECTIP=127.0.0.1
CONNECTPASS=linkage
CONNECTPORT=6667
UNREALNUMERIC=200
IRCDTYPE=1
SRANICK=
SRAPASS=
NETWORK=ExampleNet
IRCD=1

# Load up configure.cache -- For the destination dir.
if [ -f ../configure.cache ] ; then
    . ../configure.cache
fi

# Check for a makeconf.cache
if [ -f ../makeconf.cache -a ! "$IGNORE_CACHE" ] ; then
    cat <<EOT
Using cached answers from a previous run. To ignore, either
remove makeconf.cache or give the command-line option "-ignore-cache".

EOT
    . ../makeconf.cache
fi

# Intro text

export ok INPUT
ok=0
if [ "$AUTO" ] ; then
	echo " "
	echo "*** Would you like to run the makeconf script?"
	echo "*** You will still need to edit your conf file"
	echo "*** by hand to configure most options."
	echo " "
	while [ $ok -eq 0 ] ; do
		echo2 "[Yes] "
		if read INPUT ; then : ; else echo "" ; exit 1 ; fi
		if [ ! "$INPUT" ] ; then
			ok=1
		fi
		case $INPUT in
			[Yy]*)
				ok=1
				;;
			[Nn]*)
				echo " "
				echo "*** Now edit the conf, configure your server(s), and run"
				echo "*** OperStats. Please read the documentation before seeking"
				echo "*** help. Thanks."
				echo " "
				exit 0
				;;
		esac
	done
fi

clear
echo "OperStats -- Copyright (c) 2000-2003 Darcy Grexton"
echo ""
echo "This generator will only ask you for information for the"
echo "most crucial questions. The operstats.conf file contains"
echo "many configurable options. It's suggested that after you"
echo "are done with this script, you check your conf file over."
echo ""
echo2 "Press enter to begin, or CTRL+C to abort."
if read INPUT ; then : ; else echo "" ; exit 1 ; fi

# On to the questions!

echo " "
echo "What IP address should OperStats try to connect to?"
echo2 "[$CONNECTIP] "
read cc
if [ ! -z "$cc" ]; then
	CONNECTIP="$cc"
fi

echo " "
echo "What port should we use when connecting to the above address?"
echo2 "[$CONNECTPORT] "
read cc
if [ ! -z "$cc" ]; then
	CONNECTPORT="$cc"
fi

echo " "
echo "What password should we use for the link?"
echo2 "[$CONNECTPASS] "  
read cc
if [ ! -z "$cc" ]; then
	CONNECTPASS="$cc"
fi

echo " "
echo "What is your network's domain name? ie, example.net"
echo2 "[$DOMAIN] "
read cc
if [ ! -z "$cc" ]; then
	DOMAIN="$cc"
fi

# Update these
INFONAME=operstats.$DOMAIN

echo " "
echo "What should OperStats call it's server?"
echo2 "[$INFONAME] "
read cc
if [ ! -z "$cc" ]; then
	INFONAME="$cc"
fi

echo " "
echo "What is your network's name? ie, ExampleNet"
echo2 "[$NETWORK] "
read cc
if [ ! -z "$cc" ]; then
	NETWORK="$cc"
fi

ok=0
echo " "
echo "Please select the IRCd you are using from the list below."
echo "This MUST be set properly or OperStats will not function correctly."
echo " "
echo "    1) DreamForge"
echo "    2) Prometheus"
echo "    3) Bahamut"
echo "    4) UnrealIRCd 3"
echo "    5) UnrealIRCd 3.2"
while [ $ok -eq 0 ] ; do
	echo2 "[$IRCDTYPE] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$IRCDTYPE
	fi
	case $INPUT in
		[1-5])
			ok=1
			;;
		*)
			echo "Please choose one of the options listed above."
			;;
	esac
done
IRCDTYPE=$INPUT

if [ "$IRCDTYPE" -eq "4" -o "$IRCDTYPE" -eq "5" ] ; then
	echo " "
	echo "Please enter a number between 1 and 255 for use with Unreal."
	echo "This number cannot be in use by any other servers on your network."
	echo2 "[$UNREALNUMERIC] "
	read cc
	if [ ! -z "$cc" ]; then
		UNREALNUMERIC="$cc"
	fi
fi

echo " "
echo "Please give the nickname for one SRA. If you want to define more,"
echo "edit operstats.conf after you are done makeconf. You must define"
echo "at least one SRA."
echo2 "[$SRANICK] "
read cc
if [ ! -z "$cc" ]; then
	SRANICK="$cc"
fi

echo " "
echo "Please give the password for the above SRA."
echo2 "[$SRAPASS] "
read cc
if [ ! -z "$cc" ]; then
	SRAPASS="$cc"
fi

echo " "
echo2 "Writing operstats.conf ..."
cat >operstats.conf <<_EOT_
# OperStats Config file -- Copyright (c) 2000-2003 Darcy Grexton
#
# Options that indicate a time value should be given as #w#d#h#m#s - Examples:
# 2 weeks, 4 days, 3 hours, 15 minutes, 30 seconds: 2w4d3h15m30s
# 5 days, 12 hours, 10 minutes, 30 seconds: 5d12h10m30s
# 6 hours, 30 seconds: 6h30s
# 60 seconds: 60s
# 1 minute: 1m
#
# Options with no parameters/format listed are toggles and are either
# on or off. (uncommented or commented)
#
# Multi-parameter options require all parameters unless otherwise stated.

# Server Information: What should we call ourselves?
# Format: Info operstats.server.name Server Description
# This is required.

Info $INFONAME

# Connection Information: Where should we connect to?
# Format: Connect IP.or.address Port Password
# This is required.

Connect $CONNECTIP $CONNECTPORT $CONNECTPASS

# BindTo: What address/port should be bind to, if any?
# Format: BindTo IP.or.address Port
# Comment to disable.

#BindTo $CONNECTIP $CONNECTPORT

# If you are using UnrealIRCd, define this. This needs to be a number
# between 1 and 255, and not used by any other servers on your network.
# This is required for UnrealIRCd.

_EOT_
     
if [ "$IRCD" -eq "3" -o "$IRCD" -eq "5" ] ; then cat <<_EOT_ >>operstats.conf
UnrealNumeric $UNREALNUMERIC
_EOT_
fi

if [ ! "$IRCD" -eq "3" -a ! "$IRCD" -eq "5" ] ; then cat <<_EOT_ >>operstats.conf
#UnrealNumeric 200
_EOT_
fi

cat <<_EOT_ >>operstats.conf

# Define the type of IRCd you are using. This MUST be configured. If
# this is not set properly, OperStats will not work properly on your network.
# If your IRCd is not listed below, you can try a similar IRCd, but there
# are no gaurantees with any IRCds not listed.
# There are 5 supported IRCds:
#      DreamForge
#      Prometheus
#      Bahamut
#      Unreal 3
#      Unreal 3.2
# You must include any spaces shown above. For example:
# IRCdType Unreal 3.2
# This is required.

_EOT_

if [ "$IRCDTYPE" -eq "1" ] ; then cat <<_EOT_ >>operstats.conf
IRCdType DreamForge
_EOT_
fi

if [ "$IRCDTYPE" -eq "2" ] ; then cat <<_EOT_ >>operstats.conf
IRCdType Prometheus
_EOT_
fi

if [ "$IRCDTYPE" -eq "3" ] ; then cat <<_EOT_ >>operstats.conf
IRCdType Bahamut
_EOT_
fi

if [ "$IRCDTYPE" -eq "4" ] ; then cat <<_EOT_ >>operstats.conf
IRCdType Unreal 3
_EOT_
fi

if [ "$IRCDTYPE" -eq "5" ] ; then cat <<_EOT_ >>operstats.conf
IRCdType Unreal 3.2
_EOT_
fi

cat <<_EOT_ >>operstats.conf

# OperServ Information - IRC Operator Service
# Format: OperatorService NickName UserName HostName Modes RealName
# Mode suggestions:
#      UnrealIRCd: +ioS
#      Other     : +io
# Comment to disable.

OperatorService OperServ service $DOMAIN +io IRC Operator Service

# StatServ Information - Statistical Service
# Format: StatisticalService NickName UserName HostName Modes RealName
# Mode suggestions:
#      UnrealIRCd: +iS
#      Other     : +i
# Comment to disable.

StatisticalService StatServ service $DOMAIN +i Statistical Service

# GlobalNoticer Information - Global Noticer
# Needed by: OS:LOGONMSG, OS:GLOBAL
# Requires: OperServ
# Format: GlobalNoticer NickName UserName HostName Modes RealName
# Mode suggestions:
#      UnrealIRCd: +ioS
#      Other     : +io
# Note: The GlobalNoticer must always be +o to function.
# Comment to disable.

GlobalNoticer Global service $DOMAIN +io Global Noticer

# SpamServ Information - Spam Detection Bot
# Format: SpamService NickPrefix UserName HostName RealName
# Note: Unlike other pseudo-clients, SpamServ has no modes and
# makes its own nickname. It's suggested you set it's information
# to something 'natural' so it looks like a real client and not a bot.
# The NickPrefix will have a 5 digit number appended to it. For
# example, setting it to Guest might result in Guest48937.
# Comment to disable.

SpamService Guest ~me dial-102.$DOMAIN hi

# Define your SRAs here. Use one line per SRA. The format is:
# SRA nickname password
# Any opered user can authenticate as an SRA, so you could define
# one SRA to use as a role nick.
# At least one SRA is required.

SRA $SRANICK $SRAPASS

# Do you want OperStats to send out a GlobOps when an SRA AUTHs? Some
# people like it, others don't, it's up to you. Note that OperStats will
# always globop failed auth attempts.
# Comment to disable.

GlobalOnAuth

# What is the name of your network? This can be up to 32 characters in length.
# This is needed for certain functions of services. Ideally you would put
# something here like 'ExampleNet'.
# This is required.

NetworkName $NETWORK

# Enter your desired flood trigger here. This tells OperStats how many
# messages in how much time to consider a flood. If it determines that
# a user is flooding, it will ignore them twice, then it will temporarily
# AKill them for 30 minutes. This does NOT detect your average flood. This
# only applies to users communicating with OperStats. The first number
# controls how many messages may be recieved, and the second number is
# how long before the counter resets. For example, 7 10s would take action
# against users who sent 7 messages to OperStats in 10 seconds.
# Comment to disable.

FloodTrigger 7 10s

# If you want Services to send GlobOps about people who are flooding,
# define this. Otherwise it will all be done silently.
# Comment to disable.

NoisyFlood

# TLD: Define your network's top level domains. This is nessecary
# for the global noticing functions. This should be a space seperated
# list of TLDs, not including the leading period. For example:
# TLD net org com ca us au
# Needed by: GlobalNoticer

TLD net org com

# Select the type of helpfile indexes you want to use. You have
# three choices here:
#   Short : Only the commands are shown, no descriptions.
#   Medium: Some important commands have descriptions, the rest don't.
#   Long  : Every command has a description.
# If this is not set, it will default to Medium.

HelpIndex Medium

# How often should we write the databases to disk? If you are on a
# large network, it may be to your advantage to raise this slightly;
# but beware that the higher this is, the more data you risk losing in
# the event of an unplanned shutdown.
# This is required.

SyncTime 5m

# Do you want to log the DataBase Syncs? Normally you won't need this;
# it'll just cause excessive logging. If you think some of your DB entries
# might be causing crashes, turn this on to see if it crashes during an
# update.
# Comment to disable.

#LogUpdates

# If we terminate for any reason, should we try to reconnect to the server?
# If so, how long should we wait? A good value would be 30 seconds.
# Minimum value is 5s.
# Comment to disable.

WaitRestart 30s

# Does your IRCd have /*Serv commands, such as /StatServ? If this is on,
# OperStats will tell users to use these aliases, rather than /MSG. This
# also assumes that your IRCd has a complete set of /*Serv commands. A
# complete set contains /StatServ and /OperServ.
# Comment to disable.

#HaveServ

# Should we output various mundane information to a channel? If so, where?
# Comment to disable.

#SnoopChan #Snoop

# Should the pseudo clients join a channel? This could be the same as your
# Snoop channel, or something different. They'll join this channel and op
# themselves, nothing more.
# Comment to disable.

#JoinChan #Services

# If you want OperStats to communicate with Cygnus, configure this line.
# OperStats will send special messages to this server on certain events
# to communicate with Cygnus. This doesn't work with any other services.
# Comment to disable.

#CygnusServer services.$DOMAIN

# If you want OperStats to share AutoKills with Cygnus, enable this.
# OperStats will inform Cygnus when AKills are added and deleted, and
# will add and remove AKills from Cygnus as well.
# Requires: CygnusServer
# Comment to disable.

ShareAKills

# If you want OperStats to share SRAs with Cygnus, enable this.
# OperStats will inform Cygnus when SRAs auth and deauth, and will also
# give and take SRA access through Cygnus as well. This would allow you
# to receive SRA access on Cygnus and OperStats by identifying for your
# nickname, if you wish.
# Requires: CygnusServer
# Comment to disable.

ShareSRAs

# Security Setting: How much security should we use in user communication?
# This defines what messages OperStats requires to be secure. Secure
# communication must be sent to the OperStats server, for example
# /msg StatServ@$INFONAME
# This can be set one of three ways:
#     All       - Total security. All communication must be secure.
#     Passwords - Only password commands require security, such as IDENTIFY.
#     None      - Users can MSG Services any way they want.
# Comment to disable.

SecuritySetting Passwords

# List any servers that are U:Lined on your network besides OperStats.
# These servers are hidden from SS MAP. Use one line per server.

#ULine services.$DOMAIN

# If you want OperStats to ignore U:Lined servers completely, turn this
# option on. This will hide servers defined above, as well as OperStats
# itself, from user and server counts, as well as from commands such as
# SERVERS.
# Comment to disable.

#IgnoreULines

# How many non * and ? characters should we require for AutoKills? This helps
# to prevent against wildcard AKills that affect most of the network. It is
# recommended this be at LEAST 2. It should be no more than 5, or some
# perfectly valid AKills may not be settable. For example: A setting of 2
# would not allow an AKill on *@*.m?, but would allow an AKill on *@*.my.
# Needed by: OperServ

NonWildCards 5

# How long should the default AKill expire time be?
# Needed by: OperServ

AKillExpire 1w

# What reasons should we use for AKills if none is specified?
# Needed by: OperServ

DefaultAKillReason You are banned.

# Do you want OperServ to send a GlobOps when an AKill is placed?
# Comment to disable.

GlobalOnAKill

# What is the default number of allowed connections from a single host?
# If a connecting client would make the number of connections from that
# host exceed this, the new connection will be killed. For example, if
# this was 5, each user would be allowed 5 connections. If a 6th connected,
# it would be killed. If this is not set, there will be no clone detection.
#
# If you give a time, OperStats will make AKills for cloning last for this
# time. If you don't give a time, OperStats will use the default AKill
# expiry time.
#
# IMPORTANT: If you turn this on, make sure you set a Trigger or an
# Exception for any other services and hostnames that exceed this number!
# Otherwise OperStats will kill those hosts upon linking. You've been
# warned.
#
# Format:
# MaxConnections Limit AKill-Time
# Comment to disable.

#MaxConnections 5 24h

# How many times should we allow a host to be killed for cloning before
# we AKill it? This is useful for clone attacks. If this is not set, OperServ
# will kill the offending clones continuously.
# Requires: MaxConnections
# Comment to disable.

#MaxCloneKills 5

# Should OperServ send a globops if the number of users on the network
# increases rapidly? This may indicate an impending clone attack. If this
# is set, OperServ will send a globops if the number of users on the network
# increases by this amount in 30 seconds.
# Comment to disable.

WarnClients 50

# Define any Triggers you want here. This allows you to stop OperStats from
# killing certain hosts when they first link, before you have a chance to
# add a Trigger through OperServ. Use one line per trigger. The format is:
# Trigger user@host limit
# Once OperStats updates the DBs, these Triggers will be stored there.
# Triggers in the DBs override Triggers in the conf, so you will need to
# change the Trigger through OperServ to change the limit. (Do this by
# simply adding it with the TRIGGER command.)

#Trigger *@$DOMAIN 500

# Should we completely exempt certain hosts from clone detection? If so,
# define those hosts here. These hosts will be immune to all clone
# detection. Triggers set for excepted hosts will have no effect. Use one
# line per exception. The format is:
# Exception user@host

#Exception *@$DOMAIN

# Do you want StatServ to send a GlobOps if this lag threshold is reached
# for a server?

GlobalOnLag

# Do you want StatServ to send a GlobOps when a new peak NetSpeed is
# reached? This could help to indicate an oncoming attack.

GlobalOnSpeed

# If you want all global notices sent with OS GLOBAL to be tagged with
# a message, enter that message below. [Netwide Notice] might not be a
# bad choice. This setting has no effect on global notice with a specified
# destination.
# Comment to disable.

GlobalMSG [Netwide Notice]

# Should we date-stamp new LogonMSGs? If this is enabled, LogonMSGs will be
# stamped with the date like so: [News - January 1, 1970]
# Comment to disable.

StampLogons

# How often should we PING the servers on the network to
# keep an up to date lag count? Note that under most circumstances,
# the servers on your network will be PINGed virtually at the same time,
# and this may cause a slight lag spike.
# Needed by: StatServ

PingDelay 1m

# What should a servers ping time be before it's considered lagged? This must
# be at least 1 second. If this is not set or is less than 1 second, it will
# default to 20 seconds.
# Needed by: StatServ

LagThreshold 20s

# Do you want OperStats to generate a channels.php file with a list of current
# channels, users and topics on those channels, and optionally, chat links?
# See the doc/web.doc file for more information on this feature.
# Comment to disable.

#DumpChans

# Do you want to make use of Java Links? If you enable this, links to channels
# in the channels.php file will have Java Links beside them. The parameter
# for this option is where an image for the Java Links will be. A .gif file
# is included in the dist/imgs directory.
# See the doc/web.doc file for more information on this feature.
# Format: JavaClient imgs/java.gif
# Requires: DumpChans
# Comment to disable.

#JavaClient imgs/java.gif

# Do you want to make use of mIRCLinks? If you enable this, links to channels
# in the channels.php file will have mIRCLinks beside them. To use this
# properly, you need to put your IRC network's address and a port here.
# The second parameter of this line is where (in relation to where
# channels.html will be) an image for the mIRCLink is. A .gif file
# is included in the dist/imgs directory.
# See the doc/web.doc file for more information on this feature.
# Format: mIRCLink irc.example.net:6667 imgs/mirc.gif
# Requires: DumpChans
# Comment to disable.

#mIRCLink irc.$DOMAIN:6667 imgs/mirc.gif

# Do you want OperStats to generate a servers.php file with current server
# statistics? The output is similar to SS SERVERS and SS STATS SERVER.
# See the doc/web.doc file for more information on this feature.
# Comment to disable.

#DumpServers

# Do you want OperStats to generate a stats.php file with current network
# stats? This file will contain current/max user, channel, and server counts,
# the current network speed, the hit count for the day, and server uptimes.
# See dist/statsexample.php for a small example of these stats in use.
# See the doc/web.doc file for more information on this feature.
# Comment to disable.

#DumpStats

# Do you want OperStats to generate a tldstats.php file with bar graphs
# for the TLD hit percentages for the day? This file will be updated
# with each DB update, and the hit counts will be reset at midnight.
# See the doc/web.doc file for more information on this feature.

#DumpTLDs

# If you want OperStats to put a logo at the top of generated HTML files,
# enter the path to that logo (in relation to where the html files will be).
# The keen OperStats logo is included.
# Comment to disable.

LogoImage imgs/ologo.png

# That's it. Most options in this file can be updated through the REHASH
# function and don't require a restart. If your changes don't get done
# with REHASH, you will need to use RESTART to have them take effect.
_EOT_
echo "done."

# Move the conf to the data directory if it exists.
if [ -d $DEST ] ; then
    echo2 'Moving conf... '
    mv operstats.conf $DEST
    echo 'done.'
    echo ' '
    echo '*** Now configure your server(s), and run OperStats.'
    echo '*** Please read the documentation before seeking help. Thanks.'
    echo ' '
else
    echo ' '
    echo '*** Now move the conf to the directory you installed'
    echo '*** OperStats in, configure your server(s), and run OperStats.'
    echo '*** Please read the documentation before seeking help. Thanks.'
    echo ' '
fi

# Now cache it all for a next run
cat <<EOT >../makeconf.cache
DOMAIN=$DOMAIN
INFONAME=$INFONAME
CONNECTIP=$CONNECTIP
CONNECTPASS=$CONNECTPASS
CONNECTPORT=$CONNECTPORT
UNREALNUMERIC=$UNREALNUMERIC
IRCDTYPE=$IRCDTYPE
SRANICK=$SRANICK
SRAPASS=$SRAPASS
NETWORK=$NETWORK
IRCD=$IRCD
EOT
exit 0
