Debian Codegraph @

script/add.modules.in

raw
1 #! /bin/sh
2 #
3 # ADD.MODULES, Adds $ETC/*.modules to the $HOME/.login, .cshrc, .profile,
4 # .bashrc & .kshenv
5 # Copyright (C) 2000-2017 R.K. Owen
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 ##########################################################################
21
22 # shellcheck disable=SC2016
23 VERSION='$Id: 196cf1d4fbd7d3deecf648e85ee37ead75a60a93 $'
24 SKEL=/etc/skel
25 ETC=/skel
26 /bin/cat <<!
27
28 $0
29 adds a few lines to the beginning of your
30 $HOME/.cshrc, $HOME/.login,
31 $HOME/.profile, $HOME/.bashrc, and
32 possibly your $HOME/.kshenv (or whatever is
33 specified by the ENV environment variable).
34 !
35 if [ -r "$HOME/.bash_profile" ]; then
36 /bin/cat <<!
37 Also your $HOME/.bash_profile will be processed.
38 !
39 fi
40 if [ -r "$HOME/.bash_login" ]; then
41 /bin/cat <<!
42 Also your $HOME/.bash_login will be processed.
43 !
44 fi
45 /bin/cat <<!
46 The lines are prepended for sourcing the $ETC/csh.modules or
47 $ETC/profile.modules files or to define the module alias or function.
48 Why is it necessary?
49 To ensure that you will have access to modules for all subshells,
50 these lines need to be added to some of your 'dot' files.
51
52 Your old .cshrc, .login, .profile, .bashrc and .kshenv will be
53 renamed to .login.old, .cshrc.old, .profile.old, .bashrc.old and
54 .kshenv.old respectively. So if you have any problems you will
55 can easily restore them.
56
57 This is version $VERSION .
58
59 !
60
61 /bin/echo "Continue on (type n for no - default=yes)?\c"
62 read xxx
63
64 if [ "$xxx" = 'n' ]
65 then
66 exit 1
67 fi
68
69 # cleanse old dot file $1=file $2=clean temporary $3=file.old
70 cleandot() {
71 /bin/cat <<!
72 Cleaning $1
73 !
74 mv "$1" "$3"
75 sed \
76 -e "/^[ ]*if[ ]*([ ]*-e[ ].*\/csh\.modules[ ]*)[ ]*then[ ]*\$/,/^[ ]*endif[ ]*\$/d" \
77 -e "/^[ ]*if[ ]*\[[ ]*-[ef][ ].*\/profile\.modules[ ]*][ ]*\$/,/^[ ]*fi[ ]*\$/d" \
78 -e "/^[ ]*if[ ]*\[[ ]*-[ef][ ].*\/profile\.modules[ ]*].*then[ ]*\$/,/^[ ]*fi[ ]*\$/d" \
79 -e "/^[ ]*if[ ]*([ ]*\$?tcsh[ ]*)[ ]*then[ ]*\$/,/^[ ]*alias[ ]*module[ ]*.*\$/d" \
80 -e "/^[ ]*module().*[ ]*.*}[ ]*\$/d" \
81 -e "/^[ ]*case[ ]*\"\$0\"[ ]*in[ ]*\$/,/^[ ]*esac[ ]*\$/d" \
82 -e "/^[ ]*source[ ]*.*\/Modules.*\$/d" \
83 -e "/^[ ]*source[ ]*.*\/csh\.modules[ ]*/d" \
84 -e "/^[ ]*\.[ ]*.*\/profile\.modules[ ]*/d" \
85 "$3" > "$2"
86 }
87
88 # find if certain of the dot files have load lines already
89 findload() {
90 grep "^[ ]*module[ ]*load" "$1" > /tmp/load.$$
91 }
92
93 # put common stuff derivatives here $1=.dot_file $2=action $3=shell(csh,sh) $4=skel alternative
94 shdot() {
95 if [ -f "$1" ]
96 then
97 /bin/cat <<!
98
99 Processing your $1 (your old one is $1.old)
100 !
101 if cleandot "$1" "/tmp/$1.$$" "$1.old"
102 then
103 if [ "$2" = 'source' ]
104 then
105 /bin/cat <<!
106 Adding sourcing lines at beginning of "$1"
107 !
108 if [ "$3" = 'sh' ]
109 then
110 findload "$1.old"
111 /bin/cat > "$1" <<!
112 if [ -f $ETC/profile.modules ]
113 then
114 . $ETC/profile.modules
115 # put your own module loads here
116 !
117 if [ -s /tmp/load.$$ ]
118 then
119 /bin/cat /tmp/load.$$ >> "$1"
120 else
121 /bin/cat >> "$1" <<!
122 module load null
123 !
124 fi
125 /bin/cat >> "$1" <<!
126 fi
127 !
128 elif [ "$3" = 'csh' ]
129 then
130 findload "$1.old"
131 /bin/cat > "$1" <<!
132 if ( -e $ETC/csh.modules ) then
133 source $ETC/csh.modules
134 # put your own module loads here
135 !
136 if [ -s /tmp/load.$$ ]
137 then
138 /bin/cat /tmp/load.$$ >> "$1"
139 else
140 /bin/cat >> "$1" <<!
141 module load null
142 !
143 fi
144 /bin/cat >> "$1" <<!
145 endif
146 !
147 fi
148 /bin/cat "/tmp/$1.$$" >> "$1" && /bin/rm "/tmp/$1.$$"
149 elif [ "$2" = 'alias' ]
150 then
151 /bin/cat <<!
152 Adding alias or function lines at beginning of $1
153 !
154 if [ "$3" = 'sh' ]
155 then
156 /bin/cat > "$1" <<!
157 case "\$0" in
158 -sh|sh|*/sh) modules_shell=sh ;;
159 -ksh|ksh|*/ksh) modules_shell=ksh ;;
160 -zsh|zsh|*/zsh) modules_shell=zsh ;;
161 -bash|bash|*/bash) modules_shell=bash ;;
162 esac
163 module() { eval \`@TCLSH@ '@libexecdir@/modulecmd.tcl' \$modules_shell \$*\`; }
164 !
165 elif [ "$3" = 'csh' ]
166 then
167 /bin/cat > "$1" <<!
168 if (\$?tcsh) then
169 set modules_shell="tcsh"
170 else
171 set modules_shell="csh"
172 endif
173 alias module 'eval \`@TCLSH@ "@libexecdir@/modulecmd.tcl" '\$modules_shell '\!*\`'
174 !
175 fi
176 /bin/cat "/tmp/$1.$$" >> "$1" && /bin/rm "/tmp/$1.$$"
177 fi
178 else
179 /bin/echo "Had problems with your $1"
180 fi
181 else
182 /bin/cat <<!
183 You had no $1 as I see it. Copying $4 for you.
184 !
185 /bin/cp "$4" "$1"
186 fi
187 /bin/rm /tmp/load.$$ 2> /dev/null
188 }
189
190 # process files in $HOME
191 cd "$HOME" || exit 1
192 if [ -r .bash_profile ]; then
193 shdot .bash_profile source sh $SKEL/.profile
194 fi
195 if [ -r .bash_login ]; then
196 shdot .bash_login source sh $SKEL/.profile
197 fi
198 shdot .profile source sh $SKEL/.profile
199 shdot .bashrc alias sh $SKEL/.kshenv
200 shdot "$(basename "${ENV:=.kshenv}")" alias sh $SKEL/.kshenv
201 shdot .login source csh $SKEL/.login
202 shdot .cshrc alias csh $SKEL/.cshrc
203
204 exit