#!/bin/bash

WD=`pwd`/Builds
DIR="src"
LIBS="libs"
COP="license"
GAME="Photon-Project"
TEMP=`mktemp -d`

mkdir -p Builds

if [ "$1" == "nix" ]; then {

  cp -Rv "$DIR" $TEMP
  cp -Rv "$LIBS".zip $TEMP
  cp -Rv "$COP" $TEMP
  cd $TEMP
  unzip "$LIBS".zip
  cd "$DIR"
  zip -9 -r "$GAME".love .
  cd ..
  mkdir -p Linux_Output
  cd Linux_Output
  cp -r ../"$COP" .
  cat ../"$LIBS"/linux/bin/love ../"$DIR"/"$GAME".love > ../"$LIBS"/linux/bin/"$GAME"
  chmod +x ../"$LIBS"/linux/bin/"$GAME"
  
cat << 'EOF' > ../"$LIBS"/linux/love.desktop
[Desktop Entry]
Name=The Photon Project
Comment=When the light denied the past
MimeType=application/x-love-game;
Exec=Photon-Project %f
Type=Application
Categories=Game;
Terminal=false
Icon=love
EOF

  ./../"$LIBS"/appimagetool.AppImage ../"$LIBS"/linux "$GAME".AppImage
  cd ..
  cp -r Linux_Output "$WD"
  rm -rf $TEMP

}

elif [ "$1" == "win" ]; then {

  if [ "$2" == "32" ]; then {
  
    cp -Rv "$DIR" $TEMP
    cp -Rv "$LIBS".zip $TEMP
    cp -Rv "$COP" $TEMP
    cd $TEMP
    unzip "$LIBS".zip
    cd "$DIR"
    zip -9 -r "$GAME".love .
    cd ..
    mkdir -p Win32_Output
    cd Win32_Output
    cp -r ../"$LIBS"/win32/*dll .
    cp -r ../"$COP" .
    cat  ../"$LIBS"/win32/love.exe ../"$DIR"/"$GAME".love > "$GAME".exe
    cd ..
    cp -r Win32_Output "$WD"
    rm -rf $TEMP
  
  }
  
  elif [ "$2" == "64" ]; then {
  
    cp -Rv "$DIR" $TEMP
    cp -Rv "$LIBS".zip $TEMP
    cp -Rv "$COP" $TEMP
    cd $TEMP
    unzip "$LIBS".zip
    cd "$DIR"
    zip -9 -r "$GAME".love .
    cd ..
    mkdir -p Win64_Output
    cd Win64_Output
    cp -r ../"$LIBS"/win64/*dll .
    cp -r ../"$COP" .
    cat  ../"$LIBS"/win64/love.exe ../"$DIR"/"$GAME".love > "$GAME".exe
    cd ..
    cp -r Win64_Output "$WD"
    rm -rf $TEMP
  
  }
  
  else {
  
    echo "Wrong architecture / Specify one"
  
  } fi

}

elif [ "$1" == "mac" ]; then {

  cp -Rv "$DIR" $TEMP
  cp -Rv "$LIBS".zip $TEMP
  cp -Rv "$COP" $TEMP
  cd $TEMP
  unzip "$LIBS".zip
  cd "$DIR"
  zip -9 -r "$GAME".love .
  cd ..
  mkdir -p Mac_Output
  cd Mac_Output
  cp -r ../"$COP" .
  mv ../"$LIBS"/macos/love.app ../"$LIBS"/macos/"$GAME".app
  cp -r ../"$DIR"/"$GAME".love ../"$LIBS"/macos/"$GAME".app/Contents/Resources/
  cat << EOF > ../"$LIBS"/macos/"$GAME".app/Contents/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>BuildMachineOSBuild</key>
	<string>21D49</string>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeExtensions</key>
			<array>
				<string>love</string>
			</array>
			<key>CFBundleTypeIconFile</key>
			<string>GameIcon</string>
			<key>CFBundleTypeName</key>
			<string>LÖVE Project</string>
			<key>CFBundleTypeRole</key>
			<string>Viewer</string>
			<key>LSHandlerRank</key>
			<string>Owner</string>
			<key>LSItemContentTypes</key>
			<array>
				<string>org.love2d.love-game</string>
			</array>
			<key>LSTypeIsPackage</key>
			<integer>1</integer>
		</dict>
		<dict>
			<key>CFBundleTypeName</key>
			<string>Folder</string>
			<key>CFBundleTypeOSTypes</key>
			<array>
				<string>fold</string>
			</array>
			<key>CFBundleTypeRole</key>
			<string>Viewer</string>
			<key>LSHandlerRank</key>
			<string>None</string>
		</dict>
		<dict>
			<key>CFBundleTypeIconFile</key>
			<string>Document</string>
			<key>CFBundleTypeName</key>
			<string>Document</string>
			<key>CFBundleTypeOSTypes</key>
			<array>
				<string>****</string>
			</array>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
		</dict>
	</array>
	<key>CFBundleExecutable</key>
	<string>love</string>
	<key>CFBundleIconFile</key>
	<string>OS X AppIcon</string>
	<key>CFBundleIconName</key>
	<string>OS X AppIcon</string>
	<key>CFBundleIdentifier</key>
	<string>org.PhotonTeam.PhotonProject</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>Photon Project</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>11.4a</string>
	<key>CFBundleSignature</key>
	<string>LoVe</string>
	<key>CFBundleSupportedPlatforms</key>
	<array>
		<string>MacOSX</string>
	</array>
	<key>DTCompiler</key>
	<string>com.apple.compilers.llvm.clang.1_0</string>
	<key>DTPlatformBuild</key>
	<string>13C100</string>
	<key>DTPlatformName</key>
	<string>macosx</string>
	<key>DTPlatformVersion</key>
	<string>12.1</string>
	<key>DTSDKBuild</key>
	<string>21C46</string>
	<key>DTSDKName</key>
	<string>macosx12.1</string>
	<key>DTXcode</key>
	<string>1321</string>
	<key>DTXcodeBuild</key>
	<string>13C100</string>
	<key>LSApplicationCategoryType</key>
	<string>public.app-category.games</string>
	<key>LSMinimumSystemVersion</key>
	<string>10.7</string>
	<key>NSHighResolutionCapable</key>
	<true/>
	<key>NSHumanReadableCopyright</key>
	<string>© 2006-2022 LÖVE Development Team</string>
	<key>NSPrincipalClass</key>
	<string>NSApplication</string>
	<key>NSSupportsAutomaticGraphicsSwitching</key>
	<false/>
</dict>
</plist>
EOF

  cd ../"$LIBS"/macos/
  zip -9 -r -y "$GAME".zip .
  cd $TEMP
  cp -r "$LIBS"/macos/"$GAME".zip Mac_Output
  cp -r Mac_Output "$WD"
  rm -rf $TEMP

}

elif [ "$1" == "universal" ]; then {

  cp -Rv "$DIR" $TEMP
  cp -Rv "$COP" $TEMP
  cd $TEMP
  cd "$DIR"
  zip -9 -r "$GAME".love .
  cd ..
  mkdir -p Universal_Output
  cd Universal_Output
  cp -r ../"$COP" .
  cp -r ../"$DIR"/"$GAME".love .
  cd ..
  cp -r Universal_Output "$WD"
  rm -rf $TEMP

}

else {

  echo "OS not found"

} fi
