#!/bin/ash

# Secure entrypoint
# Initialize & Start MariaDB
mkdir -p /run/mysqld
chown -R mysql:mysql /run/mysqld
mysql_install_db --user=mysql --ldata=/var/lib/mysql
mysqld --user=mysql --console --skip-networking=0 &

# Wait for mysql to start
while ! mysqladmin ping -h'localhost' --silent; do echo 'not up' && sleep .2; done


function genPass() {
    cat /dev/urandom | tr -dc '[:alnum:]' | head -c 64
}

mysql -u root << EOF
DROP DATABASE IF EXISTS apexsurvive;
CREATE DATABASE apexsurvive;

CREATE TABLE apexsurvive.users (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    email varchar(255),
    password varchar(255) NOT NULL,
    unconfirmedEmail varchar(255),
    confirmToken varchar(255),
    fullName varchar(255) DEFAULT '',
    username varchar(255) DEFAULT '',
    isConfirmed varchar(255) DEFAULT 'unverified',
    isInternal varchar(255) DEFAULT 'false',
    isAdmin varchar(255) DEFAULT 'false'
);

CREATE TABLE apexsurvive.products (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255),
    price VARCHAR(255),
    image VARCHAR(255),
    description VARCHAR(255),
    seller VARCHAR(255),
    note TEXT
);

INSERT INTO apexsurvive.users VALUES(
    1,
    'xclow3n@apexsurvive.htb',
    '$(genPass)',
    '',
    '',
    'Rajat Raghav',
    'xclow3n',
    'verified',
    'true',
    'true'
);

INSERT INTO apexsurvive.products VALUES(
    1,
    'MazeMaster Compass',
    '1337',
    '/static/images/compass.png',
    'A precision-engineered compass with a built-in digital display, providing real-time maze mapping and navigation.',
    'Xclow3n',
    '<div><h4 class="text-white">Celestial Pathfinder User Manual</h4><p>Welcome to the Celestial Pathfinder - your guide through the labyrinth of magic and mystery.</p><p><strong>Usage:</strong></p><p>Hold the Aetherial Compass upright and let it align with the constellations for celestial guidance.</p><p><strong>Special Features:</strong></p><ul><li>Precise navigation through mystical pathways.</li><li>Soft glow in the presence of magical routes.</li></ul><p>Adventure awaits as you follow the stars with the Celestial Pathfinder!</p></div>'
);

INSERT INTO apexsurvive.products VALUES(
    2,
    'Mystic Mapweaver\'s',
    '1337',
    '/static/images/codex.webp',
    'Record your labyrinth journey with the Mystic Mapweaver\'s Codex.',
    'Xclow3n',
    '<div><h4 class="text-white">Mystic Mapweaver\'s Codex User Manual</h4><p>Record your labyrinth journey with the Mystic Mapweaver\'s Codex - a magical tome for navigation and observation.</p><p><strong>Usage:</strong></p><p>Flip through the animated illustrations to see the ever-shifting nature of the maze.</p><p><strong>Special Features:</strong></p><ul><li>Real-time updates of the labyrinth\'s twists and turns.</li><li>Weather-resistant for durability in mystical environments.</li></ul><p>Let the Codex be your guide through the enchanting maze!</p></div>'
);

INSERT INTO apexsurvive.products VALUES(
    3,
    'Chimeric Shapeshifter\'s',
    '1337',
    '/static/images/toolkit.png',
    'Adapt to the ever-changing challenges of the labyrinth with the Chimeric Shapeshifter\'s Toolkit.',
    'Xclow3n',
    '<div><h4 class="text-white">Chimeric Shapeshifter\'s Toolkit User Manual</h4><p>Adapt to the ever-changing labyrinth with the Chimeric Shapeshifter\'s Toolkit - your magical companion.</p><p><strong>Usage:</strong></p><p>Watch the toolkit transform into various tools based on your needs and challenges.</p><p><strong>Special Features:</strong></p><ul><li>Golden key for unlocking ethereal gates.</li><li>Spectral rope for climbing mystical obstacles.</li></ul><p>Embrace versatility with the Chimeric Shapeshifter\'s Toolkit as you conquer the maze!</p></div>'
);

INSERT INTO apexsurvive.products VALUES(
    4,
    'Faerylight',
    '1337',
    '/static/images/headlamp.png',
    'Light up the shadows with the Faerylight Eclipsing Lantern, a beacon of enchantment in the heart of the labyrinth.',
    'Xclow3n',
    '<div><h4 class="text-white">Faerylight Illumination User Manual</h4><p>Introducing the Faerylight Eclipsing Lantern - your enchanting companion in the heart of the labyrinth.</p><p><strong>Usage:</strong></p><p>Activate the lantern to illuminate your path and reveal hidden portals and doorways.</p><p><strong>Special Features:</strong></p><ul><li>Soft, otherworldly glow for a magical ambiance.</li><li>Reveals secret passages in the darkness.</li></ul><p>Let the Faerylight guide you through the mystical maze with its captivating radiance!</p></div>'
);

INSERT INTO apexsurvive.products VALUES(
    5,
    'Dragonheart Boost',
    '1337',
    '/static/images/ration.png',
    'Experience the strength of dragons with the Dragonheart Rations Bag.',
    'Xclow3n',
    '<div><h4 class="text-white">Dragonheart Boost User Manual</h4><p>Unleash the power of dragons with the Dragonheart Rations Bag - your source of mystical nourishment.</p><p><strong>Usage:</strong></p><p>Consume dragon-imbued rations to replenish strength and gain temporary magical enhancements.</p><p><strong>Special Features:</strong></p><ul><li>Each bite enhances your stamina and magical abilities.</li><li>Aura of dragon energy for heightened resilience.</li></ul><p>Feel the dragon\'s strength within as you indulge in the Dragonheart Boost!</p></div>'
);

INSERT INTO apexsurvive.products VALUES(
    6,
    'Boosting Elixirs',
    '1337',
    '/static/images/exlir.png',
    'Specially formulated energy-boosting elixirs to enhance your stamina and focus.',
    'Xclow3n',
    '<div><h4 class="text-white">Celestial Harmony Elixirs User Manual</h4><p>Enhance your abilities with the Celestial Harmony Elixirs - potions crafted for the labyrinth journey.</p><p><strong>Usage:</strong></p><p>Sip from the elixirs to boost agility, perception, and magical resistance.</p><p><strong>Special Features:</strong></p><ul><li>Formulated with celestial herbs for optimal benefits.</li><li>Choose from various celestial flavors for a personalized experience.</li></ul><p>Empower yourself with the magic of Celestial Harmony Elixirs!</p></div>'
);

CREATE USER 'user'@'localhost' IDENTIFIED BY 'xClow3n123';

GRANT SELECT, UPDATE, INSERT, DELETE ON apexsurvive.users TO 'user'@'localhost';
GRANT SELECT, UPDATE, INSERT, DELETE ON apexsurvive.products TO 'user'@'localhost';
FLUSH PRIVILEGES;
EOF

/usr/bin/supervisord -c /etc/supervisord.conf