<commit_before>##############################################################################
#
# Debian specific package building
#
##############################################################################
HAS_DEBIAN_INIT=0

function lp_init() {
    # Nothing to do
    return
}

function lp_prepare_package_directory() {
    mkdir -p $PKG_ROOTDIR/debian
    touch $PKG_ROOTDIR/TODO
}

function lp_handle_system_file() {
    local FILE=$1
    local TYPE=$2

    if [ "$TYPE" = "init" ]; then
        HAS_DEBIAN_INIT=1
    fi

    mv $FILE $PKG_ROOTDIR/debian/
}

function lp_handle_package_files() {
    local PHASE
    local CPKG_SCRIPT_BASE

    declare -A HAS_SCRIPTS
    HAS_SCRIPTS["postinst"]=0
    HAS_SCRIPTS["postrm"]=0
    HAS_SCRIPTS["preinst"]=0
    HAS_SCRIPTS["prerm"]=0

    if (($HAS_DEBIAN_INIT)); then
        HAS_SCRIPTS["postinst"]=1
        HAS_SCRIPTS["prerm"]=1
        HAS_SCRIPTS["postrm"]=1
    fi

    CPKG_SCRIPT=/tmp/cpkg-script.$$

    declare -A SCRIPT_SPECS
    SCRIPT_SPECS["configure"]="postinst";
    SCRIPT_SPECS["remove"]="postrm";
    SCRIPT_SPECS["purge"]="postrm";

    # Handle user-provided script snippets
    for PHASE in ${!SCRIPT_SPECS[@]}; do
        DPKG_PHASE=${SCRIPT_SPECS[${PHASE}]}

        if [ ! -f $PKG_ROOTDIR/$PKG_NAME.$PHASE ]; then
            continue
        fi

        DPKG_DEST=$PKG_ROOTDIR/debian/$PKG_NAME.$DPKG_PHASE

        HAS_SCRIPTS[${DPKG_PHASE}]=1

        cp_wrap_script_for_phase \
            $PKG_ROOTDIR/$PKG_NAME.$PHASE \
            $PHASE \
            $CPKG_SCRIPT

        cp_replace_template_var_from_file \
            $DPKG_DEST \
            "CPKG_${PHASE^^}" \
            $CPKG_SCRIPT
    done

    rm -f $CPKG_SCRIPT

    # Remove unused/empty package scripts (Debian policy)
    for PHASE in postinst postrm preinst prerm; do
        DPKG_SCRIPT=$PKG_ROOTDIR/debian/$PKG_NAME.$PHASE

        if [ ${HAS_SCRIPTS[$PHASE]} -eq 0 ]; then
            rm -f $DPKG_SCRIPT
        else
            cp_process_template $DPKG_SCRIPT
        fi
    done

    local FILES=$(find $PKG_ROOTDIR/debian -maxdepth 1 -type f | xargs)

    local TAG="##"
    local DIRVAL

    if [ -n "$FILES" ]; then
        for DIR in ${PACKAGE_DIRS}; do
            DIRVAL="${DIR}"
            cp_reinplace "s,${TAG}${DIR}${TAG},${!DIRVAL},g" $FILES
        done
    fi
}

function lp_clean_packages_scripts() {
    local FILES=$(
        find $PKG_ROOTDIR/debian -maxdepth 1 -type f | \
        xargs grep -l "#CPKG_.*"
    )

    if [ -n "$FILES" ]; then
        cp_reinplace "s,#CPKG_.*$,,g" $FILES
    fi
}

function lp_install_local_package() {
    sudo dpkg -i *.deb
}

function lp_install_packages() {
    export DEBIAN_FRONTEND=noninteractive
    sudo apt-get -y install $@
    lp_make_pkg_map
}

function lp_configure_package() {
    return
}

