Max Mahn

aboutsummaryrefslogtreecommitdiff
path: root/src/main.sh
blob: 1b38eab30078e4af50d1450ceabee4d0d0e5bb04 (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
105
106
107
108
109
110
111
#!/bin/sh

##########################################
##              { mktree }              ##
##   Copyright(C) 2026  Maxwell Mahn    ##
## see LICENSE file for license details ##
## - -- - -- - -- - -- - -- - -- - -- - ##
## just a shell script for automating   ##
## the creation of repetative directory ##
## structures. (like everytime you want ##
## to start a new project; now you can  ##
## eliminate the setup time; surely     ##
## you'll finish the project this time) ##
##########################################


# default program configuration
SETUP_SCRIPT=".setup.sh"

cfg_dir="${MKTREE_CONFIGDIR:-${HOME}/.config/mktree}"

USER_TEMPLATES="$cfg_dir/skel"
SYSTEM_TEMPLATES="/etc/mktree/skel"

##include src/util.sh


GetTemplate() {
  [ -z "$1" ] && return 1
  
  for prefix in "${CUSTOM_TEMPLATES}" "${USER_TEMPLATES}" "${SYSTEM_TEMPLATES}"
  do
    [ -z "${prefix}" ] && continue
    temp_path="${prefix}/$1"
    Debug 2 "Checking template path '${temp_path}'..."
    [ -d "${temp_path}" ] && echo "${temp_path}" && return 0
  done
  return 1
}


MakeProject() { # format_path, project_name
  [ -z "$1" ] && Error "Project format not defined!" && return 1
  [ -z "$2" ] && Error "Project name not defined!" && return 1
  
  [ -d "$2" ] && Error "Can't make project in $2. Directory already exists." && return 1
  
  # copy skeleton
  Debug 1 "Copying template skeleton..."
  cp -r "$1" "./$2" && cd "./$2"
  
  # run hooks
  Debug 1 "Running setup hooks..."
  shift 2
  setup="./${SETUP_SCRIPT}"
  [ -f "${setup}" ] && sh "${setup}" $@
  
  if [ $? -eq 0 ]
  then
    Debug 2 "Removing setup script..."
    rm -f "${setup}"
  else
    Error "Failed to run setup hooks!"
  fi
  
  Debug 1 "Done."
  return 0
}


ListTemplates() {
  [ -d ${USER_TEMPLATES} ] && echo "User Templates:" && \
    for template in ${USER_TEMPLATES}/*
  do
    echo "  $(basename ${template})"
  done
  [ -d ${SYSTEM_TEMPLATES} ] && echo "System Templates:" && \
    for template in ${SYSTEM_TEMPLATES}/*
  do
    echo "  $(basename ${template})"
  done
  exit 0
}


SetSystemDir() {
  [ -d "$1" ] && SYSTEM_TEMPLATES="$1" return 0 || return 1;
}

SetUserDir() {
  [ -d "$1" ] && USER_TEMPLATES="$1" return 0 || return 1;
}

SetFormatPath() {
  [ -d "$1" ] && FMT_PATH="$1" return 0 || return 1;
}

SetVerbose() {
  VERBOSITY+=1
}


##insert awk -v OUTPUT=shell -f args.awk res/args

##include src/args.sh


# parsing options
options=$(${GETOPT_CMD}) && ParseOpts ${options} \
  || Error "Run '$(basename $0) -h' for usage information." && exit 1