#!/bin/bash
# Copyright 2010 - 2011 Nicolás Reynolds
# Copyright 2011 Joshua Ismael Haase Hernández

# ---------- GNU General Public License 3 ----------

# This file is part of Parabola.

# Parabola is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Parabola is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Parabola. If not, see <http://www.gnu.org/licenses/>.


# set -x # uncomment for debug

function copy_log {       # copy logs if they exist

    find "${CHROOTDIR}/${CHROOT}/build/" -maxdepth 1 -name "*\.log" -exec cp {} ./ \;

}


function trap_exit {      # End inmediately but print a useful message

    copy_log
    error "$@"
    exit 1

}

# Trap signals from makepkg
set -E
trap 'trap_exit "(libremakepkg): TERM signal caught. Exiting..."' TERM HUP QUIT
trap 'trap_exit "(libremakepkg): Aborted by user! Exiting..."' INT
trap 'trap_exit "(libremakepkg): An unknown error has occurred. Exiting..."' ERR

source /etc/libretools.conf

CLEANFIRST="false"
UPDATEFIRST="false"
CHECKNONFREE="true"
LIBRECHROOT_ARGS=""
MAKEPKG_ARGS=""

function usage {

    echo ''
    echo 'cd to a dir containing a PKGBUILD and run:'
    echo '$0 [options] [-- makechrootpkg args [-- makepkg args]]'
    echo 'This script will build your package on a chroot.'
    echo ''
    echo 'OPTIONS:'
    echo ''
    echo ' -h : show this message.'
    echo ' -c : clean the chroot before building.'
    echo ' -u : update the chroot before building.'
    echo ' -d <chrootdir> : use this dir instead of "$CHROOTDIR"'
    echo ' -n <chrootname> : use this dir instead of "$CHROOT".'
    echo ' -N : do not check freedom issues' # As fullpkg-check will do that before
    echo ''
    exit 1

}

while getopts 'hcud:n:N' arg ; do
    case "${arg}" in
        h) usage ;;
        c) CLEANFIRST="true" ;;
        u) UPDATEFIRST="true" ;;
        d) CHROOTDIR="$OPTARG"
            LIBRECHROOT_ARGS='-d "$OPTARG"' ;;
        n) CHROOT="$OPTARG" ;;
        N) CHECKNONFREE="false" ;;
    esac
done

# Pass all arguments after -- right to makechrootpkg
MAKEPKG_ARGS="$makepkg_args ${*:$OPTIND}"

if (( EUID )); then
    error "This script must be run as root"
    exit 1
fi

if [ ! -e PKGBUILD ]; then  # Check if we are actually on a build directory. Do this early.
    error "This isn't a build directory"; usage
fi

msg "Checking PKGBUILD for non-free issues"
if "$CHECKNONFREE"; then
    if ! pkgbuild-check-nonfree; then

        if [[ $? -eq 15 ]]; then    # other errors mean fail, not nonfree
            error "PKGBUILD contains non-free issues"
            exit 15
        else
            warning "PKGBUILD couldn't be check aganist non-free issues"
        fi
    fi
fi

if "$CLEANFIRST"; then
    librechroot -c "$LIBRECHROOT_ARGS" "$CHROOT"
fi

if "$UPDATEFIRST"; then
    librechroot -u "$LIBRECHROOT_ARGS" "$CHROOT"
fi

unset CLEANFIRST UPDATEFIRST LIBRECHROOT_ARGS

makechrootpkg -d -r "$CHROOTDIR" -l "$CHROOT" -- $MAKEPKG_ARGS
ev="$?" # exit value

copy_log

exit $ev