1 | #!/bin/bash |
---|
2 | #set -x |
---|
3 | set -o posix |
---|
4 | #set -u |
---|
5 | #set -e |
---|
6 | #+ |
---|
7 | # =============== |
---|
8 | # make_usp_tar.sh |
---|
9 | # =============== |
---|
10 | # --------------- |
---|
11 | # Fetch and tar a unsupported configuration setup. |
---|
12 | # This script is only needed when target systems do |
---|
13 | # not have wget access to the internet. To configure |
---|
14 | # unsupported conigurations on these systems it will |
---|
15 | # first be necessary to run this script on a system that |
---|
16 | # does have access. Then copy access the resulting tar file, |
---|
17 | # unpack and run the enclosed set_local_uspcfg script in a |
---|
18 | # bash shell to complete the process. That script redefines/ |
---|
19 | # defines wget as a bash function to perform local copies |
---|
20 | # from this unpacked tarball. If you wish to create a new |
---|
21 | # configuration based on this local copy of an unsupported |
---|
22 | # configuration in future sessions then you may need to |
---|
23 | # redefine wget again before running makenemo with the |
---|
24 | # appropriate -u setting. A simple script: def_wget is included |
---|
25 | # for such situations. |
---|
26 | # --------------- |
---|
27 | # SYNOPSIS |
---|
28 | # ======== |
---|
29 | # $ make_usp_tar.sh uspcfg.txt target_conf target_dir |
---|
30 | # Note target_dir.tar will be created |
---|
31 | # |
---|
32 | # DESCRIPTION |
---|
33 | # =========== |
---|
34 | # - Extract target configuration details from uspcfg.txt |
---|
35 | # - Create target directory |
---|
36 | # - Recursively use wget to retrieve remote configuration files |
---|
37 | # into target directory |
---|
38 | # - Copy uspcfg.txt into target directory and alter remote paths |
---|
39 | # to local (relative) versions |
---|
40 | # - tar target_directory and remove originals |
---|
41 | #---------------------------------------------------------------- |
---|
42 | # |
---|
43 | #---------------------------------------------------------------- |
---|
44 | # Check the correct number of arguments have been provided |
---|
45 | #---------------------------------------------------------------- |
---|
46 | # |
---|
47 | if [ "$#" != "3" ]; then |
---|
48 | echo "Expected usage: make_usp_tar.sh uspcfg.txt target_conf target_dir" |
---|
49 | exit |
---|
50 | fi |
---|
51 | # |
---|
52 | #---------------------------------------------------------------- |
---|
53 | # Check the named uspcfg.txt file exists |
---|
54 | #---------------------------------------------------------------- |
---|
55 | # |
---|
56 | if [ ! -f $1 ]; then |
---|
57 | echo "named uspcfg.txt file does not exist ($1); attempt abandoned" |
---|
58 | exit |
---|
59 | fi |
---|
60 | # |
---|
61 | #---------------------------------------------------------------- |
---|
62 | # Check the requested configuration is listed in the named uspcfg.txt file |
---|
63 | #---------------------------------------------------------------- |
---|
64 | # |
---|
65 | inthere=$( grep -c "$2" $1 ) |
---|
66 | if [ "$inthere" -lt 1 ]; then |
---|
67 | echo "requested configuration is not in named uspcfg.txt file ($2); attempt abandoned" |
---|
68 | exit |
---|
69 | fi |
---|
70 | # |
---|
71 | #---------------------------------------------------------------- |
---|
72 | # Create the target directory if it does not already exist |
---|
73 | # and cd into it |
---|
74 | #---------------------------------------------------------------- |
---|
75 | # |
---|
76 | if [ ! -d $3 ]; then |
---|
77 | mkdir $3 |
---|
78 | else |
---|
79 | echo "target directory already exists; attempt abandoned" |
---|
80 | exit |
---|
81 | fi |
---|
82 | basedir=$(pwd) |
---|
83 | # |
---|
84 | cd ${3} |
---|
85 | # |
---|
86 | #---------------------------------------------------------------- |
---|
87 | # Copy named uspcfg.txt file into target directory |
---|
88 | #---------------------------------------------------------------- |
---|
89 | # |
---|
90 | cp $basedir/$1 . |
---|
91 | # |
---|
92 | #---------------------------------------------------------------- |
---|
93 | # Extract information on target configuration and |
---|
94 | # retrieve full file list from remote server |
---|
95 | #---------------------------------------------------------------- |
---|
96 | # |
---|
97 | grep "$2 " $1 > ./cfg.tmp |
---|
98 | # |
---|
99 | LOCAL_REF=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $2}') |
---|
100 | TAB=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $3}') |
---|
101 | REMOTE_CTL=$(cat cfg.tmp | awk 'BEGIN {FS = "#" }{print $4}') |
---|
102 | wget ${REMOTE_CTL} -O remote_file.list |
---|
103 | # |
---|
104 | #---------------------------------------------------------------- |
---|
105 | # Retrieve each remote file and create local directory structure |
---|
106 | # At the same time prepare a local version of the control file |
---|
107 | # by replacing http links with a string that will be replaced later |
---|
108 | # with a local directory path |
---|
109 | #---------------------------------------------------------------- |
---|
110 | # |
---|
111 | if [ -f remote_file.list ] ; then |
---|
112 | cat remote_file.list | grep -v '^#' | |
---|
113 | while |
---|
114 | read remfile locfile |
---|
115 | do |
---|
116 | if [ $remfile == 'create_directory' ] ;then |
---|
117 | mkdir $locfile |
---|
118 | echo $remfile " " $locfile >> local_template.ctl |
---|
119 | else |
---|
120 | wget $remfile -O $locfile |
---|
121 | remfile=$(echo $remfile | sed -e "s;.*$locfile;SET_LOCAL_DIRECTORY_HERE/$locfile;") |
---|
122 | echo $remfile " " $locfile >> local_template.ctl |
---|
123 | fi |
---|
124 | done |
---|
125 | else |
---|
126 | echo "Unable to find remote_file.list. Attempt abandoned" |
---|
127 | echo "Files may be left in "$(pwd) |
---|
128 | exit |
---|
129 | fi |
---|
130 | # |
---|
131 | #---------------------------------------------------------------- |
---|
132 | # Construct a modified version of the named uspcfg.txt file |
---|
133 | # First copy across all the other untouched configuration listed |
---|
134 | #---------------------------------------------------------------- |
---|
135 | # |
---|
136 | grep -v "$2 " $1 > uspcfg_local_template.txt |
---|
137 | # |
---|
138 | #---------------------------------------------------------------- |
---|
139 | # Now append the modified entry, replacing http links as before |
---|
140 | #---------------------------------------------------------------- |
---|
141 | # |
---|
142 | cat cfg.tmp | sed -e "s;http.*$;SET_LOCAL_DIRECTORY_HERE/local.ctl;" >> uspcfg_local_template.txt |
---|
143 | # |
---|
144 | # |
---|
145 | #---------------------------------------------------------------- |
---|
146 | # Construct a script that can be used later to complete a local installation |
---|
147 | #---------------------------------------------------------------- |
---|
148 | # |
---|
149 | cat > set_local_uspcfg << EOF |
---|
150 | #!/bin/bash |
---|
151 | if [ "\$( echo $SHELL | grep -c bash )" -lt 1 ]; then |
---|
152 | echo "WARNING: This is only going to be effective in a bash shell" |
---|
153 | echo "since it redefines the wget command as a bash function." |
---|
154 | echo "(ignore this comment if you are in a bash shell)" |
---|
155 | fi |
---|
156 | basedir=\$(pwd) |
---|
157 | # |
---|
158 | echo "Enter full path to the CONFIG directory on your target system: " |
---|
159 | read confdir |
---|
160 | # |
---|
161 | # Edit the local.ctl file to set the local directory path |
---|
162 | # |
---|
163 | sed -e "s;SET_LOCAL_DIRECTORY_HERE;\$basedir;" local_template.ctl > local.ctl |
---|
164 | # |
---|
165 | # Edit the uspcfg_local.txt file to set the local directory path |
---|
166 | # |
---|
167 | sed -e "s;SET_LOCAL_DIRECTORY_HERE;\$basedir;" uspcfg_local_template.txt > uspcfg_local.txt |
---|
168 | # |
---|
169 | # Install local versions in the named CONFIG directory |
---|
170 | # |
---|
171 | if [ -f \$confdir/uspcfg.txt ] && [ ! -L \$confdir/uspcfg.txt ]; then |
---|
172 | mv \$confdir/uspcfg.txt \$confdir/uspcfg_remote.txt |
---|
173 | echo "\$confdir/uspcfg.txt moved to \$confdir/uspcfg_remote.txt" |
---|
174 | fi |
---|
175 | if [ -f \$confdir/uspcfg_local.txt ]; then |
---|
176 | echo "Existing uspcfg_local.txt file found in \$confdir" |
---|
177 | echo "This has been moved to: "\$confdir/uspcfg_local.txt\$\$ |
---|
178 | mv \$confdir/uspcfg_local.txt \$confdir/uspcfg_local.txt\$\$ |
---|
179 | fi |
---|
180 | mv uspcfg_local.txt \$confdir/uspcfg_local.txt |
---|
181 | ln -s \$confdir/uspcfg_local.txt \$confdir/uspcfg.txt |
---|
182 | # |
---|
183 | # define/redefine the wget command |
---|
184 | # |
---|
185 | function wget { |
---|
186 | if [ "\$2" != "-O" ]; then |
---|
187 | echo "Expected wget usage: wget src -O dest" |
---|
188 | echo "-O not found. No action taken" |
---|
189 | else |
---|
190 | cp \$1 \$3 |
---|
191 | fi |
---|
192 | } |
---|
193 | export -f wget |
---|
194 | EOF |
---|
195 | #---------------------------------------------------------------- |
---|
196 | # Construct a script that can be used later to just redefine wget |
---|
197 | # in bash shell sessions |
---|
198 | #---------------------------------------------------------------- |
---|
199 | # |
---|
200 | cat > def_wget << EOFC |
---|
201 | #!/bin/bash |
---|
202 | if [ "\$( echo $SHELL | grep -c bash )" -lt 1 ]; then |
---|
203 | echo "WARNING: This is only going to be effective in a bash shell" |
---|
204 | echo "since it redefines the wget command as a bash function." |
---|
205 | echo "(ignore this comment if you are in a bash shell)" |
---|
206 | fi |
---|
207 | function wget { |
---|
208 | if [ "\$2" != "-O" ]; then |
---|
209 | echo "Expected wget usage: wget src -O dest" |
---|
210 | echo "-O not found. No action taken" |
---|
211 | else |
---|
212 | cp \$1 \$3 |
---|
213 | fi |
---|
214 | } |
---|
215 | export -f wget |
---|
216 | EOFC |
---|
217 | #---------------------------------------------------------------- |
---|
218 | # Make sure these scripts have execute permission |
---|
219 | #---------------------------------------------------------------- |
---|
220 | chmod 755 set_local_uspcfg |
---|
221 | chmod 755 def_wget |
---|
222 | # |
---|
223 | #---------------------------------------------------------------- |
---|
224 | # Tidy up and tar the contents of the downloaded configuration |
---|
225 | #---------------------------------------------------------------- |
---|
226 | rm cfg.tmp |
---|
227 | cd $basedir |
---|
228 | tar cvf ${3}.tar $3 |
---|
229 | echo ${3}.tar " file successfully created and prepared for local references. Move this" |
---|
230 | echo "tar file to your target system, unpack and run the set_local_uspcfg script in a " |
---|
231 | echo "bash shell to complete the process. This script redefines/defines wget as a bash" |
---|
232 | echo "function to perform local copies from this unpacked tarball. If you wish to create" |
---|
233 | echo "a new configuration based on this local copy of an unsupported configuration in " |
---|
234 | echo "future sessions then you may need to rerun the def_wget script" |
---|
235 | exit |
---|