function lp_build_package() {
    cd $PKG_ROOTDIR

    if [ "$PKG_ARCH" = "all" ]; then
        dpkg-buildpackage -A -uc
    else
        dpkg-buildpackage -b -uc
    fi

    cd ..

    cp_msg "checking $PKG_NAME-$PKG_VER"

    lintian \
        -q \
        --suppress-tags bad-distribution-in-changes-file \<commit_msg>debugging Travis packaging failure<commit_after>##############################################################################
#
# Debian specific package building
#
##############################################################################
HAS_DEBIAN_INIT=0

function lp_init() {
    # Nothing to do
    return
}

function lp_prepare_package_directory() {
    mkdir -p $PKG_ROOTDIR/debian
    touch $PKG_ROOTDIR/TODO
}

function lp_handle_system_file() {
    local FILE=$1
    local TYPE=$2

    if [ "$TYPE" = "init" ]; then
        HAS_DEBIAN_INIT=1
    fi

    mv $FILE $PKG_ROOTDIR/debian/
}

function lp_handle_package_files() {
    local PHASE
    local CPKG_SCRIPT_BASE

    declare -A HAS_SCRIPTS
    HAS_SCRIPTS["postinst"]=0
    HAS_SCRIPTS["postrm"]=0
    HAS_SCRIPTS["preinst"]=0
    HAS_SCRIPTS["prerm"]=0

    if (($HAS_DEBIAN_INIT)); then
        HAS_SCRIPTS["postinst"]=1
        HAS_SCRIPTS["prerm"]=1
        HAS_SCRIPTS["postrm"]=1
    fi

    CPKG_SCRIPT=/tmp/cpkg-script.$$

    declare -A SCRIPT_SPECS
    SCRIPT_SPECS["configure"]="postinst";
    SCRIPT_SPECS["remove"]="postrm";
    SCRIPT_SPECS["purge"]="postrm";

    # Handle user-provided script snippets
    for PHASE in ${!SCRIPT_SPECS[@]}; do
        DPKG_PHASE=${SCRIPT_SPECS[${PHASE}]}

        if [ ! -f $PKG_ROOTDIR/$PKG_NAME.$PHASE ]; then
            continue
        fi

        DPKG_DEST=$PKG_ROOTDIR/debian/$PKG_NAME.$DPKG_PHASE

        HAS_SCRIPTS[${DPKG_PHASE}]=1

        cp_wrap_script_for_phase \
            $PKG_ROOTDIR/$PKG_NAME.$PHASE \
            $PHASE \
            $CPKG_SCRIPT

        cp_replace_template_var_from_file \
            $DPKG_DEST \
            "CPKG_${PHASE^^}" \
            $CPKG_SCRIPT
    done

    rm -f $CPKG_SCRIPT

    # Remove unused/empty package scripts (Debian policy)
    for PHASE in postinst postrm preinst prerm; do
        DPKG_SCRIPT=$PKG_ROOTDIR/debian/$PKG_NAME.$PHASE

        if [ ${HAS_SCRIPTS[$PHASE]} -eq 0 ]; then
            rm -f $DPKG_SCRIPT
        else
            cp_process_template $DPKG_SCRIPT
        fi
    done

    local FILES=$(find $PKG_ROOTDIR/debian -maxdepth 1 -type f | xargs)

    local TAG="##"
    local DIRVAL

    if [ -n "$FILES" ]; then
        for DIR in ${PACKAGE_DIRS}; do
            DIRVAL="${DIR}"
            cp_reinplace "s,${TAG}${DIR}${TAG},${!DIRVAL},g" $FILES
        done
    fi
}

function lp_clean_packages_scripts() {
    local FILES=$(
        find $PKG_ROOTDIR/debian -maxdepth 1 -type f | \
        xargs grep -l "#CPKG_.*"
    )

    if [ -n "$FILES" ]; then
        cp_reinplace "s,#CPKG_.*$,,g" $FILES
    fi
}

function lp_install_local_package() {
    sudo dpkg -i *.deb
}

function lp_install_packages() {
    export DEBIAN_FRONTEND=noninteractive
    sudo apt-get -y install $@
    lp_make_pkg_map
}

function lp_configure_package() {
    return
}

function lp_build_package() {
    cd $PKG_ROOTDIR

    echo "PKG_ARCH=$PKG_ARCH"
    cat debian/control

    if [ "$PKG_ARCH" = "all" ]; then
        dpkg-buildpackage -A -uc
    else
        dpkg-buildpackage -b -uc
    fi

    cd ..

    cp_msg "checking $PKG_NAME-$PKG_VER"

    lintian \
        -q \
        --suppress-tags bad-distribution-in-changes-file \
{'fraction_non_alphanumeric': 0.16186880604058518, 'fraction_numerical': 0.004719207173194903, 'mean_word_length': 2.5283018867924527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '892820', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2217, 'n_words': 427}