File: switchconf

package info (click to toggle)
switchconf 0.0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 172 kB
  • sloc: sh: 437; makefile: 121
file content (159 lines) | stat: -rwxr-xr-x 3,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash

# switchconf -- Easy configuration switch
#
# (c) 2002 Sebastien J. Gross <sjg@debian.org>
# (c) 2014 John Hallam <john@hallam.dk>
# (c) 2005-2014 Instituto Superior T├ęcnico by <jose.calhariz@ist.utl.pt>
# (c) 2015 Jose M Calhariz <jose@calhariz.com>
# (c) 2019 Instituto Superior Tecnico by Jose Calhariz
#      <jose.calhariz@tecnico.ulisboa.pt>
# Please see the COPYRIGHT file included in the package.
#

lockfile="/var/lock/switchconf"

switchconf=$1

if [ "${switchconf}" = "-f" ]; then
    shift;
    switchconf=$1;
    shift;
else
    switchconf=/etc/switchconf/conf;
fi

if [ "${switchconf:0:1}" = "/" -a -r ${switchconf} ]; then
    SWITCHCONF_CONF=${switchconf}
elif [ -r ./${switchconf} ]; then
    SWITCHCONF_CONF=`pwd`/${switchconf}
else
    echo "Configuration file ${switchconf} not found"
    exit 1
fi

. ${SWITCHCONF_CONF}

memfile=${current_active_conf_file:-"/var/lib/misc/switchconf.lastcfg"}

help() {
    echo "Syntax: switchconf [ -f conf_file ] [ configuration | -list | -help ]"
    exit 0
}

list() {
    cd ${conf_top_dirs}
    find -maxdepth 1 -type d | sed 's/^\.\/\?//' | grep -v "\(^\$\|${exec_dir_before}\|${exec_dir_after}\)" | sort
    exit 0
}

run_scripts(){
    local scriptsdir=$1
    shift
    if test -x "${run_parts}" ; then
	echo "Executing scripts: ";
	${run_parts} --arg "${conf}" -v "$scriptsdir" 2>&1
	echo "Executing scripts done.";
    else
	echo "Executing scripts: ";
	for r in `find ${scriptsdir} -type f -perm /1 | sort`; do
	    echo "$r"
	    ${r} "${conf}" 2>&1
	done
	echo "Executing scripts done.";
    fi | logger -s -t switchconf -i -p daemon.notice
}

run_scripts_before(){
    run_scripts ${conf_top_dirs}/${exec_dir_before}
}

run_scripts_after(){
    run_scripts ${conf_top_dirs}/${exec_dir_after}
}

# validates configuration file
config_method=${config_method:=softlink}
case ${config_method} in
    softlink)
    ;;
    hardlink)
    ;;
    copy)
    ;;
    *)
	echo "Check you configuration"
	echo "config_method option have unsupported value'$config_method'"
	exit 1
esac

[ $# -eq 1 ] || help
conf=$1

case ${conf} in
    -list | -l )
	list
	;;
    -help | -h )
	help
	;;
esac

cd ${conf_top_dirs}
if [ ! -d ${conf} ]; then
    echo "Configuration ${conf} not found"
fi



# Check last run of switchconf have stoped
if ! dotlockfile -p -l -r 1 "${lockfile}" ; then
    echo "There is another switchconf still running" | logger -s -t switchconf -i -p daemon.notice
    echo "Aborting" | logger -s -t switchconf -i -p daemon.notice
    exit 1;
fi

# executing each scripts before
run_scripts_before

for f in `find ${conf} ! -type d | sed "s/^${conf}\///"`; do
    dir=`dirname ${dest_dir}/${f}`
    [ -d ${dir} ] || mkdir -p ${dir}
    case ${config_method} in
	copy)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    cp -fa ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		    cp -fa ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi		
	    ;;
	hardlink)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    cp -fal ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		cp -fal ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi		
	    ;;
	softlink|*)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    ln -s ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		ln -s ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi
    esac
done

# executing each scripts after
run_scripts_after

echo "${conf}" > ${memfile}

# remove lock
dotlockfile -u "${lockfile}"
exit 0