#!/bin/sh
# Simple home brew configure script, not as fancy as GNU Autotools
#
#     --with-plugins="time initctl"
#     --enable-static
#     --disable-inetd
#     --disable-plugins
#
# This creates a configuration where Finit is built statically, with the
# time and initctl plugins built-in, and no inetd support.
#

# Setup defaults if undefined
defaults()
{
        heading=${heading:='"Finit v" VERSION'}
        prefix=${prefix:="/usr"}
        sysconfdir=${sysconfdir:="/etc"}
        sbindir=${sbindir:="/sbin"}
        libdir=${libdir:="/lib"}
        pdir="finit/plugins"
        plugindir=${plugindir:="$libdir/$pdir"}
        # The initctl FIFO, should probably be in /run, not /dev
        fifo=${fifo:="/dev/initctl"}
        random=${random:="/var/lib/urandom/random-seed"}
        conf="finit.conf"
        config=${config:="$sysconfdir/$conf"}
        confdir=${confdir:="finit.d"}
        rcsd=${rcsd:="$sysconfdir/$confdir"}
        console=${console:='_PATH_CONSOLE'}
        shell=${shell:='_PATH_BSHELL'}
        defuser=${defuser:="root"}
        defhost=${defhost:="noname"}
        runlevel=${runlevel:=2}
        debug=${debug:=0}
        embedded=${embedded:=0}
        dbus=${dbus:=0}
        remount=${remount:=0}
        inetd=${inetd:=1}
        verbose=${verbose:=1}
        kernel_quiet=${kernel_quiet:=1}
        static=${static:=0}
        disable_plugins=${disable_plugins:=0}
}

result()
{
        echo
        echo "Finit configuration"
        echo "========================================================================"
        echo "Heading           : $heading"
        echo "Install prefix    : $prefix"
        echo "System config dir : $sysconfdir"
        echo "Super user bindir : $sbindir"
        echo "Shared libs dir   : $libdir"
        echo "Plugins dir       : $plugindir"
        echo "FIFO              : $fifo"
        echo "Config file       : $config"
        echo "rcs.d/ directory  : $rcsd"
        echo "Default console   : $console"
#        echo "Fallback shell    : $shell"
        if [ x"$sysroot" != x"" ]; then
                echo "Swap rootfs to    : $sysroot"
        fi
        echo "Default username  : $defuser"
        echo "Default hostname  : $defhost"
        echo "Default runlevel  : $runlevel"

        if [ $debug -ne 0 ]; then
                echo "Debug             : ENABLED"
        fi

        if [ $embedded -ne 0 ]; then
                echo "Embedded target   : YES (BusyBox getty, and mdev instead of udev)"
        fi

        if [ $dbus -ne 0 ]; then
                echo "D-Bus             : YES, remember to install dbus.so plugin as well!"
        fi

        if [ $remount -ne 0 ]; then
                echo "Remount ROOT fs   : YES, / will be remounted as read-write at bootstrap"
        fi

        if [ x"$random" != x"" ]; then
                echo "Random seed       : $random"
        fi

        if [ $inetd -ne 0 ]; then
                echo "Inetd             : Built-in"
        else
                echo "Inetd             : DISABLED"
        fi

	if [ x"$libite" = x"" ]; then
                echo "Libite (LITE)     : Built-in"
	else
                echo "Libite (LITE)     : $libite + {/lib, /include}"
	fi

	if [ x"$libuev" = x"" ]; then
                echo "LibuEv            : Built-in"
	else
                echo "LibuEv            : $libuev + {/lib, /include}"
	fi

        if [ $verbose -ne 1 ]; then
                echo "Quiet Finit       : YES"
        else
                echo "Quiet Finit       : NO, verbose output (default)"
        fi

        if [ $kernel_quiet -eq 1 ]; then
                echo "Kernel Quiet      : YES, silent after inital boot (default)"
        else
                echo "Kernel Quiet      : NO"
        fi

        if [ $static -ne 0 ]; then
                echo "Static Finit      : YES"
        fi

        if [ $disable_plugins -eq 1 ]; then
                echo "Plugins           : DISABLED"
        else
                if [ x"$plugins" = x"" ]; then
                        echo "Plugins           : ALL"
                else
                        echo "Plugins           : $plugins"
                fi
        fi
        echo
}

