File link-to-ooo-home of Package libreoffice.143

#!/usr/bin/env bash

usage() {
    echo "This script (un)links or unlinks the given to/from libreoffice home"
    echo
    echo "Usage: ${0##*/} [--unlink] filelist"
}

change_linking() {
    local libdir="$1"
    local filelist="$2"
    local linkfile=""
    local linkdir=""

    # Decide if we are linking or wiping first
    if ${link_mode}; then
        # Grab all files from the proper folder
        for file in `cat ${filelist} | grep "/usr/share/libreoffice" | sort`; do
            # if we get ourselves folder then just create it
            # it might not be around so lets be safe
            if [[ -d "${file}" ]] ; then
                dirname="${file/${datadir}/${libdir}}"
                # if the location is already there skip it
                if [[ ! -e "${dirname}" ]]; then
                    mkdir -p "${dirname}"
                fi
                continue
            fi
            linkfile="${file/${datadir}/${libdir}}"
            # if the file is already there, skip it
            # this is true when the parent folder is link
            if [[ ! -e "${linkfile}" ]]; then
                ln -sf "${file}" "${linkfile}" || exit 1
            fi
        done
    else
        # first just remove the symlinks
        for file in `cat ${filelist} | grep "/usr/share/libreoffice" | sort`; do
            linkfile=${file/${datadir}/${libdir}}
            if [[ -L "${linkfile}" && ! -r "${linkfile}" && ! -d "${linkfile}" ]]; then
                rm -f "${linkfile}" || exit 1
            fi
        done
        # continue by wiping out all EMPTY dirs
        # we have to be sure it is not owned by anything else
        # doing in 2nd run to ensure avoiding collisions
        for file in `cat ${filelist} | grep "/usr/share/libreoffice" | sort`; do
            linkdir="${file/${datadir}/${libdir}}"
            if [[ -d "${linkdir}" && -z `ls "${linkdir}"/*` ]]; then
                # check if nothing else owns the dir
                if [[ $(rpm -qf "${file}" 2>/dev/null |wc -l) == 0 ]]; then
                    rmdir "${linkdir}" || exit 1
                fi
            fi
        done
    fi
}

# Global VARS
link_mode=true
datadir=/usr/share
libdirs=(
    "/usr/lib/"
    "/usr/lib64/"
    "/usr/lib32/"
)

if [[ "$1" == "--unlink" ]]; then
    link_mode=false
    shift
fi

if [[ "$1" == "--help" ]]; then
    usage
    exit 0
fi

# Verify we have just one left argument which is the filelist
if [[ $# > 1 || ! -f "$1" ]]; then
    usage
    exit 1
fi

for libdir in ${libdirs[@]}; do
    # for each dir verify there is libreoffice folder, otherwise skip
    if [[ ! -d "${libdir}/libreoffice/" ]]; then
        continue
    fi
    change_linking ${libdir} $1
done
openSUSE Build Service is sponsored by