
if [ -f /var/lib/dpkg/info/apt-cacher-ng.list ]
then
    export http_proxy=http://127.0.0.1:3142
    export https_proxy=$http_proxy
    echo http_proxy is set to $http_proxy
else
    echo No apt-cacher-ng installed, downloads will take ages...
fi

are_you_root ()
{
if [ ! "$(/usr/bin/whoami)" = "root" ]
then
    cat << AREWEROOT

    Sorry, only root is allowed to do this.
    Alternatively, try 'sudo $0'.

AREWEROOT
    exit 0
fi
}

check_build_target ()
{
if [ "$#" = "0" ]
then
    ARCHLIST="$(dpkg --print-architecture)"
    cat << EOFARCH

    No build target arguments were specified, thus
    only building for system architecture "${ARCHLIST}".

    To build another (or multiple) architectures, 
    please use one (or both) of these arguments:

    $0 amd64 i386
EOFARCH
else
    ARCHLIST="$@"
fi
cat << EOFARGS

    Building following architecture(s): ${ARCHLIST}

EOFARGS
for ARCH in ${ARCHLIST}
do
   if [ ! -d wmlive-$ARCH ]
   then
       cat << EOFNOARCH
    Target directory "wmlive-$ARCH" does not exist.
    Please verify for typing errors and try again.

EOFNOARCH
   exit 1
   fi
done
}

check_live_build_version ()
{
LIVEBUILDVERSION="4.0.3-1"

if [ "$(dpkg -l live-build | grep -c ^i)" = "1" ] && [ "$(dpkg -l live-build | grep ^i | awk '{print $3}')" != "$LIVEBUILDVERSION" ]
then
    cat << VERSIONCHECK

    This wmlive build tree is tailored for live-build version $LIVEBUILDVERSION,
    but the installed live-build version has changed to $(dpkg -l live-build | grep ^i | awk '{print $3}').

    So better first check if our script assumptions still match it.

    Once verified everything works, just increment the LIVEBUILDVERSION
    value defined in $(pwd)/build_env and all related 
    */apt/preferences files of the build tree.

VERSIONCHECK
    exit 1
elif [ "$(dpkg -l live-build | grep -c ^i)" = "0" ]
then
    cat << LBINSTCHECK

    Cannot proceed because the 'live-build' package is not installed.

    You can find deb binaries of all required live-* packages either
    in the /usr/local/src/wmlive/live-pkgs directory of your system,
    or in the /pool directory on the wmlive CD.

LBINSTCHECK
    exit 1
fi
}

#
# procedures to create a backup of the wmlive build tree
#