usage()
{
        defaults

        echo "Run this script to configure finit for your operating system."
        echo
        echo "Usage: $0 [ARG]"
        echo
        echo "  --help                 This help text"
        echo "  --prefix=PREFIX        Set installation prefix, default '$prefix'"
        echo "  --sysconfdir=CONF      Set conf dir, default '$sysconfdir'"
        echo "  --sbindir=SBIN         Set sbin dir, default '$sbindir'"
        echo "  --libdir=LIB           Set lib dir, default '$libdir'"
        echo "  --plugindir=DIR        Set plugin dir, default '\$libdir/$pdir'"
        echo
        echo "  --with-fifo=FIFO       FIFO for initctl plugin, default '$fifo'"
        echo "  --with-config=CONFIG   Finit CONFIG file, default '\$sysconfdir/$conf'"
        echo "  --with-heading=GREET   Display GREET instead of Finit version at boot"
        echo "  --with-randomseed=SEED Seed /dev/urandom at boot from SEED.  Usually found in"
        echo "                         standard Linux systems at $random"
        echo "                         which is also the default.  Requires urandom plugin!"
        echo "  --with-plugins=\"a b c\" Build plugins 'a', 'b' and 'c', either as dynamic .so"
        echo "                         files, or as built-in if --enable-static is given,"
        echo "                         defaults to all plugins.  Exclude extension!"
        echo "  --with-rcsd=RCSD       Finit RCSD directory, default '\$sysconfdir/$confdir'"
        echo "  --with-console=DEV     Console to use, default: /dev/console"
#        echo "  --with-shell=PATH      Fallback shell to use, default: /bin/sh"
        echo "  --with-sysroot=PATH    Some distributions keep a different (writable) rootfs"
        echo "                         that is used at runtime, usually /sysroot or /mnt"
        echo "  --with-username=USER   User for 'startx' .conf option, default: root"
        echo "  --with-hostname=NAME   Hostname, if /etc/hostname is missing, default: noname"
        echo "  --with-runlevel=LEVEL  Runlevel to switch to after bootstrap (S), default: 2"
        echo "  --with-libite=PATH     Look for libite in PATH/include and PATH/lib"
        echo "                         This is for disabling the use of built-in libite."
        echo "  --with-libuev=PATH     Look for libuEv in PATH/include and PATH/lib"
        echo "                         This is for disabling the use of built-in libuEv."
        echo
        echo "  --enable-embedded      Embedded defaults, BusBox getty, mdev etc."
        echo "  --enable-dbus          Enable D-Bus plugin.  Note: install plugin as well!"
        echo "  --enable-rw-rootfs     Remount / as read-write at bootstrap, not for embedded"
        echo "  --enable-debug         Enable debug flags, '-O0, -g'"
        echo "  --enable-quiet         Quiet mode, reduce screen output to absolute minimum"
        echo "  --enable-static        Link statically.  This disables a few features."
        echo "  --disable-inetd        Disable inetd support if not needed."
        echo "  --disable-kernel-quiet Disable kernel cmdline quiet, default enabled."
        echo "  --disable-plugins      Disable plugins"
        echo
        echo "The build system also supports external CFLAGS, CPPFLAGS, LDFLAGS, LDLIBS,"
        echo "and DESTDIR, useful for packaging or cross-compiling."
        echo
        echo "Example:"
        echo "         ./configure --enable-embedded --with-plugins=\"initctl tty\""
        echo
        echo "Report bugs to https://github.com/troglobit/finit/issues"
        echo

        exit 1
}

