-- Util System
----------------------------------------------------------
-- Last update: April 14, 2008 by Renaud
-- KNOWN ISSUES:
-- 
-- 
-- 
----------------------------------------------------------



--on getWritableFolder
--  if isMac() then
--    saveFolder = baSysfolder("pref")
--    if saveFolder = "" then
--      return the moviePath
--    else
--      return saveFolder
--    end if
--  else
--    saveFolder = baSysFolder( "35" )
--    if saveFolder = "" then
--      return the MoviePath
--    else
--      return saveFolder
--    end if
--  end if
--end

on getWritableFolder
  if isMac() then
    saveFolder = baSysfolder("pref")
    if saveFolder = "" then
      return the moviePath
    else
      return saveFolder
    end if
  else
    saveFolder = baSysFolder("40")  -- was 35
    if saveFolder = "" then
      return the MoviePath
    else
      -- see what os we're using
      osVer = (_system.environmentPropList.osVersion).item[2]
      case osVer of
        6: -- vista
          return saveFolder & "AppData\Roaming\"
                  
        otherwise: -- other
          saveFolder = baSysFolder("35")
          if saveFolder = "" then
            return the moviePath
          else
            return saveFolder
          end if
      end case
      
    end if
  end if
end

on getFile(fileName) 
  filename = macTheAddress(filename)

  IOObject = new(xtra "fileio") 
  IOObject.openFile(filename, 1) 
  str = IOObject.readFile()
  IOObject.closeFile()
  IOObject = void 
  return str 
end

on setFile(fileName, stringdata)
  filename = macTheAddress(filename)
 
  IOObject = new(xtra "fileio") 
  -- if file exists, delete it 
  IOObject.openFile(fileName, 0) 
  if IOObject.status()=0 then 
    IOObject.delete() 
    IOObject.closeFile()
  end if

  -- create new file 
  IOObject.createFile(fileName) 
  -- open file 
  IOObject.openFile(fileName, 0) 
  -- write to it 
  IOObject.writeString(stringData)
  -- close the file 
  IOObject.closeFile()
  -- clear memory 
  IOObject = void 
end


on isMac
  if the platform contains "mac" then
    return true
  else
    return false
  end if
end

on macTheAddress theAdress
  if isMac() then
    repeat with counter = 1 to theAdress.length
      if chars(theAdress,counter,counter) = "\" then
        put ":" into char counter of theAdress
      end if
    end repeat
    return theAdress
  else
    return theAdress
  end if
end

on GetAuthor()
  global gMovie_Util_System_Authoring
  
  if gMovie_Util_System_Authoring = void then
    if the environment.runmode contains "Author" then
      gMovie_Util_System_Authoring = #yes
    else
      gMovie_Util_System_Authoring = #no
    end if
  end if
  
  
  if gMovie_Util_System_Authoring = #yes then
    return true
  else
    return false
  end if
  
  
end


on SaveSysInfo()
  
  currSys = getpref("SystemInfo")
  
  if currSys = "" or currSys = void then
    
    specs = ""
    
    -- OPERATING SYSTEM STUFF --
    WinVer = baVersion( "os" )
    SP = "no-Sp"
    Ver = "no-Ver"
    
    if WinVer = "WinNT" or WinVer = "Win2000" or  WinVer = "WinXP" then
      SP = baVersion( "service pack" )
      Ver = baVersion( "nt type" )
    end if
    
    -- OPERATING SYSTEM STUFF --
    
    -- CPU STUFF --    
    
    CpuId = baCpuInfo( "vendor" )
    Cpu = baCpuInfo( "type" )
    cpuspeed = bacpuinfo("speed")
    
    -- CPU STUFF --    
    
    -- RAM --
    
    ram = baMemoryInfo( "ram" )
    ram = ram / 1000000
    ram = integer(ram)
    ram = string(ram) & "MB-Ram"
    fram = baMemoryInfo( "free ram" )
    fram = fram / 1000000
    fram = integer(fram)
    fram = string(fram) & "MB-FreeRam"
    
    swp = baMemoryInfo( "swap" )
    swp = swp / 1000000
    swp = integer(swp)
    swp = string(swp) & "MB-Swap"
    fswp = baMemoryInfo( "free swap" )
    fswp = fswp / 1000000
    fswp = integer(fswp)
    fswp = string(fswp) & "MB-FreeSwap"
    
    -- RAM --
    
    -- HardDrives --
    
    disks = baDiskList( )
    c = disks.count
    
    driver = ""
    
    repeat with b = 1 to c
      
      Siz = baDiskInfo( disks[b].char[1] , "size" )
      Siz = Siz / 1000
      Siz = integer(Siz)
      Siz = string(Siz) & "MB-Total"
      typ = baDiskInfo( disks[b].char[1] , "type" )
      fre = baDiskInfo( disks[b].char[1] , "free" )
      fre = fre / 1000
      fre = integer(fre)
      fre = string(fre) & "MB-Free"
      
      driver = driver & disks[b] & typ && siz && fre & " "
      
    end repeat
    
    -- HardDrives --
    
    specs = "*" & WinVer && SP && Ver & "*" && CpuId && Cpu && cpuSpeed & "*" && ram && fram && swp && fswp & "*" && driver & "*" && the moviePath
    
    setPref("SystemInfo", specs)
    
  end if
  
end
