@@ -182,7 +182,27 @@ function config.uci_commit_all()
182182 end
183183end
184184
185+ function config .execute_hooks (action )
186+ for hook in fs .dir (config .hooksDir ) do
187+ local hookCmd = config .hooksDir .. " /" .. hook
188+ local shCmd = " sh -c 'ACTION=" .. action .. " " .. hookCmd .. " '"
189+ print (" Executed hook:" , shCmd , os.execute (shCmd ))
190+ end
191+ end
192+
193+ -- ! FIXME: This should be somewhere more appropriate
194+ function config .file_exists (name )
195+ return fs .stat (name , " type" ) == " reg"
196+ end
197+
185198function config .main ()
199+ -- ! Check whether this is the first ever run
200+ if not config .file_exists (config .uci :get_confdir () .. " /" .. config .UCI_AUTOGEN_NAME ) then
201+ config .execute_hooks (" init" )
202+ end
203+
204+ config .execute_hooks (" pre" )
205+
186206 -- ! Get mac address and set mac-based configuration file name
187207 local network = require (" lime.network" )
188208 local utils = require (" lime.utils" )
@@ -193,16 +213,15 @@ function config.main()
193213 -- ! Populate the default template configs if lime-node and lime-community
194214 -- ! are not found in /etc/config
195215 for _ , cfg_name in pairs ({config .UCI_COMMUNITY_NAME , config .UCI_NODE_NAME }) do
196- local lime_path = config .uci :get_confdir () .. " /" .. cfg_name
197- local cf = io.open (lime_path )
198- if not cf then
216+ local lime_path = config .uci :get_confdir () .. " /" .. cfg_name
217+ if not config .file_exists (lime_path ) then
199218 config .initialize_config_file (cfg_name )
200- else
201- cf :close ()
202219 end
203220 end
204221 config .uci_autogen ()
205222
223+ config .execute_hooks (" merged" )
224+
206225 local modules_name = { " hardware_detection" , " wireless" , " network" , " firewall" , " system" ,
207226 " generic_config" }
208227
@@ -221,10 +240,7 @@ function config.main()
221240 xpcall (module .configure , function (errmsg ) print (errmsg ) ; print (debug.traceback ()) end )
222241 end
223242
224- for hook in fs .dir (config .hooksDir ) do
225- local hookCmd = config .hooksDir .. " /" .. hook .. " after"
226- print (" executed hook:" , hookCmd , os.execute (hookCmd ))
227- end
243+ config .execute_hooks (" configured" )
228244
229245 local cfgpath = config .get_config_path ()
230246 -- ! flush all config changes
@@ -235,6 +251,8 @@ function config.main()
235251 ' # Instead please edit /etc/config/lime-node and/or /etc/config/lime-community files\n ' ..
236252 ' # and then regenerate this file executing lime-config\n\n ' )
237253 utils .write_file (cfgpath , notice_message .. autogen_content )
254+
255+ config .execute_hooks (" post" )
238256end
239257
240258return config
0 commit comments