mkwmlive_backup ()
{
TODAY=$(date +%Y-%m-%d)
WMLIVEBUILDIR=../wmlive-desktop
WMLIVEBKPDIR=../backup.d

echo "Cleaning up first..."
echo ""

for WMLIVEDIR in wmlive-amd64 wmlive-i386
do
    [ -d $WMLIVEDIR ] && cd $WMLIVEDIR && lb clean
    cd -
    rm -f $WMLIVEDIR/build*.log
    rm -f $WMLIVEDIR/config/build
done

if [ ! -d "$WMLIVEBKPDIR" ]
then
    mkdir -p "$WMLIVEBKPDIR"
fi

if [ -d "$WMLIVEBUILDIR" ] && [ -d "$WMLIVEBKPDIR" ]
then
    rm -vf $WMLIVEBUILDIR/build-config/includes.binary/*/sources/wmlive-desktop_*.tar.xz \
    $WMLIVEBUILDIR/build-config/binary_debian-installer/additions.tar.gz

    # to only remove directories, make sure to add trailing slash:
    rm -rfv $WMLIVEBUILDIR/build-config/includes.binary/*/custom/*/

    BKPTIME=$(date +%Y-%m-%d-%H%M)
    WMLIVEBKPTXZ=wmlive-desktop_"$BKPTIME".tar.xz
    WMLIVEBACKUP="$WMLIVEBKPDIR"/"$WMLIVEBKPTXZ"

    echo ""
    echo "Creating $WMLIVEBACKUP ..."
    echo ""

    tar cJf "$WMLIVEBACKUP" --exclude="wmlive-desktop/wmlive-*/cache/*" --exclude="wmlive-desktop/wmlive-*/.build" \
       --exclude="wmlive-desktop/wmlive-*/config/includes.binary/*/*.tar.*" \
        --exclude="wmlive-desktop/build-config/wmlive-opt/linux-*/*" --exclude="wmlive-desktop/mk*_*.log" \
        --exclude="wmlive-desktop/*diff" --exclude="wmlive-desktop/build-config/includes.chroot/opt/*" \
        --exclude="wmlive-desktop/build-config/packages.*/*/*deb" "$WMLIVEBUILDIR"

    if [ -s "$WMLIVEBACKUP" ]
    then
        echo ""
        echo "Backed up $WMLIVEBUILDIR to $WMLIVEBACKUP:"
        echo ""
        ls -l "$WMLIVEBACKUP"
        echo ""
    fi
else
    echo "Are you in the correct location?"
fi
}

mkwmlive_backup_full ()
{
TODAY=$(date +%Y-%m-%d)
WMLIVEBUILDIR=../wmlive-desktop
WMLIVEBKPDIR=../backup.d

for WMLIVEDIR in wmlive-amd64 wmlive-i386
do
    [ -d $WMLIVEDIR ] && cd $WMLIVEDIR && lb clean
    cd -
    rm -f $WMLIVEDIR/build*.log
done

if [ ! -d "$WMLIVEBKPDIR" ]
then
    mkdir -p "$WMLIVEBKPDIR"
fi

if [ -d "$WMLIVEBUILDIR" ] && [ -d "$WMLIVEBKPDIR" ]
then
    BKPTIME=$(date +%Y-%m-%d-%H%M)
    WMLIVEBKPTXZ=wmlive-desktop_"$BKPTIME".tar.xz
    WMLIVEBACKUP="$WMLIVEBKPDIR"/"$WMLIVEBKPTXZ"

    echo ""
    echo "Creating full $WMLIVEBACKUP ..."
    echo ""

    tar cJf "$WMLIVEBACKUP" --exclude="wmlive-desktop/wmlive-*/cache/*" --exclude="wmlive-desktop/wmlive-*/.build" \
       --exclude="wmlive-desktop/wmlive-*/config/includes.binary/*/*.tar.*" \
       --exclude="wmlive-desktop/build-config/wmlive-opt/linux-*/*" --exclude="wmlive-desktop/mk*_*.log" \
       --exclude="wmlive-desktop/*diff" --exclude="wmlive-desktop/build-config/includes.chroot/opt/*" "$WMLIVEBUILDIR"

    if [ -s "$WMLIVEBACKUP" ]
    then
        echo ""
        echo "Backed up $WMLIVEBUILDIR to $WMLIVEBACKUP:"
        echo ""
        ls -l "$WMLIVEBACKUP"
        echo ""
    fi
else
    echo "Are you in the correct location?"
fi
}

#
# check for existence of and copy in place wmlive sources
#

copy_wmlive_source ()
{
if [ -d ../backup.d ]
then
    WMLIVESRC="$(echo build-config/includes.binary/$(echo "$ARCH" | cut -d \- -f1)/sources/wmlive-desktop_$(date +%Y-%m-%d).tar.xz)"
    SRCTARGET=$(dirname "$WMLIVESRC")

    if [ -d "$SRCTARGET" ] && [ ! -s "$WMLIVESRC" ]
    then
        cp -v ../backup.d/$(basename $(ls ../backup.d | tail -n1)) "$WMLIVESRC"
    else
        echo Cannot copy \'$(basename "$WMLIVESRC")\' to target directory 
        echo \'"$SRCTARGET"\' because one of them
        echo or maybe even both of them are missing.
    fi
fi
}

create_pkgrepack_list ()
{
for ARCHDIR in $(ls build-config/packages.chroot | grep -E '^amd64|^i386')
do
    LISTDIR=build-config/packages.chroot/$ARCHDIR
    PKGLIST="$LISTDIR"/pkg_list
    if [ -d "$LISTDIR" ] && [ "$(ls -1 "$LISTDIR"  | grep -c deb$)" != "0" ]
    then
        rm -f $PKGLIST
        ls "$LISTDIR" | grep deb$ | cut -d_ -f1 > "$PKGLIST"
    fi
done
}

create_pkgs_from_repack_list ()
{
ARCH=$1
if [ -d build-config/packages.chroot/$ARCH ] && [ -s build-config/packages.chroot/$ARCH/pkg_list ]
then
    cd build-config/packages.chroot/$ARCH
    for pkg in $(cat ./pkg_list)
    do
        if [ "$(/bin/ls "$pkg"_*.deb 2>/dev/null | wc -l)" = "0" ]
        then
            dpkg-repack $pkg 2>/dev/null
    fi
    done
    cd - >/dev/null
fi
}