while [ "$*" != "" ]; do
        opt=`expr "$1" : "--\([^=]*\)=.*"`
        arg=`expr "$1" : "--[^=]*=\(.*\)"`
        if [ -z "$opt" ]; then
                opt=`expr "$1" : "--\(.*\)"`
        fi
        shift

        case $opt in
                prefix)
                        prefix="$arg"
                        ;;

                sysconfdir)
                        sysconfdir="$arg"
                        ;;

                sbindir)
                        sbindir="$arg"
                        ;;

                libdir)
                        libdir="$arg"
                        ;;

                plugindir)
                        plugindir="$arg"
                        ;;

                with-fifo)
                        fifo="$arg"
                        ;;

                with-config)
                        config="$arg"
                        ;;

                with-heading)
                        heading="\"$arg\""
                        ;;

                with-randomseed)
                        random="$arg"
                        ;;

                with-plugins)
                        plugins="$arg"
                        ;;

                with-rcsd)
                        rcsd="$arg"
                        ;;

                with-console)
                        console="\"$arg\""
                        ;;

                with-shell)
                        shell="\"$arg\""
                        ;;

                with-sysroot)
                        sysroot="$arg"
                        ;;

                with-usename)
                        defuser="$arg"
                        ;;

                with-hostname)
                        defhost="$arg"
                        ;;

                with-runlevel)
                        runlevel="$arg"
                        ;;

                with-libite)
                        libite="$arg"
                        ;;

                with-libuev)
                        libuev="$arg"
                        ;;

                enable-debug)
                        debug=1
                        ;;

                enable-embedded)
                        embedded=1
                        ;;

                enable-dbus)
                        dbus=1
                        ;;

                enable-rw-rootfs)
                        remount=1
                        ;;

                enable-quiet)
                        verbose=0
                        ;;

                enable-static)
                        static=1
                        ;;

                disable-inetd)
                        inetd=0
                        ;;

                disable-kernel-quiet)
                        kernel_quiet=0
                        ;;

                disable-plugins)
                        disable_plugins=1
                        ;;

                help)
                        usage
                        ;;

                *)
                        echo "Unknown option: $opt"
                        usage
                        ;;
        esac
done

# If no arguments given to configure script, use defaults
defaults
result

##############################################################################
echo "# Generated by finit configure script."                      > config.mk
echo "prefix      = $prefix"                                      >> config.mk
echo "sysconfdir  = $sysconfdir"                                  >> config.mk
echo "sbindir     = $sbindir"                                     >> config.mk
echo "libdir      = $libdir"                                      >> config.mk
echo "plugindir   = $plugindir"                                   >> config.mk
echo "incdir      = \$(prefix)/include/finit"                     >> config.mk
echo "datadir     = \$(prefix)/share/doc/finit"                   >> config.mk
echo "mandir      = \$(prefix)/share/man/man8"                    >> config.mk
echo "FINIT_RCSD  = $rcsd"                                        >> config.mk

if [ $inetd -eq 0 ]; then
        echo                                                      >> config.mk
        echo "OBJS       := \$(filter-out inetd.o, \$(OBJS))"     >> config.mk
fi

echo                                                              >> config.mk
if [ $debug -eq 1 ]; then
        echo "CFLAGS     += -O0 -g"                               >> config.mk
else
        echo "CFLAGS     += -O2"                                  >> config.mk
fi

echo                                                              >> config.mk
if [ $static -ne 0 ]; then
        echo "STATIC      = 1"                                    >> config.mk
        echo "LDFLAGS    += -static"                              >> config.mk
        echo "CPPFLAGS   += -DENABLE_STATIC"                      >> config.mk
        echo "PLUGINS     = \"\""                                 >> config.mk
        if [ $disable_plugins -ne 1 ]; then
                if [ x"$plugins" != x"" ]; then
                        echo "OBJS       += \$(addsuffix .o, \$(addprefix plugins/, $plugins))" >> config.mk
                else
                        echo "tmp        += \$(wildcard plugins/*.c)"     >> config.mk
                        echo "OBJS       += \$(tmp:.c=.o)"                >> config.mk
                fi
        fi
else
        echo "STATIC      = 0"                                    >> config.mk
        echo "LDFLAGS    += -rdynamic"                            >> config.mk
        echo "LDLIBS     += -ldl"                                 >> config.mk
        if [ x"$plugins" != x"" ]; then
                echo "PLUGINS     = \$(addsuffix .so, $plugins)"  >> config.mk
        fi
