#Launc command in powershell window: ./mission.ps1 E:\temp\*.miz A-10C A-10C_2    it will change all A-10C aircraft to A-10C_2 in all missions in the folder E:\temp\
#you can modify all miz files in 1 folder or specific miz file name
#E:\temp\*.miz  or E:\temp\mymission_file.miz (if the script is in the same folder with all miz file just enter *.miz or mymission_file.miz skipping full path)
$MIZ=$args[0]

#Aircraft type in original mission (A-10C in my exemple)
$ORIG_AIRCRAFT=$args[1]
#Aircraft type in new mission (A-10C_2 in my exemple
$NEW_AIRCRAFT=$args[2]

#find all miz file or specific one 
Get-ChildItem $MIZ | ForEach-Object {

  $_

# Load miz file 
try { $null = [IO.Compression.ZipFile] }
catch { [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') }

# Open miz file 
try { $fileZip = [System.IO.Compression.ZipFile]::Open( $_, 'Update' ) }
catch { throw "Another process has locked the '$_' file." }

# Find the mission file within the miz file

$fileZip.Entries | Where-Object { $_.FullName -EQ 'mission' }


#read the contents of mission file to $mission_tmp 
$desiredFile = [System.IO.StreamReader]($fileZip.Entries | Where-Object { $_.FullName -EQ 'mission' }).Open()
$mission_tmp = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()

# replace aircraft type in $mission_tmp

$mission_tmp = $mission_tmp -replace $ORIG_AIRCRAFT, $NEW_AIRCRAFT

# Re-open the file this time with streamwriter
$desiredFile = [System.IO.StreamWriter]($fileZip.Entries | Where-Object { $_.FullName -EQ 'mission' }).Open()
$desiredFile.BaseStream.SetLength(0)

# Insert the $mission_tmp to the mission file and close
$desiredFile.Write($mission_tmp -join "`r`n")
$desiredFile.Flush()
$desiredFile.Close()

# Write the changes and close the zip file
$fileZip.Dispose()

}
