get_doom_random_map.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. # fail if any commands fails
  3. set -e
  4. # debug log
  5. #set -x
  6. show_usage () {
  7. printf "Usage: $0 [options [parameters]]\n"
  8. printf "\n"
  9. printf "Mandatory options:\n"
  10. printf " -i|--iwad [doom|doom2|tnt|plutonia|heretic|hexen]\n"
  11. printf " -l|--map-limit [none|vanilla|nolimit|boom|zdoom]\n"
  12. printf "\n"
  13. printf "Options:\n"
  14. printf " -d|--game-dir [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
  15. printf " -h|--help, Print help\n"
  16. exit
  17. }
  18. if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  19. show_usage
  20. fi
  21. if [ -z "$1" ]; then
  22. show_usage
  23. fi
  24. while [ -n "$1" ]; do
  25. case "$1" in
  26. --game-dir|-d)
  27. shift
  28. param_game_dir=$1
  29. ;;
  30. --iwad|-i)
  31. shift
  32. param_iwad=$1
  33. ;;
  34. --map-limit|-l)
  35. shift
  36. param_map_limit=$1
  37. ;;
  38. *)
  39. show_usage
  40. ;;
  41. esac
  42. shift
  43. done
  44. ### Configuration
  45. if [ -z "$param_game_dir" ]; then
  46. param_game_dir=$HOME/games/doom
  47. fi
  48. config_script_dir="$(pwd $(dirname $0))"
  49. config_iwads_dir=$param_game_dir/wads/iwads
  50. ### check parameter values
  51. # iwad
  52. doom_game="doom doom2"
  53. echo "$doom_game" | tr ' ' '\n' | while read -r item; do
  54. if [ "$item" = "$param_iwad" ]; then touch match; fi
  55. done
  56. if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $doom_game"; exit 1; fi; rm match
  57. map_limit="none vanilla nolimit boom zdoom"
  58. echo "$map_limit" | tr ' ' '\n' | while read -r item; do
  59. if [ "$item" = "$param_map_limit" ]; then touch match; fi
  60. done
  61. if [ ! -f match ]; then echo "ERROR: $param_map_limit is not a valid option, valid options are: $map_limit"; exit 1; fi; rm match
  62. ### Script
  63. config_exclude_files="! -name *tex*.* ! -name *res*.* ! -name *fix.* ! -name *demo*.* ! -name *credits*.*"
  64. config_pwads_dir="$param_game_dir/wads/$param_iwad"
  65. if [ "$param_map_limit" = "none" ]; then
  66. config_pwad_file=$(find "$config_pwads_dir"/vanilla "$config_pwads_dir"/nolimit "$config_pwads_dir"/boom "$config_pwads_dir"/zdoom/ -name "*.wad" -or -name "*.pk3" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
  67. elif [ "$param_map_limit" = "vanilla" ]; then
  68. config_pwad_file=$(find "$config_pwads_dir"/vanilla -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
  69. elif [ "$param_map_limit" = "nolimit" ]; then
  70. config_pwad_file=$(find "$config_pwads_dir"/nolimit -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
  71. elif [ "$param_map_limit" = "boom" ]; then
  72. config_pwad_file=$(find "$config_pwads_dir"/boom -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
  73. elif [ "$param_map_limit" = "zdoom" ]; then
  74. config_pwad_file=$(find "$config_pwads_dir"/zdoom/ -name "*.wad" -or -name "*.pk3" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
  75. fi
  76. # Check pwad found
  77. if [ -z "$config_pwad_file" ]; then
  78. echo "ERROR: No pwad file found"
  79. echo "ERROR: param_iwad : $param_iwad"
  80. echo "ERROR: pwad file : $config_pwad_file"
  81. exit 1
  82. else
  83. echo "$config_pwad_file"
  84. fi