import groovy.io.*

def debug = false
def stageName = ''
def git_link
def config_path = ''


if(params.DEBUG)
{
   debug = true
}
if(params.GIT)
{
	git_link = params.GIT
}
if(params.CFG_PATH)
{
	config_path = params.CFG_PATH
}

def listfiles(dir) {
	dlist = []
	flist = []
	new File(dir).eachDir {dlist << it.name }
	dlist.sort()
	new File(dir).eachFile(FileType.FILES, {flist << it.name })
	flist.sort()
	return (dlist << flist).flatten()
}

def printDebug(msg)
{
    println 'DEBUG: ' + msg
}

node('test')
{
	if(debug)
	{
		printDebug('FFUUCCKK')
	}
	
	fs = listfiles(pwd() + config_path)
	fs.each
	{
		println it
	}
	
   stageName = 'checkout'
   stage(stageName)
   {
		sh label: 'checkout_git', script: 'git clone ' + git_link + ' openwrt; cd openwrt'
	}
	 
	stageName = 'pre-build'
	stage(stageName)
	{
		sh label: 'patch_sources', script: 'patch -p1 < ../owrt_build_script/patches/16M.patch'
		sh label: 'feeds_update', script: './scripts/feeds update -a'
		sh label: 'feeds_install', script: './scripts/feeds install -a'
		
	}
    
   stageName = 'build'
   stage(stageName)
   {
		sh label: 'build', script: 'echo fuck you'
   }
}