fi

if [ x"$libite" != x"" ]; then
        echo "LIBITE      = $libite"                              >> config.mk
        echo "CPPFLAGS   += -I\$(LIBITE)/include"                 >> config.mk
        echo "LDFLAGS    += -L\$(LIBITE)/lib"                     >> config.mk
        echo "LDLIBS     += -l:libite.so.2"                       >> config.mk
else
        echo "LDFLAGS    += -L\$(TOPDIR)/libite"                  >> config.mk
        echo "LDLIBS     += -lite"                                >> config.mk
        echo "DEPLIBS    += libite/libite.a"                      >> config.mk
fi

if [ x"$libuev" != x"" ]; then
        echo "LIBUEV      = $libuev"                              >> config.mk
        echo "CPPFLAGS   += -I\$(LIBUEV)/include"                 >> config.mk
        echo "LDFLAGS    += -L\$(LIBUEV)/lib"                     >> config.mk
        echo "LDLIBS     += -l:libuev.so.1"                       >> config.mk
else
        echo "LDFLAGS    += -L\$(TOPDIR)/libuev"                  >> config.mk
        echo "LDLIBS     += -luev"                                >> config.mk
        echo "DEPLIBS    += libuev/libuev.a"                      >> config.mk
fi

echo                                                              >> config.mk
echo "export libdir plugindir incdir"                             >> config.mk
echo "export CFLAGS CPPFLAGS LDFLAGS LDLIBS STATIC"               >> config.mk

##############################################################################
echo "/* Generated by finit configure script. */"    > config.h
echo                                                >> config.h
echo "#ifndef FINIT_CONFIG_H_"                      >> config.h
echo "#define FINIT_CONFIG_H_"                      >> config.h
echo                                                >> config.h
echo "#define INIT_HEADING    $heading"             >> config.h
echo "#define VERBOSE_MODE    $verbose"             >> config.h
echo "#define KERNEL_QUIET    $kernel_quiet"        >> config.h
if [ $embedded -eq 1 ]; then
        echo "#define EMBEDDED_SYSTEM 1"                        >> config.h
        echo "#define SETUP_DEVFS     \"/sbin/mdev -s\""        >> config.h
        echo "#define GETTY           \"/sbin/getty -L\""       >> config.h
        echo "#define GETTY_BUSYBOX   1"                        >> config.h
        echo "#define BAUDRATE        115200"                   >> config.h
else
        echo "#define SETUP_DEVFS     \"/sbin/udevd --daemon\"" >> config.h
        echo "#define GETTY           \"/sbin/getty -8\""       >> config.h
        echo "#define GETTY_AGETTY    1"                        >> config.h
        echo "#define BAUDRATE        38400"                    >> config.h
fi
if [ $remount -eq 1 ]; then
        echo "#define REMOUNT_ROOTFS  1"            >> config.h
fi
if [ $dbus -eq 1 ]; then
        echo "#define HAVE_DBUS       1"            >> config.h
fi

if [ $inetd -eq 0 ]; then
        echo "#define INETD_DISABLED"               >> config.h
fi
echo "#define FINIT_FIFO      \"$fifo\""            >> config.h
echo "#define FINIT_CONF      \"$config\""          >> config.h
echo "#define FINIT_RCSD      \"$rcsd\""            >> config.h
echo "#define CONSOLE         $console"             >> config.h
echo "#define FALLBACK_SHELL  $shell"               >> config.h
if [ x"$sysroot" != x"" ]; then
        echo "#define SYSROOT         \"$sysroot\"" >> config.h
fi
echo "#define PLUGIN_PATH     \"$plugindir\""       >> config.h
if [ x"$random" != x"" ]; then
        echo "#define RANDOMSEED      \"$random\""  >> config.h
fi
echo "#define DEFUSER         \"$defuser\""         >> config.h
echo "#define DEFHOST         \"$defhost\""         >> config.h
echo "#define RUNLEVEL        $runlevel"            >> config.h

echo >> config.h
echo "#endif /* FINIT_CONFIG_H_ */"                 >> config.h
