gen.conf<-function(datafile='run/sam.dat', conf='conf/model.cfg', retro='conf/retro.cfg', leaveout='conf/leaveout.cfg'){

  lin<-readLines(datafile)
  idx<-grep('# Fleet types',lin)
  fleetTypes<-as.integer(strsplit(sub("^*[[:blank:]]",'',lin[idx+1]),' ')[[1]])
  idx1<-grep('# The observation matrix',lin)
  idx2<-grep('# Proportion mature',lin)
  dat<-do.call(rbind,lapply(strsplit(lin[(idx1+1):(idx2-1)],' '),as.numeric))
  ages<-do.call(rbind,tapply(dat[,3], INDEX=dat[,2], FUN=range))
  minAge<-min(ages)
  maxAge<-max(ages)
  nAges<-maxAge-minAge+1
  nFleets<-nrow(ages)

  if(!file.exists(conf)){
    cfg<-list()
    l<-function()length(cfg)+1
    pushMat<-function(m){
      for(i in 1:nrow(m)){
        cfg[l()]<<-paste(m[i,], collapse="\t")
      }  
    }
    pushVec<-function(v){
        cfg[l()]<<-paste(v, collapse="\t")
    }
    setSeq<-function(min,max){
      if(min==max){
        ret<-1
      }else{
        ret<-c(1:(max-min),max-min)
      }
      return(ret)
    }
    cfg[l()]<-" # Min Age (should not be modified unless data is modified accordingly)"
    cfg[l()]<-minAge
    cfg[l()]<-"# Max Age (should not be modified unless data is modified accordingly)"
    cfg[l()]<-maxAge
    cfg[l()]<-"# Max Age considered a plus group (0=No, 1=Yes)"
    cfg[l()]<-1
    cfg[l()]<-"# The following matrix describes the coupling" 
    cfg[l()]<-"# of fishing mortality STATES"
    cfg[l()]<-"# Rows represent fleets." 
    cfg[l()]<-"# Columns represent ages."
    x<-matrix(0, nrow=nFleets, ncol=nAges)
    lastMax<-0
    for(i in 1:nrow(x)){
      if(fleetTypes[i]==0){
        x[i,(ages[i,1]-minAge+1):(ages[i,2]-minAge+1)]<-setSeq(ages[i,1],ages[i,2])+lastMax
        lastMax<-max(x)
      }
    }  
    pushMat(x)
  
    cfg[l()]<-"# Use correlated random walks for the fishing mortalities"
    cfg[l()]<-"# ( 0 = independent, 1 = correlation estimated)"
    cfg[l()]<-0
    cfg[l()]<-"# Coupling of catchability PARAMETERS"
    x<-matrix(0, nrow=nFleets, ncol=nAges)
    lastMax<-0
    for(i in 1:nrow(x)){
      if(fleetTypes[i]%in%c(1,2)){
        x[i,(ages[i,1]-minAge+1):(ages[i,2]-minAge+1)]<-setSeq(ages[i,1],ages[i,2])+lastMax
        lastMax<-max(x)
      }
    }  
    pushMat(x)
     
    cfg[l()]<-"# Coupling of power law model EXPONENTS (if used)"
    x<-matrix(0, nrow=nFleets, ncol=nAges)
    pushMat(x)
  
    cfg[l()]<-"# Coupling of fishing mortality RW VARIANCES"
    x<-matrix(0, nrow=nFleets, ncol=nAges)
    lastMax<-0
    for(i in 1:nrow(x)){
      if(fleetTypes[i]==0){
        x[i,(ages[i,1]-minAge+1):(ages[i,2]-minAge+1)]<-lastMax+1
        lastMax<-max(x)
      }
    }  
    pushMat(x)
  
    cfg[l()]<-"# Coupling of log N RW VARIANCES"
    x<-c(1,rep(2,nAges-1))  
    pushVec(x)
  
    cfg[l()]<-"# Coupling of OBSERVATION VARIANCES"
    x<-matrix(0, nrow=nFleets, ncol=nAges)
    lastMax<-0
    for(i in 1:nrow(x)){
      if(fleetTypes[i]%in%c(0,1,2)){
        x[i,(ages[i,1]-minAge+1):(ages[i,2]-minAge+1)]<-lastMax+1
        lastMax<-max(x)
      }
    }  
    pushMat(x)
  
    cfg[l()]<-"# Stock recruitment model code (0=RW, 1=Ricker, 2=BH, ... more in time)"
    cfg[l()]<-0
  
    cfg[l()]<-"# Years in which catch data are to be scaled by an estimated parameter" 
    cfg[l()]<-0
    cfg[l()]<-"# first the number of years" 
    cfg[l()]<-"# Then the actual years" 
    cfg[l()]<-"# Them the model config lines years cols ages" 
   
    cfg[l()]<-"# Define Fbar range" 
    cfg[l()]<-paste(ages[fleetTypes==0,,drop=FALSE][1,], collapse='\t')
    cat(unlist(lapply(cfg, function(x)paste(x,'\n',sep=''))), file=conf)
  }
  if(!file.exists(retro)){
    ret<-list()
    l<-function()length(ret)+1
    ret[l()]<-" # This file configures the retrospective runs. "
    ret[l()]<-"#"
    ret[l()]<-"# Each line corresponds to a single run, and each"
    ret[l()]<-"# column to a fleet."
    ret[l()]<-"#"
    ret[l()]<-"# The number indicates how many years the corresponding"
    ret[l()]<-"# fleet is reduced by in that run"   
    for(i in 1:5){
      ret[l()]<-paste(rep(i,nFleets), collapse='\t')
    }
    cat(unlist(lapply(ret, function(x)paste(x,'\n',sep=''))), file=retro)
  }
  if(!file.exists(leaveout)){
    lo<-list()
    l<-function()length(lo)+1
    lo[l()]<-" # This file configures the leave out runs. "
    lo[l()]<-"#"
    lo[l()]<-"# Each line corresponds to a single run, and each"
    lo[l()]<-"# column to a fleet."
    lo[l()]<-"#"
    lo[l()]<-"# A number of 0 indicates that all data is use for the fleet "
    lo[l()]<-"# A number of -1 indicates that data is not used for the fleet"   
    for(i in 1:(nFleets-1)){
      x<-rep(0,nFleets)
      x[i+1] <- -1
      lo[l()]<-paste(x, collapse='\t')
    }
    cat(unlist(lapply(lo, function(x)paste(x,'\n',sep=''))), file=leaveout)
  }
}
