<cfcomponent>
	
	<cffunction name="init" returntype="void" output="no" hint="">
		<cfargument name="path" type="string">

		<cfparam name="this.oInstall" default="#createobject("component","farcry.core.webtop.install.components.install").init(this)#" />

		<cfset systemoutput("path:"&path)>
	</cffunction>
    
    
	<cffunction name="install" returntype="string" output="no" hint="called from Railo to install application">
		<cfargument name="error" type="struct">
		<cfargument name="path" type="string">
		<cfargument name="config" type="struct">

		<cfparam name="this.oInstall" default="#createobject("component","farcry.core.webtop.install.components.install").init(this)#" />

		<cfset var stResult = structnew() />
		<cfset var qPlugins = this.oInstall.getPlugins() />
		<cfset var plugins = "" />
		<cfset var thisplugin = "" />

		<cfset arguments.config.mixed.webroot = expandpath("{web-root-directory}") />

		<cfset arguments.config.mixed.pluginwebroots = "" />
		<cfloop list="#arguments.config.mixed.plugins#" index="thisplugin">
			<cfif FindNoCase("addWebrootMapping",thisplugin) neq 1>
				<cfset plugins = listappend(plugins,thisplugin) />
				<cfif ListFindNoCase(config.mixed.plugins,"addWebrootMapping"&thisplugin)>
					<cfset arguments.config.mixed.pluginwebroots = listappend(arguments.config.mixed.pluginwebroots,thisplugin) />
				</cfif>
			</cfif>
		</cfloop>
		<cfset arguments.config.mixed.plugins = plugins />

		<cfset arguments.config.mixed.dbtype = listFirst(config.mixed.datasource,':')/>
		<cfset arguments.config.mixed.dsn = mid(config.mixed.datasource,len(arguments.config.mixed.dbtype)+2,1000) />
		<cfset arguments.config.mixed.bInstallDBOnly = false />

		<cfset stResult = this.oInstall.install(argumentCollection=arguments.config.mixed) />

		<cfsavecontent variable="confirm"><cfoutput>
			<div>
				<div class="item">
					<h2><strong>Congratulations!</strong>  Your application has sucessfully installed.</h2>
					<p>The installer has created an administration account for you to logon to the FarCry webtop:</p>
					<p>&nbsp;</p>

					<ul>
						<li>Username: <strong>#stResult.adminUser#</strong></li>
						<li>Password: <strong>#stResult.adminPassword#</strong></li>
					</ul>

					<p>&nbsp;</p>
					<p class="warning">WARNING: Be sure to <strong>change this account</strong> information on your first login for security reasons.</p>
					<p>&nbsp;</p>

				</div>
				<div class="itemButtons">
					<form name="installComplete" id="installComplete" method="post" action="">
						<input type="button" name="login" value="LOGIN TO THE FARCRY WEBTOP" onClick="alert('Your default Farcry login is\n\n u: #stResult.adminUser#\n p: #jsstringformat(stResult.adminPassword)#');window.open('#stResult.webtopURL#')" class="normalbttnstyle" onMouseOver="this.className='overbttnstyle'" onMouseOut="this.className='normalbttnstyle'" />
						<input type="button" name="view" value="VIEW SITE" onClick="window.open('#stResult.siteURL#')" class="normalbttnstyle" onMouseOver="this.className='overbttnstyle'" onMouseOut="this.className='normalbttnstyle'" />
						<input type="button" name="install" value="INSTALL ANOTHER PROJECT" onClick="window.open('#cgi.script_name#?restartInstaller=1', '_self')" class="normalbttnstyle" onMouseOver="this.className='overbttnstyle'" onMouseOut="this.className='normalbttnstyle'" />
					</form><br /> 
				</div>
			</div>
		</cfoutput></cfsavecontent>

		<cfreturn confirm>
	</cffunction>
    
	<cffunction name="uninstall" returntype="string" output="no" hint="called from Railo to uninstall application">
		<cfargument name="path" type="string">
		<cfargument name="config" type="struct">

		<!--- TODO uninstall code --->

		<cfreturn 'Farcry is now successfully removed'>
	</cffunction>
	
	<!--- validate ---->
	<cffunction name="validate" returntype="void" output="no" hint="called to validate values">
		<cfargument name="error" type="struct">
		<cfargument name="path" type="string">
		<cfargument name="config" type="struct">
		<cfargument name="step" type="numeric">

		<cfparam name="this.oInstall" default="#createobject("component","farcry.core.webtop.install.components.install").init(this)#" />

		<cfset var stConfig = structnew() />

			<cfset this.path=arguments.path>

		<cfif step EQ 1>

			<cfset error.fields = this.oInstall.validateDetails(config.mixed) />

		<cfelseif step EQ 2>

			<cfset stConfig.dbtype = listFirst(config.mixed.datasource,':') />
			<cfset stConfig.dsn = mid(config.mixed.datasource,len(stConfig.dbtype)+2,1000) />
			<cfset stConfig.dbowner = config.mixed.dbowner />
			<cfset error.fields = this.oInstall.validateDatabase(stConfig) />

		<cfelseif step EQ 3>

			<cfset error.fields = this.oInstall.validateSkeleton(arguments.config.mixed) />

		<cfelseif step eq 4>

			<cfset error.fields = this.oInstall.validatePlugins(arguments.config.mixed) />

		</cfif>
	</cffunction>


	<cffunction name="setProgress" returntype="void" output="yes" hint="Used by installer to update progress">
		<cfargument name="progressmessage" type="string" required="true" hint="Message describing progress" />
		<cfargument name="progress" type="numeric" reqired="true" hint="Number between 0 and 1" />
		
		
		<cfset systemoutput("value:"&arguments.progress&";text"&arguments.progressmessage) />
	</cffunction>

	<cffunction name="setError" returntype="void" output="yes" hint="Used by installer to notify about errors">
		<cfargument name="error" type="struct" required="true" hint="CFCatch struct" />
		
		<cfdump var="#arguments.error#">
		<cfabort>
	</cffunction>


	<!--- form values ---->
	<cffunction name="fillAdminPassword" returntype="void" output="no" hint="called from form generator to create dynamic forms">
		<cfargument name="item" required="yes" hint="item cfc">

		<cfset item.setValue('susi')>
	</cffunction>

	<cffunction name="fillUpdateApplicationKey" returntype="void" output="no" hint="called from form generator to create dynamic forms">
		<cfargument name="item" required="yes" hint="item cfc">

		<cfset item.setValue('peter')>
	</cffunction>
    
	<cffunction name="fillLocales" returntype="void" output="no" hint="called from form generator to create dynamic forms">
		<cfargument name="item" required="yes" hint="item cfc">
	
		<cfset var key="">

		<cfadmin action="getLocales" returnVariable="locales">
		<cfadmin action="getRegional" type="#request.adminType#" password="#session["password"&request.adminType]#" returnVariable="regional">

		<cfset keys=structSort(locales,'textnocase')>
		<cfset item.createOption(value:'',label:'-- Select Locale --')>
		<cfloop collection="#keys#" item="i">
			<cfset key=keys[i]>
			<cfset item.createOption(value:key,label:locales[key]&"",selected:key EQ regional.locale)>
		</cfloop>
	</cffunction>
    
    
    
	<!--- fillDatasources ---->
	<cffunction name="fillDatasources" returntype="void" output="no" hint="select datasource">
		<cfargument name="item" required="yes" hint="item cfc">

		<cfset var datasources="">
		<cfset var type="">
		<cfadmin action="getDatasources" type="#request.adminType#" password="#session["password"&request.adminType]#" returnVariable="datasources">

		<cfset has=false>

		<cfset item.createOption(value:':',label:'-- Select Datasource --')>
		<cfloop query="datasources">
			<cfif findNoCase('microsoft',datasources.classname ) or findNoCase('jtds',datasources.classname )>
				<cfset type="mssql">
			<cfelseif findNoCase('mysql',datasources.classname )>
				<cfset type="mysql">
			<cfelseif findNoCase('oracle',datasources.classname )>
				<cfset type="ora">
			<cfelseif findNoCase('postgre',datasources.classname )>
				<cfset type="postgresql">
			<cfelse>
				<cfcontinue>
			</cfif>
			<cfset has=true>
			<cfset item.createOption(value:type&":"&datasources.name,label:datasources.name&" ("&datasources.host&")")>
		</cfloop>
		<cfif not has>
			<cfthrow message="There is no MySQL, MSSQL, Oracle or Postgre SQL datasource defined. Please define a corresponding datasource first.">
		</cfif>
	</cffunction>
    
	<cffunction name="fillSkeleton" returntype="void" output="no" hint="select Skeleton">
		<cfargument name="item" required="yes" hint="item cfc">

		<cfset touchFarcry()>

		<cfparam name="this.oInstall" default="#createobject("component","farcry.core.webtop.install.components.install").init(this)#" />

		<cfset var qSkeletons = this.oInstall.getSkeletons() />
		
		<cfset item.createOption(value:"",label:"-- Select Skeleton --") />
		<cfloop query="qSkeletons">
			<cfset manifest = createObject("component", "#qSkeletons.value#.install.manifest")>
			<cfset item.createOption(value:qSkeletons.value,label:qSkeletons.label)>
		</cfloop>
	</cffunction>

	<cffunction name="touchFarcry" returntype="void" output="no" hint="deploy farcry folder to webroot">
		<!--- TODO handle if there is a old farcry version --->
		<cfset var farcry=expandPath('{web-root-directory}/farcry/')>
		<cfif not DirectoryExists(farcry)>
			<cfdirectory action="rename" directory="#expandpath('/install/farcry/')#" newdirectory="#farcry#">
		</cfif>
	</cffunction>
    
    
	<!--- fillPlugins ---->
	<cffunction name="fillPlugins" returntype="void" output="no">
		<cfargument name="group" required="yes" hint="group cfc">

		<cfset var item="">
		<cfset var qPlugins="">
		<cfset var oManifest="">
		<cfset var pluginSupported = "">
		<cfset var pluginPath =expandpath('{web-root-directory}/farcry/plugins')>
		<cfset touchFarcry()>

		<cfparam name="this.oInstall" default="#createobject("component","farcry.core.webtop.install.components.install").init(this)#" />

		<cfset var qPlugins = this.oInstall.getPlugins() />

		<cfset systemoutput("++"&qPlugins.recordcount&"
")>

		<cfloop query="qPlugins">
			<cfset item=group.createItem(
									type:'checkbox',
									name:"plugins",
									label:qPlugins.label& iif(qPlugins.supported,de(''),de(' (unsupported)'))
							
									)>
			<cfset item.createOption(
									value:"#qPlugins.value#",
									label:qPlugins.description
									)>
	
			<cfif qPlugins.requiresmapping>
	
				<cfset item.createOption(
									value:"addWebrootMapping#qPlugins.value#",
									label:"Copy Plugin Webroot to project",
									description:"This plugin requires a webroot mapping. You can create the webroot mapping on your webserver, or alteratively you can select to have the webroot copied into your project to avoid having to create the mapping.")>
	
			
			</cfif>
		</cfloop>
	</cffunction>

	

</cfcomponent>