#! /usr/bin/python
# -*- coding=utf-8 -*-

"""
    This file is part of Torrent Search.
    
    Torrent Search is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Torrent Search is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from TorrentSearch.informations import *
import os

PATH=os.path.split(__file__)[0]
EXEPATH=PATH+"/dist"

def walk_files(res,path,files):
   if ".svn" in path:
      return
   for i in files:
      filename=path+"/"+i
      if os.path.isfile(filename) and i[-1]!="~" and i[-4:]!=".bak":
         res.append((path,i))

if __name__=='__main__':
   files=[]
   os.path.walk(EXEPATH,walk_files,files)
   sources=[]
   rmfiles=[]
   for path,i in files:
      winpath="y:\\"+path[len(PATH):].replace("/","\\")
      sources.append("Source: \""+winpath+"\\"+i+"\"; DestDir: \"{app}"+path[len(EXEPATH):].replace("/","\\")+"\"")
      rmfiles.append("Type: files; Name: \"{app}"+path[len(EXEPATH):].replace("/","\\")+"\\"+i+"\"")
   data="""[Setup]
AppName=%APPNAME%
AppVersion=%VERSION%
AppCopyright=Copyright (C) 2010 Gwendal Le Bihan
AppContact=gwendal.lebihan.dev@gmail.com
AppPublisher=Gwendal Le Bihan
AppPublisherURL=http://torrent-search.sourceforge.net/
AppSupportURL=http://sourceforge.net/projects/torrent-search/support
DefaultDirName={pf}\\%APPNAME%
DefaultGroupName=%APPNAME%
UninstallDisplayIcon={app}\\%UNIXNAME%.exe
Compression=lzma2
SolidCompression=yes
OutputDir=userdocs:Inno Setup Examples Output
SetupIconFile=torrent-search.ico

[Files]
"""
   for i in sources:
      data+=i+"\n"
   data+="""
[InstallDelete]
"""
   for i in rmfiles:
      data+=i+"\n"
   data+="""
[Icons]
Name: "{group}\\Torrent Search"; Filename: "{app}\\torrent-search.exe"
Name: "{group}\\Uninstall %APPNAME%"; Filename: "{uninstallexe}"
Name: "{commondesktop}\\Torrent Search"; Filename: "{app}\\torrent-search.exe"; Components: icons; Tasks: desktopicon
Name: "{userappdata}\\Microsoft\\Internet Explorer\\Quick Launch\\Torrent Search"; Filename: "{app}\\torrent-search.exe"; Components: icons; Tasks: quicklaunchicon

[Tasks]
Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: "{cm:AdditionalIcons}:"; Components: icons
Name: quicklaunchicon; Description: {cm:CreateQuickLaunchIcon}; GroupDescription: "{cm:AdditionalIcons}:"; Components: icons; Flags: unchecked

[Components]
Name: icons; Description: {cm:AdditionalIcons}; Types: full compact custom; Flags: fixed

[Languages]
Name: "en"; MessagesFile: "compiler:Default.isl"
Name: "nl"; MessagesFile: "compiler:Languages\\Dutch.isl"
Name: "fr"; MessagesFile: "compiler:Languages\\French.isl"
Name: "pl"; MessagesFile: "compiler:Languages\\Polish.isl"
Name: "ru"; MessagesFile: "compiler:Languages\\Russian.isl"
Name: "de"; MessagesFile: "compiler:Languages\\German.isl"

[Run]
Filename: "{app}\%UNIXNAME%.exe"; Description: "{cm:LaunchProgram,%APPNAME%}"; Flags: postinstall nowait skipifsilent
"""
   data=data.replace("%APPNAME%",APPNAME)
   data=data.replace("%UNIXNAME%",UNIXNAME)
   data=data.replace("%VERSION%",VERSION)
   f=open(PATH+"/torrent-search.iss","w")
   f.write(data)
   f.close()