121 lines
3.4 KiB
Text
121 lines
3.4 KiB
Text
|
#!/bin/sh
|
||
|
#
|
||
|
# 2008 Nico Schottelius (nico-cinit-conf at schottelius.org)
|
||
|
#
|
||
|
# This file is part of cinit-conf.
|
||
|
#
|
||
|
# cinit-conf is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# cinit-conf is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with cinit-conf. If not, see <http://www.gnu.org/licenses/>.
|
||
|
#
|
||
|
# cinit: create dependency: service a needs or wants service b
|
||
|
#
|
||
|
|
||
|
set -x
|
||
|
|
||
|
# initialise cinit-conf framework
|
||
|
hier="${0%/*}"; set -e; . "${hier}/cinit-conf.read-conf"
|
||
|
|
||
|
if [ $# -ne 3 ]; then
|
||
|
echo "`basename $0`: service_A [wants|needs] service_B"
|
||
|
echo ""
|
||
|
echo " Service A needs or wants Service B."
|
||
|
echo " You must specify whether to use wants or needs."
|
||
|
echo ""
|
||
|
exit 23
|
||
|
fi
|
||
|
|
||
|
SVC_A="$1"
|
||
|
DEP="$2"
|
||
|
SVC_B="$3"
|
||
|
|
||
|
# adding 'wishes' for x-mas and requires for 'the mass' and 'tv'
|
||
|
case "${DEP}" in
|
||
|
wishes|wants) DEP="$C_WANTS" ;;
|
||
|
needs|requires) DEP="$C_NEEDS" ;;
|
||
|
*) echo "Use \"want\" or \"need\" as type of dependency."
|
||
|
esac
|
||
|
|
||
|
# First of all create absolute paths
|
||
|
# if SVC_X beginning matches R_CINIT_DIR it is absolute
|
||
|
# else it is relative to R_CINIT_DIR
|
||
|
echo "${SVC_A}" | grep "^${R_SVC_DIR}" >/dev/null || SVC_A="${R_SVC_DIR}/${SVC_A}"
|
||
|
echo "${SVC_B}" | grep "^${R_SVC_DIR}" >/dev/null || SVC_B="${R_SVC_DIR}/${SVC_B}"
|
||
|
|
||
|
SVC_A_ABS=$(cd "${SVC_A}" && pwd -P)
|
||
|
SVC_B_ABS=$(cd "${SVC_B}" && pwd -P)
|
||
|
|
||
|
SVC_A_REL="${SVC_A_ABS##*/}"
|
||
|
SVC_B_REL="${SVC_B_ABS##*/}"
|
||
|
|
||
|
set +e
|
||
|
match=""
|
||
|
count="0"
|
||
|
svc_stripped="$(echo $SVC_A_ABS | sed -e 's;/$;;' -e 's;/[^/]*$;;')"
|
||
|
while [ "$svc_stripped" ]; do
|
||
|
match="$(echo ${SVC_B_ABS} | grep "^${svc_stripped}")"
|
||
|
count=$(($count+1))
|
||
|
if [ "$match" ]; then
|
||
|
relpath=""
|
||
|
while [ "$count" -gt 0 ]; do
|
||
|
source="../${source}"
|
||
|
count=$(($count-1))
|
||
|
done
|
||
|
source="${source}${SVC_B_REL}"
|
||
|
|
||
|
break
|
||
|
fi
|
||
|
svc_stripped="$(echo $svc_stripped | sed -e 's;/$;;' -e 's;/[^/]*$;;')"
|
||
|
done
|
||
|
|
||
|
# nothing in common? link absolute
|
||
|
if [ ! "$match" ]; then
|
||
|
source="${SVC_B_ABS}"
|
||
|
fi
|
||
|
|
||
|
# Then find out the level of subdirs, excluding destdir!
|
||
|
# FIXME: could someone PLEASE simplify that?
|
||
|
#SLASHES=$(echo $SVC_A | sed -e 's,/$,,' -e 's,[^/],,g' -e 's,/,../,g' -e 's,^,../../,')
|
||
|
|
||
|
# find common anchor and link after that:
|
||
|
# /destdir/etc/cinit/svc/svca/b/
|
||
|
# /destdir/etc/cinit/svc/svca/c/
|
||
|
# results in ../c/
|
||
|
# Thus:
|
||
|
# while unmatched
|
||
|
# remove /* (one level, shortest match)
|
||
|
#
|
||
|
|
||
|
|
||
|
# 1. remove destdir, if present 2. remove everything but the slashes 3. replace them with ../
|
||
|
#
|
||
|
SLASHES=$(echo $SVC_A | sed -e "s;^$DESTDIR;;" -e 's,/$,,' -e 's,[^/],,g' -e 's,/,../,g' -e 's,^,../../,')
|
||
|
DEST_NAME=$(echo $SVC_A | sed -e 's,/$,,' -e 's,/,-,g')
|
||
|
|
||
|
SOURCE="${SLASHES}${SVC_B}"
|
||
|
DEP_DIR="${SVC_A}/${DEP}"
|
||
|
|
||
|
if [ ! -d "$DEP_DIR" ]; then
|
||
|
echo "$DEP_DIR does not exist, creating..."
|
||
|
mkdir "$DEP_DIR"
|
||
|
if [ $? -ne 0 ]; then
|
||
|
echo "Error occured due to creating directory. Exiting."
|
||
|
exit 1
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
DEST="${DEP_DIR}/${DEST_NAME}"
|
||
|
|
||
|
echo -n "Linking ${source} to ${DEST} ... "
|
||
|
ln -sf "${source}" "${DEST}"
|
||
|
echo "finished."
|