#!/bin/bash

### drfc
sudo apt-get update
sudo apt-get install -y jq awscli python3-boto3 python3-pip docker-compose tmux ffmpeg

# uv install
curl -LsSf https://astral.sh/uv/install.sh | sh
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
cd ~/physicar-deepracer-for-cloud
uv sync

# deepracer-for-cloud
mkdir -p ~/.physicar-deepracer-for-cloud
cd ~/.physicar-deepracer-for-cloud
git clone --branch v5.3.3 --depth 1 --single-branch \
  https://github.com/aws-deepracer-community/deepracer-for-cloud.git
cd deepracer-for-cloud
./bin/prepare.sh
sudo usermod -aG docker $USER
newgrp docker
bin/init.sh -c local -a cpu -s compose


### data copy & rm .devcontainer
cp -r ~/physicar-deepracer-for-cloud/.devcontainer/.physicar-deepracer-for-cloud/. ~/.physicar-deepracer-for-cloud/
# rm -rf ~/physicar-deepracer-for-cloud/.devcontainer


### env setup
sed -i 's/^DR_UPLOAD_S3_PROFILE=.*$/DR_UPLOAD_S3_PROFILE=minio/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_UPLOAD_S3_BUCKET=.*$/DR_UPLOAD_S3_BUCKET=bucket/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_LOCAL_S3_BUCKET=.*$/DR_LOCAL_S3_BUCKET=bucket/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_LOCAL_S3_PROFILE=.*$/DR_LOCAL_S3_PROFILE=minio/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_HOST_X=.*$/DR_HOST_X=True/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_GUI_ENABLE=.*$/DR_GUI_ENABLE=False/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i '/^#[[:space:]]*DR_DISPLAY/ s/^#[[:space:]]*//' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_DISPLAY=.*$/DR_DISPLAY=:99/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/system.env
sed -i 's/^DR_UPLOAD_S3_PREFIX=.*$/DR_UPLOAD_S3_PREFIX=$DR_LOCAL_S3_MODEL_PREFIX/' ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/run.env


######## bucket (minio)
cat <<EOF > ~/.aws/credentials
[minio]
aws_access_key_id = deepracer
aws_secret_access_key = deepracer
EOF
source ~/.physicar-deepracer-for-cloud/deepracer-for-cloud/bin/activate.sh --minio
cd ~/physicar-deepracer-for-cloud
uv run ~/.physicar-deepracer-for-cloud/setup_minio_anonymous.py


### mount
sudo apt-get update
sudo apt-get install -y s3fs
mkdir -p ~/.s3fs
echo "deepracer:deepracer" > ~/.s3fs/credentials
chmod 600 ~/.s3fs/credentials


### vnc
sudo apt-get update
sudo apt-get install -y xvfb x11vnc x11-xserver-utils fluxbox twm # for Xvfb
sudo apt-get install -y novnc websockify  # for noVNC
touch ~/.Xauthority


######## bashrc
cat << 'EOF' >> ~/.bashrc

export DISPLAY=:99

EOF


######## systemd 서비스 등록 - entrypoint
USERNAME=${SUDO_USER:-$(id -un)}
HOMEDIR=$(getent passwd "$USERNAME" | cut -d: -f6)

sudo chmod +x "$HOMEDIR/.physicar-deepracer-for-cloud/entrypoint.ec2.sh"

sudo tee /etc/systemd/system/physicar-deepracer-for-cloud-entrypoint@.service >/dev/null <<'UNIT'
[Unit]
Description=Run physicar deepracer entrypoint in tmux for %i
After=network.target docker.service

[Service]
Type=simple
User=%i
WorkingDirectory=/home/%i
Environment=HOME=/home/%i
ExecStart=/usr/bin/tmux new-session -d -s physicar-deepracer-for-cloud-entrypoint "/home/%i/.physicar-deepracer-for-cloud/entrypoint.ec2.sh"
RemainAfterExit=yes
Restart=on-failure

[Install]
WantedBy=multi-user.target
UNIT

sudo systemctl daemon-reload
sudo systemctl enable --now "physicar-deepracer-for-cloud-entrypoint@${USERNAME}.service"




