#!/usr/bin/python
# Creates timelapse by taking images from differnt reordered camera angles
import os,shutil
camdir='/mnt/max/nick/cams'
mergedir='/mnt/max/nick/cams/baeren_switched'
path=lambda x : '%s/baerenpark%d/' % (camdir,x)
zahlen=map(str,range(10))
ut=lambda fn : int(filter(lambda x:x in zahlen,os.path.basename(fn)))
percam=25*5  # five seconds are just about a day
camlist=range(8) # all cams
camlist=[0,1,2,3,6,7] # without close-up on the bear-hill

def find_nearest(pf,t):
 mindif,minimg=1e10,False
 for fi in os.listdir(pf):
  dif=abs(ut(fi)-t)
  if dif<mindif and ut(fi)>t: mindif=dif; minimg=fi
 return minimg

if not os.path.isdir(mergedir): os.mkdir(mergedir)
current_time=0
while True:
 for camnum in camlist:
  files=sorted(os.listdir(path(camnum)))
  start_photo=find_nearest(path(camnum),current_time)
  if start_photo not in files: continue
  start_idx=files.index(start_photo)
  for i in range(percam):
   current_idx=start_idx+i
   if current_idx>=len(files): continue
   current_photo=files[current_idx]
   current_time=ut(current_photo)
   source=path(camnum)+current_photo
   print source,current_time
   shutil.copy(source,mergedir)
 if start_photo not in files: break