summaryrefslogtreecommitdiff
path: root/ar/.local/bin/createsh
blob: 53c2a322c352cbd1b500fb77cca91c8f4d0ddf73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh

# Function to display help
usage() {
  echo "Generate a shell script in sh, bash, or zsh"
  echo ""
  echo "Usage: ${0##*/} [OPTION] [FILENAME]"
  echo ""
  echo "Options:"
  echo "  -h, --help    : Show this help message."
  echo "  -b, --bash    : Create a Bash script (bash)."
  echo "  -d, --dash    : Create a POSIX-compliant shell script (sh)."
  echo "  -z, --zsh     : Create a Zsh script (zsh)."
  echo ""
  echo "Example:"
  echo "  ${0##*/} -d sambacreate   # Create a POSIX-compliant shell script named 'sambacreate'"
}

# Default shell to POSIX if no option is provided
shell="sh"
script_name=""

# Parse options
while [ "$1" ]; do
  case "$1" in
  -d | --dash)
    shell="sh"
    ;;
  -b | --bash)
    shell="bash"
    ;;
  -z | --zsh)
    shell="zsh"
    ;;
  -h | --help)
    usage
    exit 0
    ;;
  -*)
    echo "Error: Invalid option '$1'. Use -h or --help for usage information."
    exit 1
    ;;
  *)
    if [ -z "$script_name" ]; then
      script_name="$1"
    else
      echo "Error: Multiple filenames provided. Use -h or --help for usage information."
      exit 1
    fi
    ;;
  esac
  shift
done

# Validate script name
if [ -z "$script_name" ]; then
  echo "Error: No filename provided. Use -h or --help for usage information."
  exit 1
fi

# Check if the script already exists
script_path="$HOME/.local/bin"
if [ -f "$script_path/$script_name" ]; then
  echo "Error: File '$script_path/$script_name' already exists."
  exit 1
fi

# Create the new script file
mkdir -p "$script_path"

echo "#!/bin/$shell

# Help function
usage() {
  echo \"Usage: \${0##*/} [OPTION] ????\"
  echo \"????\"
  echo \"\"
  echo \"Options:\"
  echo \"  -h, --help       : Show this help message\"
  echo \"\"
  echo \"Example:\"
  echo \"  \${0##*/} -? ????     # ????\"
}

# Main function
$script_name() {
  case \"\$1\" in
  -h | --help)
    usage
    exit 0
    ;;
  *)
    echo \"\${0##*/}\"
    ;;
  esac
}

# Call the main function with arguments
$script_name \"\$@\"" >"$script_path/$script_name"

# Make the script executable
chmod +x "$script_path/$script_name"

echo "'$script_name' created successfully at $script_path."