#!/bin/bash
#
# SnollyGoster
# Have you ever think if you were special?
# https://snollygoster.xyz
# @snollyygoster // snollygoster@duck.com // SnollyGoster#2340
#
# Script auto generate wallet solana, dan menyimpan semua info didalam wallet.txt
# Pastikan jika ingin running script ini untuk selanjut nya, pindahkan kedalam dir lain. Jangan sampai tertimpa di dir baru. Mager buat fungsi baru akwowakowako
#

# Membuat direktori jika belum ada
mkdir -p keyring

# Membuka file wallet.txt untuk menyimpan informasi wallet
output_file="wallet.txt"
> $output_file

# Meminta pengguna untuk memasukkan jumlah wallet yang ingin dibuat
read -p "Masukkan jumlah wallet yang ingin dibuat: " num_wallets

# Dir saat ini
current=$(pwd)

# Array untuk menyimpan nama file wallet
wallet_files=()

# Loop untuk membuat wallet sesuai jumlah yang dimasukkan pengguna
for ((i=1; i<=$num_wallets; i++)); do
    filename="id${i}.json"
    # Generate wallet
    output=$(solana-keygen new -o ${current}/keyring/${filename})
    phrase=$(echo "$output" | awk '/Save this seed phrase and your BIP39 passphrase to recover your new keypair/{getline; print}')

    # Mendapatkan pubkey
    pubkey=$(solana-keygen pubkey ${current}/keyring/${filename})

    # Mendapatkan private key dari file
    privkey=$(cat ${current}/keyring/${filename})

    # Menyimpan nama file wallet ke dalam array
    wallet_files+=("${current}/keyring/${filename}")

    # Menyimpan informasi wallet ke dalam file wallet.txt
    echo "${filename}" >> $output_file
    echo "Pubkey: ${pubkey}" >> $output_file
    echo "Phrase: ${phrase}" >> $output_file
    echo "Privkey: ${privkey}" >> $output_file
    echo >> $output_file
done

# Membuat program otomatis ore untuk setiap wallet yang dibuat
for wallet_file in "${wallet_files[@]}"; do
    filename=$(basename -- "$wallet_file")
    extension="${filename##*.}"
    filename="${filename%.*}"
    script_file="${filename}.sh"
    echo '#!/bin/bash' > $script_file
    echo "echo 'Starting mining process for ${filename}.json...'" >> $script_file
    echo "current=\$(pwd)" >> $script_file
    echo "while true; do" >> $script_file
    echo "    ore \\" >> $script_file
    echo "    --rpc GANTIRPC \\" >> $script_file
    echo "    --keypair \${current}/keyring/${filename}.json \\" >> $script_file
    echo "    --priority-fee 1 \\" >> $script_file
    echo "    mine \\" >> $script_file
    echo "    --threads 6" >> $script_file
    echo '    if [[ $? -eq 0 ]]; then' >> $script_file
    echo "        echo 'Mining process failed for ${filename}.json.'" >> $script_file
    echo '        sleep 5' >> $script_file
    echo '    else' >> $script_file
    echo "        echo 'Restarting in 5 seconds...'" >> $script_file
    echo '      if [[ $output == *"Transaction failed: Error"* ]]; then' >> $script_file
    echo '       break' >> $script_file
    echo '      fi' >> $script_file
    echo '        sleep 5' >> $script_file
    echo '    fi' >> $script_file
    echo 'done' >> $script_file
    chmod +x $script_file
done

echo "Script Completed dude"
