libreoffice/link-to-ooo-home

84 lines
2.2 KiB
Bash

#!/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=""
# If we are starting up then create the folder structure first
if ${link_mode}; then
for dir in `grep "%dir" ${filelist} | sed "s|%dir ||"` ; do
mkdir -p "${dir/${datadir}/${libdir}}"
done
fi
# Create link for each regular file or prune the link if removing
for file in `grep -v "%dir" ${filelist} | sed "s|%config ||"` ; do
linkfile=${file/${datadir}/${libdir}}
if ${link_mode}; then
ln -sf "${file}" "${linkfile}" || exit 1
else
# remove only empty (broken links)
# there might be multiple deps providing the same
if [[ -L "${linkfile}" && ! -r "${linfile}" ]]; then
rm -f "${linkfile}" || exit 1
fi
fi
done
# Remove all empty dirs but validate if they do not belong
# to some other pkg too
if ! ${link_mode}; then
for dir in `grep "%dir" ${filelist} | sed "s|%dir ||"` ; do
linkdir="${dir/${datadir}/${libdir}}"
# Check if dir is empty
if [[ -d "${linkdir}" && -z `ls "${linkdir}"/*` ]]; then
# check if nothing else owns the dir
if [[ $(rpm -qf "${dir}" 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