Blame docker-builder-data/build/script/common/helpers.sh

e7fa50
# helpers
e7fa50
f17b54
allvars() {
f17b54
    for LOCAL_ALLVARS_VAR_PREFIX in _ {a..z} {A..Z}; do
f17b54
        eval echo -n $\{\!$LOCAL_ALLVARS_VAR_PREFIX*} | sed "s|LOCAL_ALLVARS_VAR_PREFIX||g"
f17b54
        echo -n " "
f17b54
    done
f17b54
}
f17b54
f17b54
vars_clear() {
f17b54
    # local PREFIX=$1
f17b54
    [ ! -z "$1" ] || return 1
f17b54
    for VAR in $(allvars); do
f17b54
        if [[ "$VAR" = $1* ]]; then
f17b54
            unset $VAR
f17b54
        fi
f17b54
    done
f17b54
}
f17b54
f17b54
vars_copy() {
f17b54
    # local PREFIX_FROM=$1
f17b54
    # local PREFIX_TO=$2
f17b54
    # local EXPORT=$3
f17b54
    [ "$1" == "$2" ] && return 0
f17b54
    for VAR in $(allvars); do
f17b54
        if [[ "$VAR" = $1* ]]; then
f17b54
            if [ "$3" = "export" ]; then
f17b54
                eval export ${2}${VAR#$1}='${!VAR}'
f17b54
            else
f17b54
                eval ${2}${VAR#$1}='${!VAR}'
cc53b4
            fi
f17b54
        fi
f17b54
    done
f17b54
}
f17b54
f17b54
vars_rename() {
f17b54
    # local PREFIX_FROM=$1
f17b54
    # local PREFIX_TO=$2
f17b54
    [ ! -z "$1" ] || return 1
f17b54
    vars_copy "$1" "$2"
f17b54
    vars_clear "$1"
f17b54
}
f17b54
    
f17b54
vars_backup() {
f17b54
    # local PREFIX=$1
f17b54
    [ ! -z "$1" ] || return 1
f17b54
    vars_copy "" "$1"
f17b54
}
f17b54
f17b54
vars_restore() {
f17b54
    # local PREFIX=$1
f17b54
    # local EXPORT=$2
f17b54
    [ ! -z "$1" ] || return 1
f17b54
    vars_copy "$1" "" "$2"
f17b54
}
f17b54
e7fa50
copy() {
e7fa50
    local SRC=$1
e7fa50
    local DEST=$2
e7fa50
    if [ -d "$SRC" ]; then
e7fa50
        if ! mkdir -p $DEST; then
e7fa50
            return 1
e7fa50
        fi
e7fa50
        if [ "$(ls -A $1)" ]; then
e7fa50
            if ! cp --remove-destination -rlP $SRC/* "$DEST/"; then
e7fa50
                return 1
e7fa50
            fi
e7fa50
        fi
e7fa50
    elif [ -f "$SRC" ]; then
e7fa50
        if ! (mkdir -p `dirname $DEST` && cp --remove-destination -l "$SRC" "$DEST"); then
e7fa50
            return 1
e7fa50
        fi
e7fa50
    else
e7fa50
        return 1
e7fa50
    fi
e7fa50
}
e7fa50
e7fa50
foreachfile() {
3ec770
    local DIR="$1"
3ec770
    local COMMAND="$2"
3ec770
    if [ -z "$DIR" ] || [ ! -e "$DIR" ]; then
3ec770
        return 1
3ec770
    fi
3ec770
        
3ec770
    if [ -d "$DIR" ]; then
3ec770
        for FILE in "$DIR/".*; do
3ec770
            if [ "$FILE" != "$DIR/." ] && [ "$FILE" != "$DIR/.." ]; then
3ec770
                if ! "$COMMAND" "$FILE" ${@:3}; then
3ec770
                    return 1
3ec770
                fi
3ec770
            fi
3ec770
        done
3ec770
        for FILE in "$DIR/"*; do
3ec770
            if [ "$FILE" != "$DIR" ] && [ "$FILE" != "$DIR/" ]; then
3ec770
                if ! "$COMMAND" "$FILE" ${@:3}; then
3ec770
                    return 1
3ec770
                fi
3ec770
            fi
3ec770
        done
3ec770
    fi
3ec770
}
3ec770
3ec770
remove_recursive() {
3ec770
    local DIR="$1"
3ec770
    local NEEDLE="$2"
3ec770
3ec770
    if [ -d "$DIR" ]; then
3ec770
        rm -f "$DIR/"$NEEDLE
3ec770
        if ! foreachfile "$DIR" "${FUNCNAME[0]}" "$NEEDLE"; then
3ec770
            return 1
3ec770
        fi
3ec770
    fi
3ec770
}
3ec770
3ec770
foreachfile() {
e7fa50
    local FILE=$1
e7fa50
    local COMMAND=$2
f026f4
    if [ ! -e "$FILE" ]; then
e7fa50
        return 1
e7fa50
    fi
e7fa50
    if [ -d "$FILE" ]; then    
e7fa50
        ls -1 "$FILE" | while read SUBFILE; do
e7fa50
            if ! $COMMAND "$FILE/$SUBFILE" ${@:3}; then
e7fa50
                return 1
e7fa50
            fi
e7fa50
        done
e7fa50
    fi
e7fa50
}
e7fa50
3ec770
sha512dir() {
3ec770
    local DIR="$1"
3ec770
    local INFO="$2"
b22744
    
b22744
    local BASE=$(basename "$DIR")
e7fa50
b22744
    [[ "$BASE" != ".git" ]] || return 0
b22744
    [[ "$BASE" != *.po   ]] || return 0
e7fa50
3ec770
    if [ "$INFO" = "info" ]; then
3ec770
        basename "$DIR" || return 1
3ec770
        stat -c%F:%a:%s "$DIR" || return 1
3ec770
    fi
3ec770
        
3ec770
    if [ -d "$DIR" ]; then
3ec770
        (foreachfile "$DIR" "${FUNCNAME[0]}" info | sha512sum -b | cut -c1-128) || return 1
b22744
    elif [ -L "$DIR" ]; then
b22744
        (readlink "$DIR" | sha512sum -b | cut -c1-128) || return 1
3ec770
    else
3ec770
        (sha512sum -b "$DIR" | cut -c1-128) || return 1
3ec770
    fi
e7fa50
}
e7fa50
e7fa50
copy_system_lib() {
e7fa50
    local SRC_NAME=$1
e7fa50
    local DST_PATH=$2
96ed23
    cp --remove-destination /lib/x86_64-linux-gnu/$SRC_NAME* "$DST_PATH" &> /dev/null
96ed23
    cp --remove-destination /lib/i386-linux-gnu/$SRC_NAME* "$DST_PATH" &> /dev/null
96ed23
    cp --remove-destination /usr/lib/x86_64-linux-gnu/$SRC_NAME* "$DST_PATH" &> /dev/null
96ed23
    cp --remove-destination /usr/lib/i386-linux-gnu/$SRC_NAME* "$DST_PATH" &> /dev/null
96ed23
    if ! (ls "$DST_PATH/$SRC_NAME"* &> /dev/null); then
96ed23
        echo "$SRC_NAME not found in system libraries"
96ed23
        return 1
96ed23
    fi
e7fa50
}
e7fa50
474272
add_common_licenses() {
474272
    local FILE="$1"
474272
    local TARGET="$2"
474272
474272
    local LIC_PATH="/usr/share/common-licenses"
474272
    [ -d "$LIC_PATH" ] || return 0
474272
    [[ ! "$FILE" = "$LIC_PATH/"* ]] || return 0
474272
    ls -d1 "$LIC_PATH/"* | while read SUB_FILE; do
474272
        if grep -q "$SUB_FILE" "$FILE"; then
474272
            add_license "$SUB_FILE" "$SUB_FILE" "$TARGET"
474272
        fi
474272
    done
474272
}
474272
e7fa50
add_license() {
e7fa50
    local FILE="$1"
e7fa50
    local FILE_IN_TITLE="$2"
e7fa50
    local TARGET="$3"
e7fa50
    if [ ! -z "$FILE_IN_TITLE" ]; then
e7fa50
        echo ""                                      >> "$TARGET" || return 1
e7fa50
        echo "-------------------------------------" >> "$TARGET" || return 1
e7fa50
        echo "  File: $FILE_IN_TITLE"                >> "$TARGET" || return 1
e7fa50
        echo "-------------------------------------" >> "$TARGET" || return 1
e7fa50
        echo ""                                      >> "$TARGET" || return 1
e7fa50
    else
e7fa50
        echo ""                                      >> "$TARGET" || return 1
e7fa50
        echo "-------------------------------------" >> "$TARGET" || return 1
e7fa50
        echo ""                                      >> "$TARGET" || return 1
e7fa50
    fi
e7fa50
    cat "$FILE"                                      >> "$TARGET" || return 1
474272
    add_common_licenses "$FILE" "$TARGET" || return 1
e7fa50
}
e7fa50
e7fa50
copy_system_license() {
e7fa50
    local SRC_NAMES=$1
e7fa50
    local DST_PATH=$2
e7fa50
    local SRC_NAME=
474272
e7fa50
    for SRC_NAME in $SRC_NAMES; do
e7fa50
        rm -f "$DST_PATH/license-$SRC_NAME"
e7fa50
    done
e7fa50
    for SRC_NAME in $SRC_NAMES; do
e7fa50
        local TARGET="$DST_PATH/license-$SRC_NAME"
474272
        for SUFFIX in "" {0..9} "-"; do
474272
            local SUB_NAME="$SRC_NAME$SUFFIX"
474272
            if [ ! -z "$SUFFIX" ]; then
474272
                SUB_NAME="$SUB_NAME*"
474272
            fi
e7fa50
474272
            for MASK in "/usr/share/doc/$SUB_NAME/copyright" \
474272
                        "/usr/share/licenses/$SUB_NAME" \
474272
                        "/usr/share/licenses/$SUB_NAME/*" \
474272
                        "/usr/share/doc/$SUB_NAME/*"
474272
            do
474272
                local FOUND=
474272
                ls -d1 $MASK 2>/dev/null | while read FILE; do
474272
                    if [ -f "$FILE" ] && [[ "$FILE" != *.bz2 ]]; then
474272
                        FOUND=1
474272
                        if ! add_license "$FILE" "$FILE" "$TARGET"; then
474272
                            echo "Cannot add license file: $FILE -> $TARGET";
474272
                            return 1
474272
                        fi
474272
                    fi
474272
                done
474272
                if [ ! -z "$FOUND" ]; then
474272
                    break
474272
                fi
e7fa50
            done
474272
474272
            if [ -z "$SUFFIX" ] && [ -f "$TARGET" ]; then
474272
                return 0
474272
            fi
474272
        done
474272
e7fa50
        if [ -f "$TARGET" ]; then
e7fa50
            return 0
e7fa50
        fi
e7fa50
    done
e7fa50
e7fa50
    echo "Cannot found any license for one of system packages: $SRC_NAMES (for $DST_PATH)"
e7fa50
    return 1
e7fa50
}
e7fa50