[buildout]
extends = 
  ../coreutils/buildout.cfg
  ../bash/buildout.cfg

[randomsleep]
recipe = slapos.recipe.build
location = ${buildout:parts-directory}/${:_buildout_section_name_}
bin_dir = ${buildout:bin-directory}
bash_script_code =
  if [ "$#" -ne 1 ]; then
    echo "usage: randomsleep maxseconds"
    exit
  fi
  exec ${coreutils:location}/bin/sleep $((RANDOM * $1 >> 15)).$((RANDOM * 100 >> 15))
wrapper_script_code =
  #!${bash:location}/bin/bash
  . ${:location}/randomsleep.bash
script =
  os.makedirs(self.options['location'])

  bash_script_path = os.path.join(self.options['location'], 'randomsleep.bash')
  with open(bash_script_path, 'w') as f:
    f.write(self.options['bash_script_code'])

  wrapper_script_path = os.path.join(self.options['bin_dir'], 'randomsleep')
  with open(wrapper_script_path, 'w') as f:
    f.write(self.options['wrapper_script_code'])
  os.chmod(wrapper_script_path, 0o750)
