Skip to content

Commit

Permalink
Improve run.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
ysdragon committed Nov 13, 2024
1 parent 1cdc17d commit 1747874
Showing 1 changed file with 146 additions and 38 deletions.
184 changes: 146 additions & 38 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,166 @@
#!/bin/sh

# Color definitions
PURPLE='\033[0;35m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

printf "\033c"
printf "${GREEN}Starting..${NC}\n"
sleep 1
printf "\033c"

DIR=$PWD # get current dir
# if current dir is /root print ~
if [ "$PWD" = "/root"* ]; then
DIR="~${PWD#/root}"
fi
# Configuration
HOSTNAME="MyVPS"
HISTORY_FILE="${HOME}/.custom_shell_history"
MAX_HISTORY=1000

printf "\033c"
printf "${GREEN}╭────────────────────────────────────────────────────────────────────────────────╮${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}│ Pterodactyl VPS EGG │${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}${RED}© 2021 - 2024 ${PURPLE}ysdragon${GREEN}${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}╰────────────────────────────────────────────────────────────────────────────────╯${NC}\n"
printf " \n"
printf "root@MyVPS:${DIR}# \n"

run_cmd() {
read -p "root@MyVPS:$DIR# " CMD
eval "$CMD"

# Update DIR after executing command
DIR=$PWD
if [ "$PWD" = "/root"* ]; then
DIR="~${PWD#/root}"
# Function to handle cleanup on exit
cleanup() {
printf "\n${GREEN}Session ended. Goodbye!${NC}\n"
exit 0
}

# Function to get formatted directory
get_formatted_dir() {
current_dir="$PWD"
case "$current_dir" in
/root*)
printf "~${current_dir#/root}"
;;
*)
printf "$current_dir"
;;
esac
}

# Function to print the banner
print_banner() {
printf "\033c"
printf "${GREEN}╭────────────────────────────────────────────────────────────────────────────────╮${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}│ Pterodactyl VPS EGG │${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}${RED}© 2021 - 2024 ${PURPLE}ysdragon${GREEN}${NC}\n"
printf "${GREEN}│ │${NC}\n"
printf "${GREEN}╰────────────────────────────────────────────────────────────────────────────────╯${NC}\n"
printf " \n"
}

# Function to print prompt
print_prompt() {
user="$1"
printf "\n${GREEN}${user}@${HOSTNAME}${NC}:${RED}$(get_formatted_dir)${NC}# "
}

# Function to save command to history
save_to_history() {
cmd="$1"
if [ -n "$cmd" ] && [ "$cmd" != "exit" ]; then
printf "$cmd\n" >> "$HISTORY_FILE"
# Keep only last MAX_HISTORY lines
if [ -f "$HISTORY_FILE" ]; then
tail -n "$MAX_HISTORY" "$HISTORY_FILE" > "$HISTORY_FILE.tmp"
mv "$HISTORY_FILE.tmp" "$HISTORY_FILE"
fi
fi

printf "root@MyVPS:$DIR# \n"
run_user_cmd
}

run_user_cmd() {
read -p "user@MyVPS:$DIR# " CMD2
eval "$CMD2"

# Update DIR after executing command
DIR=$PWD
if [ "$PWD" = "/root"* ]; then
DIR="~${PWD#/root}"
# Function reinstall the OS
reinstall() {
# Source the /etc/os-release file to get OS information
. /etc/os-release

if [ "$ID" = "alpine" ] || [ "$ID" = "chimera" ]; then
rm -rf / > /dev/null 2>&1
else
rm -rf --no-preserve-root / > /dev/null 2>&1
fi
}

# Function to print a beautiful help message
print_help_message() {
printf "${PURPLE}╭────────────────────────────────────────────────────────────────────────────────╮${NC}\n"
printf "${PURPLE}│ │${NC}\n"
printf "${PURPLE}│ Available Commands │${NC}\n"
printf "${PURPLE}│ │${NC}\n"
printf "${PURPLE}${YELLOW}clear, cls${GREEN} - Clear the screen. ${PURPLE}${NC}\n"
printf "${PURPLE}${YELLOW}exit${GREEN} - Shutdown the server. ${PURPLE}${NC}\n"
printf "${PURPLE}${YELLOW}history${GREEN} - Show command history. ${PURPLE}${NC}\n"
printf "${PURPLE}${YELLOW}reinstall${GREEN} - Reinstall the server. ${PURPLE}${NC}\n"
printf "${PURPLE}${YELLOW}help${GREEN} - Display this help message. ${PURPLE}${NC}\n"
printf "${PURPLE}│ │${NC}\n"
printf "${PURPLE}╰────────────────────────────────────────────────────────────────────────────────╯${NC}\n"
}

# Function to handle command execution
execute_command() {
cmd="$1"
user="$2"

printf "root@MyVPS:$DIR# \n"
run_cmd
# Save command to history
save_to_history "$cmd"

# Handle special commands
case "$cmd" in
"clear"|"cls")
print_banner
print_prompt "$user"
return 0
;;
"exit")
cleanup
;;
"history")
if [ -f "$HISTORY_FILE" ]; then
cat "$HISTORY_FILE"
fi
print_prompt "$user"
return 0
;;
"reinstall")
printf "\n${GREEN}Reinstalling....${NC}\n"
reinstall
exit 2
;;
"help")
print_help_message
print_prompt "$user"
return 0
;;
*)
eval "$cmd"
print_prompt "$user"
return 0
;;
esac
}

run_cmd
# Function to run command prompt for a specific user
run_prompt() {
user="$1"
read -r cmd

execute_command "$cmd" "$user"
print_prompt "$user"
}


# Create history file if it doesn't exist
touch "$HISTORY_FILE"

# Set up trap for clean exit
trap cleanup INT TERM

# Print initial banner
print_banner

# Print initial command
printf "${GREEN}root@${HOSTNAME}${NC}:${RED}$(get_formatted_dir)${NC}#\n"

# Main command loop
while true; do
run_prompt "user"
done

0 comments on commit 1747874

Please sign in to comment.