#!/bin/bash -p
DMG="$1"

MNT=$(hdiutil attach "${DMG}" -nobrowse -readonly | awk -F '\t' '/\/Volumes\// {print $3; exit}')

QUARK_NAME="Quark"
CLOUD_DRIVE_NAME="quark-cloud-drive"
DEST="/Applications/${QUARK_NAME}.app"
QUARK_USER_DATA_DIR="${HOME}/Library/Application Support/${QUARK_NAME}"
ONE_QUARK_DIR="${QUARK_USER_DATA_DIR}/one-quark-migration"
CLOUD_DRIVE_USER_DATA_DIR="${HOME}/Library/Application Support/${CLOUD_DRIVE_NAME}"
CLOUD_DRIVE_INSTALL_DIR="/Applications/夸克网盘.app"

report_result() {
	local PORT="$1"
	local INSTALL_RESULT="$2"
	local PAY_LOAD
	if [ "$INSTALL_RESULT" -eq 1 ]; then
		PAY_LOAD="${DEST}/Contents/MacOS/${QUARK_NAME}"
	else
		PAY_LOAD="UAC_BREAK"
	fi
	local CODE
	if [ "$INSTALL_RESULT" -eq 1 ]; then
		CODE="OK"
	else
		CODE="FAIL"
	fi

	curl -G \
		--connect-timeout 1 \
		--max-time 3 \
		--data-urlencode "channel=ONE_QUARK_INSTALLER" \
		--data-urlencode "code=${CODE}" \
		--data-urlencode "payload=${PAY_LOAD}" \
		"http://127.0.0.1:${PORT}/external_app_message"
}

notify_result() {
	local INSTALL_RESULT="$1"
	report_result 9125 $INSTALL_RESULT || \
	report_result 9126 $INSTALL_RESULT || \
	report_result 9127 $INSTALL_RESULT
}

cleanup() {
	if [ "$1" -ne 0 ]; then
		notify_result 0
	else
		notify_result 1
	fi
	hdiutil detach "${MNT}"
}
trap 'cleanup $?' EXIT

if [[ -d "$DEST" ]]; then
	exit 1
else
	rsync --links --recursive --perms --times --delete "$MNT/$QUARK_NAME.app/" "$DEST" || exit 1

	CHMOD_MODE="a+rX,u+w,go-w"
	if [ ${EUID} -ne 0 ] ; then
	  if [ "${DEST:0:14}" = "/Applications/" ] &&
	     chgrp -Rh admin "${DEST}" >& /dev/null ; then
	    CHMOD_MODE="a+rX,ug+w,o-w"
	  fi
	else
	  chown -Rh root:wheel "${DEST}" >& /dev/null
	fi

	chmod -R "${CHMOD_MODE}" "${DEST}" >& /dev/null
	find "${DEST}" -type l -exec chmod -h "${CHMOD_MODE}" {} + >& /dev/null
	QUARANTINE_ATTR=com.apple.quarantine
	xattr -d -r "${QUARANTINE_ATTR}" "${DEST}" >& /dev/null

	cat > "${QUARK_USER_DATA_DIR}/d479ffb69b84efa2" <<EOF
{
	"source_type": "ClouddriveUpdate",
	"pre_install_state": "2"
}
EOF

	mkdir -p "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/config.json" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/Cookies" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/Cookies-journal" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/preference.json" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/IndexedDB" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_USER_DATA_DIR}/Local Storage" "$ONE_QUARK_DIR"
	cp -Rf "${CLOUD_DRIVE_INSTALL_DIR}/Contents/Resources/assets/extensions/darwin/config.txt" "$ONE_QUARK_DIR/drive-app-config.txt"

	UTDID=""
	APPCH=""
	DRIVE_AUTO_STARTUP="0"
	if [ -e "${CLOUD_DRIVE_USER_DATA_DIR}/config.json" ]; then
		UTDID=$(jq -r '.utdid' "${CLOUD_DRIVE_USER_DATA_DIR}/config.json")
		APPCH=$(jq -r '.appch' "${CLOUD_DRIVE_USER_DATA_DIR}/config.json")
	fi
	if [ -e "${CLOUD_DRIVE_USER_DATA_DIR}/preference.json" ]; then
		AUTO=$(jq -r '.[ "global:setting" ].permissionSwitch.autoLaunchEnable' "${CLOUD_DRIVE_USER_DATA_DIR}/preference.json")
		if [ "$AUTO" = "true" ]; then
			DRIVE_AUTO_STARTUP="1"
		else
			DRIVE_AUTO_STARTUP="0"
		fi
	fi
	cat > "${ONE_QUARK_DIR}/pkg_install_info" <<EOF
{
	"from": "clouddrive",
	"phase": "background",
	"brand": "clouddrive",
	"install_time": "$(date +%s%3N)",
	"drive_app_install_path": "/Applications/夸克网盘.app",
	"drive_app_installed": "1",
	"session_id": "0",
	"utdid": "$UTDID",
	"appch": "$APPCH",
	"drive_auto_startup": "$DRIVE_AUTO_STARTUP"
}
EOF
fi
