# SliTaz package receipt.

PACKAGE="busybox"
VERSION="1.31.1"
CATEGORY="base-system"
SHORT_DESC="Busybox combines tiny versions of many common UNIX utilities."
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="GPL2"
TARBALL="$PACKAGE-$VERSION.tar.bz2"
WEB_SITE="http://www.busybox.net/"
WGET_URL="http://www.busybox.net/downloads/$TARBALL"

DEPENDS="slitaz-base-files glibc-base ncurses-common"
BUILD_DEPENDS="bzip2 libtirpc-dev pam-dev"

CONFIG_FILES="/etc/dnsd.conf /etc/inetd.conf /etc/udhcpd.conf \
/etc/resolv.conf /etc/httpd.conf"

# Busybox patches
apply_bb_patchs()
{
    while read file; do
    	[ -f done.$file ] && continue
    	echo "Apply $file..."
    	patch -p1 < $stuff/patches/$PACKAGE-${VERSION%.*}-$file || return 1
	touch done.$file
    done <<EOT
tar.u
stat.u
ris.u
zmodules.u
cmdline.u
diff.u
diet.u
losetup.u
fbvnc.u
cpio.u
shutdown.u
scriptreplay.u
mkfs_vfat.u
ash.u
EOT
    cp $stuff/$PACKAGE-${VERSION%.*}.config .config
    cp $stuff/$PACKAGE-${VERSION%.*}.config .
}

# Rules to configure and make the package.
compile_rules()
{
    apply_bb_patchs &&
    patch -p1 < $stuff/patches/$PACKAGE-${VERSION%.*}-0003-libbb-reduce-the-overhead-of-single-parameter-bb_err.patch &&
    patch -p1 < $stuff/patches/$PACKAGE-${VERSION%.*}-0004-Remove-stime-function-calls.patch &&
    make oldconfig &&
    make && make install || return 1
    strip --strip-unneeded -R .eh_frame -R .eh_frame_hdr $src/_install/bin/busybox

    # prepare busybox-pam package
    sed -i 's/# CONFIG_PAM is not set/CONFIG_PAM=y/' .config
    make oldconfig && make || return 1
    mv busybox busybox-pam

    # prepare busybox-static package
    #cp $stuff/$PACKAGE-${VERSION%.*}.config-static .config
    #make oldconfig && make || return 1
    #mv busybox busybox-static

    # prepare ssfs-busybox package
    #rootfs=$src/ssfs-busybox/usr/share/ssfs/rootfs
    #mkdir -p $rootfs/etc
    #cp $stuff/$PACKAGE-${VERSION%.*}.config-ssfs .config
    #make oldconfig && make busybox &&
    #make CONFIG_PREFIX=$rootfs install || return 1
    #cp $stuff/busybox.conf-ssfs $rootfs/etc/busybox.conf
    #chown -R 0.0 $rootfs/etc
    #chmod 0600 $rootfs/etc/busybox.conf
    #chmod 4755 $rootfs/bin/busybox
}


# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
    cp -a $src/_install/* $fs
    rm -f $fs/bin/bbconfig $fs/usr/bin/ar
    mkdir -p $fs/etc/init.d $fs/var/spool/cron/crontabs $fs/var/spool/lpd

    # Busybox config files.
    for f in busybox.conf dnsd.conf udhcpd.conf inetd.conf httpd.conf \
        syslog.conf zcip.script
    do
        cp $stuff/$f $fs/etc
    done
    chown -R 0.0 $fs/etc
    chmod 600 $fs/etc/busybox.conf
    touch $fs/etc/resolv.conf

    # Daemon scripts.
    cp $stuff/daemon $fs/etc/init.d
    DAEMON="crond dnsd ftpd httpd inetd lpd klogd ntpd syslogd telnetd tftpd udhcpd zcip"
    for i in $DAEMON; do
        grep -qi config_$i=y $stuff/$PACKAGE-${VERSION%.*}.config &&
        ln -s daemon $fs/etc/init.d/$i
    done
    rm $fs/linuxrc
    mkdir -p $fs/etc/modprobe.d

    # Udhcpc stuff.
    mkdir -p $fs/usr/share/udhcpc
    cp $stuff/udhcp.script $fs/usr/share/udhcpc/default.script
    chmod +x $fs/usr/share/udhcpc/default.script

    # Httpd stuff.
    cp $stuff/httpd_helper.sh $fs/usr/bin
    chmod +x $fs/usr/bin/httpd_helper.sh
    cp -a $stuff/www $fs/var
}

# GNU utils stuff.
pre_install()
{
    local i
    cp -a /etc/resolv.conf /etc/resolv.conf-busybox-install
    answer=""
    for i in $(cat $1$INSTALLED/$PACKAGE/files.list); do
        [ -f $1$i ] || continue
        case "$i" in
        /bin/busybox) continue ;;
        *bin/*) ;;
        *) continue ;;
        esac
        if [ -z "$answer" ]; then
            echo -n "Keep installed GNU utilities ? "
            read -t 30 answer	# by default: keep
            case "$answer" in
            n*|N*) break;;
            *) answer="Y";;
            esac
        fi
        cp -a $1$i $1$i-busybox-install
    done
}

post_install()
{
    local i
    [ -f /etc/resolv.conf-busybox-install ] &&
    mv -f /etc/resolv.conf-busybox-install /etc/resolv.conf
    while read i ; do
        [ -f $1$i-busybox-install ] || continue
        mv $1$i-busybox-install $1$i
    done < $1$INSTALLED/$PACKAGE/files.list
    chmod 4755 $1/bin/busybox

    # /etc/daemons.conf (tftp + dnsd + httpd may not be present)
    if ! grep -q ^DNSD_OPTIONS $root/etc/daemons.conf; then
        echo '# Domain name server options.' >> $root/etc/daemons.conf
        echo 'DNSD_OPTIONS="-d"' >> $root/etc/daemons.conf
        echo '' >> $root/etc/daemons.conf
    fi
    if ! grep -q ^TFTPD_OPTIONS $root/etc/daemons.conf; then
        echo '# Tftp daemon options.' >> $root/etc/daemons.conf
        echo 'TFTPD_OPTIONS="-r /boot"' >> $root/etc/daemons.conf
        echo '' >> $root/etc/daemons.conf
    fi
    sed -i "s/copy; 20../copy; $(date +%Y)/" $root/var/www/httpd/404.html
}

pre_remove()
{
    # We can not remove this package !
    exit 1
}