START ESCWYP MUSIC VIDEOS
------------------------------------------------- permut.lua -----------------------

num = tonumber(arg[1])
if num == nil or num < 2 then
   num = 2
end
index = 1
old = 1
minfree = 1
f = {}
c = {}
for i=1,num do
   c[i] = -1
   f[i] = 1
   --print(f[i], c[i], index, num, old)
end
print("init fertig", num)

function freespot(ind)
   if ind > 0 then
      f[ind] = 1
      if ind < minfree then
         minfree = ind
      end
   end
end

function takespot(ind)
   if ind > 0 then
      f[ind] = 0
      if ind == minfree then
         for i=minfree+1,num do
            if (f[i] == 1) then
               minfree = i
               break
            end
         end
      end
   end
end

count = 1
while index > 0 do
   local start = c[index]+1
   if start == 0 then
      start = minfree
   end
           -- print("B", start)
   for i=start,num do
      if (f[i] == 1) then
      --if (f[i] == 1) and (i > c[index]) then
         freespot(c[index])
         takespot(i)
         c[index] = i
         if index < num then
            index = index+1
           -- print("A", index)
         else
            freespot(i)
            local s = ""
            if num > 100 then
               for j=1,10 do
                  s = s..c[j].." "
               end
               s = s.."... "
               for j=num-9,num do
                  s = s..c[j].." "
               end
            --print(s, count, "old", old, "index", index)
            else
               for j=1,num do
                  s = s..c[j].." "
               end
            end
            print(s, count)
            count = count+1
         end 
         break
      end
   end
   if index == old then
      freespot(c[index])
      c[index] = -1
      index = index-1
   end
   --[[ local s = ""
   for j=1,num do
      s = s..f[j].." "
   end --]]
   --print(s, "old", old, "index", index)
   old = index
end


START ESCWYP MUSIC VIDEOS