# Created by Tobias Powalowski <tpowa@archlinux.org>

install ()
{
    SCRIPT=
    BINARIES=
    ### check for root
    if ! [ $UID -eq 0 ]; then 
        echo "ERROR: Please run as root user!"
        exit 1
    fi

    ### check for TEMPDIR
    if [ $TEMPDIR = "" ]; then
        echo "ERROR: No TEMPDIR was set, please set TEMPDIR first!"
        exit 1
    fi

    ### check for activated testing repository
    if ! [ "$(grep '^\[testing\]' /etc/pacman.conf)" = "" ]; then 
        echo "WARNING: TESTING REPOSITORY ACTIVATED"
        echo "-------------------------------------"
        echo "POSSIBILITY OF BROKEN BINARY PACKAGES ON INSTALL MEDIA!"
        echo "Use a chroot instead to avoid those problems."
        echo "5 seconds time to cancel with CTRL+C"
        sleep 5
    fi
    ### setting up directories
    if ! [ "${LOCALSETUP}" = "yes" ]; then 
        SVNTREE=$(mktemp /tmp/svntree.XXXX)
        rm ${SVNTREE}
        mkdir -p ${SVNTREE}
    else
        SVNTREE=${SVNTREE}
    fi
    cd ${SVNTREE}
    if ! [ "${LOCALSETUP}" = "yes" ]; then
        # generate base
        BASE=""
        for i in $(pacman -Sg base | sed -e "s/base//g"); do 
            BASE="$BASE $(echo $i)"
        done
        # generate base-devel
        DEVEL=""
        for i in $(pacman -Sg base-devel | sed -e "s/base-devel//g"); do 
            DEVEL="$DEVEL $(echo $i)"
        done
        SUPPORT=""
        SUPPORT="$(echo -n $(pacman -Ss | grep -e ^core | grep -v '(' | sed -e 's/\ .*/ /g' -e 's#core/##g')) gnu-netcat dosfstools ntfs-3g fuse dhclient v86d nouveau-drm-lts nouveau-firmware"
        for i in base devel lib support; do
            mkdir $i
            svn co -N ${SVNSETUP} $i
        done
        cd base; svn up $BASE; cd ..
        cd devel; svn up $DEVEL; cd ..
        cd support; svn up $SUPPORT; cd ..
        cd  ${SVNTREE}
        SEARCHSVN="$(find ./ -type d -name *"$(uname -m)" ! -name "testing*") $(find ./ -type d -name *"any")"
    else
        cd ${SVNTREE}
        SEARCHSVN="$(find ./ -type d -name *"$(uname -m)" ! -name "testing*") $(find ./ -type d -name *"any")"
    fi
    # Download all packages
    PACKAGES=$(for i in ${SEARCHSVN};do ! [ "$(echo $i | awk -F/ '{print $3}')" = "" ] && (. $i/PKGBUILD; echo ${pkgname[@]});unset pkgname;done)
    pacman -Sy
    pacman -Sw --noconfirm ${PACKAGES}
    # collect the packages
    mkdir -p ${TEMPDIR}/core-$(uname -m)/pkg/
    mkdir -p ${TEMPDIR}/core-any/pkg/
    for COPY in ${SEARCHSVN};do
        if ! [ "$(echo ${COPY} | awk -F/ '{print $3}')" = "" ]; then
            . "${COPY}/PKGBUILD"
            if [ "$(echo ${arch} | grep 'any')" ]; then
                for i in ${pkgname[@]}; do
                    cp /var/cache/pacman/pkg/${i}-${pkgver}-${pkgrel}-any.pkg.tar.* ${TEMPDIR}/core-any/pkg/
                    echo "$(echo ${COPY}| awk -F/ '{print $2}')/${i}-${pkgver}-${pkgrel}-any.pkg.tar.*" >> ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
                    repo-add ${TEMPDIR}/core-$(uname -m)/pkg/core.db.tar.gz /var/cache/pacman/pkg/${i}-${pkgver}-${pkgrel}-any.pkg.tar.* || return 1
                done
            else
                for i in ${pkgname[@]}; do
                    cp /var/cache/pacman/pkg/${i}-${pkgver}-${pkgrel}-$(uname -m).pkg.tar.* ${TEMPDIR}/core-$(uname -m)/pkg/
                    echo "$(echo ${COPY}| awk -F/ '{print $2}')/${i}-${pkgver}-${pkgrel}-$(uname -m).pkg.tar.*" >> ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
                    repo-add ${TEMPDIR}/core-$(uname -m)/pkg/core.db.tar.gz /var/cache/pacman/pkg/${i}-${pkgver}-${pkgrel}-$(uname -m).pkg.tar.* || return 1
                done
            fi
            unset pkgname
        fi
    done
    rm  ${TEMPDIR}/core-$(uname -m)/pkg/core.db.tar.gz.old
    ### HACK put udev-compat,kernel26-manpages and kernel26-docs to support category!
    sed -i -e 's#base/udev-compat#support/udev-compat#g' ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
    sed -i -e 's#base/kernel26-manpages#support/kernel26-manpages#g' ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
    sed -i -e 's#base/kernel26-docs#support/kernel26-docs#g' ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
    # generate packages.txt
    sort -u ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt -o ${TEMPDIR}/core-$(uname -m)/pkg/packages.txt
    ### HACK add grub2 and syslinux packages to image, with separate extra db file
    # set LANG to US we parse language output here
    LANG=US
    for i in grub2-bios grub2-common grub2-efi-i386 grub2-efi-x86_64 efibootmgr freetype2 syslinux mtools perl-passwd-md5  perl-digest-sha1; do
        pacman -Sw --noconfirm $i
        pkgver="$(echo $(pacman -Si $i | grep -m 1 Version | cut -d ":" -f 2))"
        pkgarch="$(echo $(pacman -Si $i | grep -m 1 Architecture | cut -d ":" -f 2))"
        if [ "${pkgarch}" = 'any' ]; then
            cp /var/cache/pacman/pkg/$i-${pkgver}-any.pkg.tar.* ${TEMPDIR}/core-any/pkg/
            repo-add ${TEMPDIR}/core-$(uname -m)/pkg/extra.db.tar.gz /var/cache/pacman/pkg/${i}-${pkgver}-any.pkg.tar.* || return 1
        else
            cp /var/cache/pacman/pkg/$i-${pkgver}-$(uname -m).pkg.tar.* ${TEMPDIR}/core-$(uname -m)/pkg/
            repo-add ${TEMPDIR}/core-$(uname -m)/pkg/extra.db.tar.gz /var/cache/pacman/pkg/${i}-${pkgver}-$(uname -m).pkg.tar.* || return 1
        fi        
    done
    rm  ${TEMPDIR}/core-$(uname -m)/pkg/extra.db.tar.gz.old
    # generate iso title name
    [ "${RUNPROGRAM}" = "mkisolinux" ] && echo "Arch_Linux_CORE $(uname -m)" >> ${ISONAME}
}

help ()
{
cat <<HELPEOF
  This hook sets up all you need for an core install image.
HELPEOF
}
