// Used by CI to stamp a mobile release build with its version number.
// Updates version.json (read by hooks/useUpdatecheck.tsx) and app.json's
// android.versionCode/versionName so the build, the in-app update check,
// and the Gitea release tag (mobile-vN) all agree on the same number.
const fs = require('fs');
const path = require('path');

const version = parseInt(process.argv[2], 10);
if (!Number.isInteger(version) || version <= 0) {
    console.error('Usage: node set-mobile-version.js <positive integer>');
    process.exit(1);
}

const root = path.join(__dirname, '..');

fs.writeFileSync(
    path.join(root, 'version.json'),
    JSON.stringify({ version }, null, 2) + '\n',
);

const appJsonPath = path.join(root, 'app.json');
const appJson = JSON.parse(fs.readFileSync(appJsonPath, 'utf8'));
appJson.expo.version = `1.0.${version}`;
appJson.expo.android = appJson.expo.android ?? {};
appJson.expo.android.versionCode = version;
fs.writeFileSync(appJsonPath, JSON.stringify(appJson, null, 2) + '\n');

console.log(`Stamped mobile version ${version}`);
