#!/bin/sh

set -e

HOST="tanker-api.yandex-team.ru"
LANGS="ru en tr uk id fr fi"
LANGS_JOINED="ru,en,tr,uk,id,fr,fi"

FORMATS="loc json"

# Костыль! Подумать, как сделать лучше

for LNG in $LANGS
do
    for FORMAT in $FORMATS
    do
        wget -6 --no-check-certificate -O "loc/Plashka.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Plashka&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Frontend.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Frontend&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Enabling2fa.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Enabling2fa&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/ProfileAccess.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=oauth&keyset-id=frontend.access&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Time.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=oauth&keyset-id=frontend.time&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/EULA.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=EULA&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/SimpleReg.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=SimpleReg&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Common.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=common&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Sex.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Sex&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Texts.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Texts&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Date.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Date&language=$LNG&safe&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Login.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Login&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Restore.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Restore&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Errors.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Errors&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Mend.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Mend&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/ProfileSocial.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=ProfileSocial&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Semiauto.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Semiauto&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Restoration.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Restoration&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Social.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Social&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Multiauth.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=multiauth&keyset-id=dropdown&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Phones.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Phones&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/ServicesSubscriptions.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=ServicesSubscriptions&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/ProfileJournal.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=ProfileJournal&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Navigation.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Navigation&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/Migrating2fa.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=Migrating2fa&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/AuthChallenges.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=AuthChallenges&language=$LNG&safe&status=unapproved&flat-keyset=1"
        wget -6 --no-check-certificate -O "loc/ProfileSecurity.$LNG.$FORMAT" "https://$HOST/keysets/$FORMAT/?project-id=passport&keyset-id=ProfileSecurity&language=$LNG&safe&status=unapproved&flat-keyset=1"
    done

    #wget -6 --no-check-certificate -O "loc/AdmRegSetPass.$LNG.json" "https://$HOST/keysets/json/?project-id=passport&keyset-id=AdmRegSetPass&language=$LNG&safe"
    #wget -6 --no-check-certificate -O "loc/MDFillInfo.$LNG.json" "https://$HOST/keysets/json/?project-id=passport&keyset-id=MDFillInfo&language=$LNG&safe"
    #wget -6 --no-check-certificate -O "loc/Passwd.$LNG.json" "https://$HOST/keysets/json/?project-id=passport&keyset-id=Passwd&language=$LNG&safe"

    wget -6 --no-check-certificate --cache=off -O "lib/tanker/tanker.dynamic.$LNG.js" "https://$HOST/tools/generate/i18n.xml?project-id=tanker&language=$LNG&keyset-id=dynamic&path=i18n&type=js&safe"

    wget -6 --no-check-certificate -O "loc/months.$LNG.json" "https://$HOST/keysets/json/?project-id=tanker&keyset-id=months&language=$LNG&safe"
    wget -6 --no-check-certificate -O "loc/Months.$LNG.loc" "https://$HOST/keysets/loc/?project-id=passport&keyset-id=Months&language=$LNG&flat-keyset=1&safe&include=no"
    sed -i '/^name /d' loc/Months.$LNG.loc
done

wget -6 --no-check-certificate -O "loc/sids.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=SidsDescriptions&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"
wget -6 --no-check-certificate -O "loc/auth.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=Errors,AdmRegSetPass,MDFillInfo,Passwd,Frontend,Errors,Mend,Message,Restore,common,FlashMessages&language=$LANGS_JOINED&flat-keyset=1&safe&all-forms=1&status=unapproved"
wget -6 --no-check-certificate -O "loc/months.json" "https://$HOST/projects/export/json/?project-id=tanker&keyset-id=months&language=$LANGS_JOINED&flat-keyset=1&safe"
wget -6 --no-check-certificate -O "lib/passport-errors/messages.json" "https://$HOST/projects/export/json/?project-id=passport&language=$LANGS_JOINED&keyset-id=Errors&flat-keyset=1&safe"
wget -6 --no-check-certificate -O "loc/multiauth.json" "https://$HOST/projects/export/json/?project-id=multiauth&keyset-id=dropdown&language=$LANGS_JOINED&flat-keyset=1&safe"
wget -6 --no-check-certificate -O "loc/frontend.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=Frontend&language=$LANGS_JOINED&flat-keyset=1&safe"
wget -6 --no-check-certificate -O "loc/profilesocial.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=ProfileSocial&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"
wget -6 --no-check-certificate -O "loc/common.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=common&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"
wget -6 --no-check-certificate -O "loc/subscriptions.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=ServicesSubscriptions&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"
wget -6 --no-check-certificate -O "loc/profilejournal.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=ProfileJournal&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"
wget -6 --no-check-certificate -O "loc/profilesecurity.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=ProfileSecurity&language=$LANGS_JOINED&flat-keyset=1&status=unapproved&safe"

# Для соц. авторизации
wget -6 --no-check-certificate -O "loc/social.json" "https://$HOST/projects/export/json/?project-id=social&flat-keyset=1&status=unapproved&safe=1"

# For jsx templates
wget -6 --no-check-certificate -O "loc/_locs.json" "https://$HOST/projects/export/json/?project-id=passport&keyset-id=OneDomik,OneDomikCustoms,Family,Frontend,Errors,EmailConfirm,Mend,Months,Profile,Profile2,ProfileDelete,ProfileSocial,common,ProfileJournal,Header,Login,Enabling2fa,Restore,Navigation,Phones,Verification,EULA,Dashboard,Favorites,UserApprove&language=$LANGS_JOINED&flat-keyset=1&safe&all-forms=1&status=unapproved"

node ./node_modules/.bin/t8y --dir loc --files lib/passport-errors/messages.json
