#!/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 `grep "/usr/share/libreoffice" ${fielist} | 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="${dir/${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 "${linfile}" ]]; then ln -sf "${file}" "${linfile}" || exit 1 fi done else # first just remove the symlinks for file in `grep "/usr/share/libreoffice" ${fielist} | sort`; do linkfile=${file/${datadir}/${libdir}} if [[ -L "${linkfile}" && ! -r "${linfile}" ]]; 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 `grep "/usr/share/libreoffice" ${fielist} | sort`; do linkdir="${dir/${datadir}/${libdir}}" 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