#!/bin/sh

# pdeb_common  version 0.1
# common functions and variables for hook scripts
#
# Copyright (c) 2009-2017 Francesco Poli
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


LC_ALL=C
export LC_ALL

APTITUDE="aptitude -q -y -o APT::Acquire::Retries=3"

SRCLIST="/etc/apt/sources.list"
DIST=$( grep '^deb ' $SRCLIST | tail -n 1 | cut -d ' ' -f 3 )

init_aptitude_log ()
{
    if test "x$1" = "x"
    then
        # need one argument!
        return 1
    fi

    local TMP_LOG=$( mktemp --tmpdir aptitude-log.XXXXXX )
    local A_TLOG="$APTITUDE -o Aptitude::Log=$TMP_LOG"

    eval "$1=\"\${A_TLOG}\""
}

purge_installed_pkgs ()
{
    if test "x$1" = "x" -o "x$2" = "x"
    then
        # need two arguments!
        return 1
    fi

    eval "local A_TLOG=\"\${$1}\""
    local FINAL_LOG=${2##*=}

    local TMP_LOG=${A_TLOG##*=}
    local TO_BE_PURGED="$( sed -n '/INSTALL/ s/^\[.*\] \([^ ]*\).*$/\1/ p' \
                           ${TMP_LOG} | sort -u )"
    cat $TMP_LOG >> $FINAL_LOG
    rm -f $TMP_LOG
    $APTITUDE -o Aptitude::Log=$FINAL_LOG --purge-unused purge $TO_BE_PURGED
}

downgrade_upgraded_pkgs ()
{
    if test "x$1" = "x" -o "x$2" = "x"
    then
        # need two arguments!
        return 1
    fi

    eval "local A_TLOG=\"\${$1}\""
    local FINAL_LOG=${2##*=}

    local TMP_LOG=${A_TLOG##*=}
    TO_BE_DOWNGRADED="$( awk '/UPGRADE/ { sub(":.*$", "", $2);
                                          print $2"="$3; }' \
                         ${TMP_LOG} | sort -u )"
    cat $TMP_LOG >> $FINAL_LOG
    rm -f $TMP_LOG
    $APTITUDE -o Aptitude::Log=$FINAL_LOG -t $DIST install $TO_BE_DOWNGRADED
}

downgrade_pkgs_again ()
{
    if test "x$1" = "x"
    then
        # need one argument!
        return 1
    fi
    if test "x$TO_BE_DOWNGRADED" = "x"
    then
        # downgrade_upgraded_pkgs should be called first!
        return 2
    fi

    eval "local A_TLOG=\"\${$1}\""

    $A_TLOG -t $DIST install $TO_BE_DOWNGRADED
}

create_local_repo ()
{
    if test "x$1" = "x" -o "x$2" = "x" -o "x$3" = "x"
    then
        # need three arguments!
        return 1
    fi

    local TMP_REPO=$( mktemp -d --tmpdir repository.XXXXXX )
    local ARCH=$( dpkg --print-architecture )
    local TMP_REPO_DIR=$TMP_REPO/dists/unreleased/main/binary-$ARCH
    local TMP_REPO_POOL=$TMP_REPO/pool
    mkdir -p $TMP_REPO_DIR $TMP_REPO_POOL
    cp -av $( find $HOME -name \*.deb ) $TMP_REPO_POOL
    local PREVWD=$( pwd )
    cd $TMP_REPO
    apt-ftparchive packages pool > $TMP_REPO_DIR/Packages

    cat > $TMP_REPO_DIR/Release << EOF
Archive: unreleased
Origin: Local
Label: local_temporary_repository
Component: main
Architecture: $ARCH
EOF

    local TMP_CHECKSUMS=$( mktemp --tmpdir checksums.XXXXXX )
    apt-ftparchive release $TMP_REPO_DIR/../../ > $TMP_CHECKSUMS

    cat > $TMP_REPO_DIR/../../Release << EOF
Origin: Local
Label: local_temporary_repository
Suite: unreleased
Codename: nextvers
Architectures: $ARCH
Components: main
Description: locally built packages
EOF

    cat $TMP_CHECKSUMS >> $TMP_REPO_DIR/../../Release
    rm -f $TMP_CHECKSUMS

    local TMP_SOURCES=$( mktemp /etc/apt/sources.list.d/localXXXXXX.list )
    echo "deb [trusted=yes] file:$TMP_REPO unreleased main" > $TMP_SOURCES

    local TMP_PREFS=$( mktemp /etc/apt/preferences.d/localXXXXXX )
    cat > $TMP_PREFS << EOF
Package: *
Pin: release o=Debian,n=$DIST
Pin-Priority: 800
EOF

    $APTITUDE update
    cd ${PREVWD}

    eval "$1=\"\${TMP_REPO}\""
    eval "$2=\"\${TMP_SOURCES}\""
    eval "$3=\"\${TMP_PREFS}\""
}

destroy_local_repo ()
{
    if test "x$1" = "x" -o "x$2" = "x" -o "x$3" = "x"
    then
        # need three arguments!
        return 1
    fi

    eval "local TMP_REPO=\"\${$1}\""
    eval "local TMP_SOURCES=\"\${$2}\""
    eval "local TMP_PREFS=\"\${$3}\""

    rm -f $TMP_PREFS $TMP_SOURCES
    rm -Rf $TMP_REPO
    $APTITUDE update
}

