#!/bin/bash

argcount=$#
if [[ $argcount -eq 0 ]]; then
    echo "Usage: $0 [command] [args(optional)]"
    echo "Available commands:"
    echo "copy-save     Copy a player save to the appropriate location on the server."
    echo "push          Builds and copies all files to the server for an update."
fi

copy_configs () {
    cd ~/IdeaProjects/rs09-remake/Server/data/configs
    rsync -v * scape2009@play.2009scape.org:configs/
    cd ..
    scp ObjectParser.xml scape2009@play.2009scape.org:IdeaProjects/2009scape/Server/data/
    echo "Configs successfully synced with remote server"
}

build_jars () {
    cp ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt.backup
    
    cp ~/IdeaProjects/rs09-remake/Client/src/main/kotlin/org/rs09/client/config/GameConfig.kt ~/IdeaProjects/rs09-remake/Client/src/main/kotlin/org/rs09/client/config/GameConfig.backup
    
    rsa_priv=`cat ~/IdeaProjects/rs09-remake/rsapriv | grep "private static final BigInteger RSA_EXPONENT = new BigInteger(" | sed 's/private static final BigInteger RSA_EXPONENT = new BigInteger(\"//' | sed 's/\")\;//'`
    rsa_pub=`cat ~/IdeaProjects/rs09-remake/rsapriv | grep "private static final BigInteger RSA_MODULUS = new BigInteger(" | sed 's/private static final BigInteger RSA_MODULUS = new BigInteger(\"//' | sed 's/\")\;//'`
    
    sed -i "s/var EXPONENT = BigInteger(\"[0-9]*\")/var EXPONENT = BigInteger(\"$rsa_priv\")/" ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt
    sed -i "s/var MODULUS = BigInteger(\"[0-9]*\")/var MODULUS = BigInteger(\"$rsa_pub\")/" ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt
    
    sed -i "s/var MODULUS = BigInteger(\"[0-9]*\")/var MODULUS = BigInteger(\"$rsa_pub\")/" ~/IdeaProjects/rs09-remake/Client/src/main/kotlin/org/rs09/client/config/GameConfig.kt
    
    cd ~/IdeaProjects/rs09-remake/Client
    gradle jar
    cd ../Server
    gradle jar
    
    cp ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt.backup ~/IdeaProjects/rs09-remake/Server/src/main/kotlin/rs09/ServerConstants.kt
    
    cp ~/IdeaProjects/rs09-remake/Client/src/main/kotlin/org/rs09/client/config/GameConfig.backup ~/IdeaProjects/rs09-remake/Client/src/main/kotlin/org/rs09/client/config/GameConfig.kt
    
    echo "Jars successfully built with RSA keys from ~/IdeaProjects/rs09-remake"
}

copy_jars() {
    cd ~/IdeaProjects/rs09-remake/Client/build/libs
    scp client-1.0.0.jar scape2009@play.2009scape.org:/opt/lampp/htdocs/2009scape.jar
    cd ~/IdeaProjects/rs09-remake/Server/build/libs
    scp server-1.0.0.jar scape2009@play.2009scape.org:server.jar
    echo "Jars successfully copied to remote server"
}

copy_player_save() {
    if [[ $argcount -lt 2 ]]; then
        echo "No file supplied! Please supply a json file."
        echo "$argcount"
        exit
    fi
    
    if [[ "$arg2" == *".json" ]]; then
        scp $arg2 scape2009@play.2009scape.org:IdeaProjects/2009scape/Server/data/players/$arg2
        exit
    fi
    
    echo "Save doesn't end in .json!"
}

command="$1"
arg2="$2"

if [[ "$command" == "copy_save" ]]; then
    copy_player_save
fi

if [[ "$command" == "push" ]]; then
    build_jars
    copy_configs
    copy_jars
fi
