makedeb.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #!/bin/bash
  2. ROOT="$(pwd)"
  3. arch="all"
  4. url=
  5. license=
  6. depends=()
  7. optdepends=()
  8. makedepends=()
  9. provides=()
  10. conflicts=()
  11. source=()
  12. sha256sums=()
  13. function prepare() {
  14. :
  15. }
  16. function build() {
  17. :
  18. }
  19. function package() {
  20. :
  21. }
  22. srcdir=$(realpath -m ./src)
  23. pkgdir=$(realpath -m ./pkg)
  24. # Simple check whether the DEBBUILD looks valid or not
  25. debbuild_verify() {
  26. # Check required values
  27. if [[ -z ${pkgname+x} ]]; then
  28. echo "pkgname not set";
  29. exit 1;
  30. elif [[ -z ${pkgrel+x} ]]; then
  31. echo "pkgrel not set";
  32. exit 1;
  33. elif [[ -z ${pkgdesc+x} ]]; then
  34. echo "pkgdesc not set";
  35. exit 1;
  36. elif [[ -z ${pkgver+x} ]]; then
  37. echo "pkgver not set";
  38. exit 1;
  39. fi
  40. }
  41. # Check dependencies
  42. function check_deps() {
  43. MISSING=()
  44. for pkg in "${makedepends[@]}"; do
  45. dpkg -s "${pkg}" > /dev/null 2>&1
  46. if [[ $? -ne 0 ]]; then
  47. MISSING+=("${pkg}")
  48. fi
  49. done
  50. for pkg in "${depends[@]}"; do
  51. dpkg -s "${pkg}" > /dev/null 2>&1
  52. if [[ $? -ne 0 ]]; then
  53. MISSING+=("${pkg}")
  54. fi
  55. done
  56. if [[ ${#MISSING[@]} -ne 0 ]]; then
  57. echo "The following packages are missing, please install them first:"
  58. echo ""
  59. echo " ${MISSING[@]}"
  60. echo ""
  61. exit 1
  62. fi
  63. }
  64. function exit_on_error() {
  65. if [[ $? -ne 0 ]]; then
  66. echo "exit"
  67. exit 1
  68. fi
  69. }
  70. function clean() {
  71. rm -rf ${srcdir}
  72. rm -rf ${pkgdir}
  73. }
  74. function verify_checksum() {
  75. NAME="${1}"
  76. INDEX=${2}
  77. if [[ ${OPT_CHECKSUMS} -ne 1 ]]; then
  78. return 0
  79. fi
  80. SHASUM="${sha512sums[${INDEX}]}"
  81. if [[ "${SHASUM}" == "SKIP" ]]; then
  82. return 0
  83. fi
  84. CHECKSUM=($(sha512sum ${NAME}))
  85. if [[ "${CHECKSUM}" != "${SHASUM}" ]]; then
  86. echo "Checksum mismatch for ${NAME}"
  87. exit 1
  88. fi
  89. return 0
  90. }
  91. function print_help() {
  92. echo "makedeb.sh [options]"
  93. echo ""
  94. echo "Options:"
  95. echo " -c, --clean"
  96. echo " -C, --cleanbuild"
  97. echo " -d, --nodeps"
  98. echo " -e, --noextract"
  99. echo " -o, --nobuild"
  100. echo " --noprepare"
  101. echo " --skipchecksums"
  102. echo ""
  103. }
  104. CUR_SOURCE=
  105. CUR_SOURCE_URI=
  106. CUR_SOURCE_NAME=
  107. CUR_SOURCE_PROTO=
  108. CUR_SOURCE_SCHEME=
  109. CUR_SOURCE_OPTIONS=
  110. function parse_current_source() {
  111. CUR_SOURCE_URI="${CUR_SOURCE##*+}"
  112. CUR_SOURCE_URI="${CUR_SOURCE_URI##*::}"
  113. CUR_SOURCE_NAME=""
  114. CUR_SOURCE_PROTO=""
  115. CUR_SOURCE_SCHEME="${CUR_SOURCE_URI%%://*}"
  116. CUR_SOURCE_OPTIONS=""
  117. if [[ "${CUR_SOURCE}" == *"::"* ]]; then
  118. CUR_SOURCE_NAME="${CUR_SOURCE%%+*}"
  119. CUR_SOURCE_NAME="${CUR_SOURCE_NAME%%::*}"
  120. fi
  121. if [[ "${CUR_SOURCE}" == *"+"* ]]; then
  122. CUR_SOURCE_PROTO="${CUR_SOURCE%%+*}"
  123. CUR_SOURCE_PROTO="${CUR_SOURCE_PROTO##*::}"
  124. fi
  125. if [[ "${CUR_SOURCE_SCHEME}" == "${CUR_SOURCE_URI}" ]]; then
  126. CUR_SOURCE_SCHEME=""
  127. fi
  128. # Local file
  129. if [[ "${CUR_SOURCE_SCHEME}" == "" && "${CUR_SOURCE_PROTO}" == "" ]]; then
  130. CUR_SOURCE_NAME="${CUR_SOURCE_URI}"
  131. fi
  132. # No name supplied
  133. if [[ "${CUR_SOURCE_NAME}" == "" ]]; then
  134. CUR_SOURCE_NAME="$(basename ${CUR_SOURCE_URI})"
  135. fi
  136. # Extra options (#branch=testing)
  137. if [[ "${CUR_SOURCE_NAME}" == *"#"* ]]; then
  138. CUR_SOURCE_OPTIONS="${CUR_SOURCE_NAME##*#}"
  139. CUR_SOURCE_NAME="${CUR_SOURCE_NAME%%#*}" # remove options
  140. fi
  141. }
  142. # Options
  143. OPT_PREPARE=1
  144. OPT_EXTRACT=1
  145. OPT_BUILD=1
  146. OPT_CLEAN=0
  147. OPT_CLEANBUILD=0
  148. OPT_DEPS=1
  149. OPT_CHECKSUMS=1
  150. # Parse flags
  151. while [[ $# -gt 0 ]]; do
  152. case $1 in
  153. -c|--clean)
  154. OPT_CLEAN=1
  155. shift
  156. ;;
  157. -C|--cleanbuild)
  158. OPT_CLEANBUILD=1
  159. shift
  160. ;;
  161. -d|--nodeps)
  162. OPT_DEPS=0
  163. shift
  164. ;;
  165. -e|--noextract)
  166. OPT_EXTRACT=0
  167. shift
  168. ;;
  169. -o|--nobuild)
  170. OPT_BUILD=0
  171. shift
  172. ;;
  173. --noprepare)
  174. OPT_PREPARE=0
  175. shift
  176. ;;
  177. --skipchecksums)
  178. OPT_CHECKSUMS=0
  179. shift
  180. ;;
  181. --help)
  182. print_help
  183. exit 0
  184. ;;
  185. esac
  186. done
  187. source DEBBUILD
  188. debbuild_verify
  189. if [[ ${OPT_DEPS} -eq 1 ]]; then
  190. check_deps
  191. fi
  192. # Clean build
  193. if [[ ${OPT_CLEANBUILD} -eq 1 ]]; then
  194. rm -rf "${srcdir}"
  195. rm -rf "${pkgdir}"
  196. fi
  197. # Download and verify sources
  198. for ((i=0; i<${#source[@]}; i++)); do
  199. echo ""
  200. CUR_SOURCE="${source[${i}]}"
  201. parse_current_source
  202. # File/directory already exists
  203. if [[ -d ${CUR_SOURCE_NAME} || -f ${CUR_SOURCE_NAME} ]]; then
  204. verify_checksum "${CUR_SOURCE_NAME}" ${i}
  205. continue
  206. fi
  207. # Git
  208. if [[ "${CUR_SOURCE_PROTO}" == "git" ]]; then
  209. GITEXTRA="${CUR_SOURCE_URI##*#}"
  210. URI="${CUR_SOURCE_URI%%#*}"
  211. GITARGS="--bare --depth 1"
  212. if [[ "${GITEXTRA}" != "" ]]; then
  213. GITKEY="${GITEXTRA%%=*}"
  214. GITVAL="${GITEXTRA##*=}"
  215. GITARGS="${GITARGS} --${GITKEY} ${GITVAL}"
  216. fi
  217. git clone ${GITARGS} "${URI}" "${CUR_SOURCE_NAME}"
  218. exit_on_error
  219. # Wget
  220. elif [[ "${CUR_SOURCE_SCHEME}" == "https" || "${CUR_SOURCE_SCHEME}" == "http" ]]; then
  221. wget -O "${CUR_SOURCE_NAME}" "${CUR_SOURCE_URI}"
  222. exit_on_error
  223. fi
  224. verify_checksum "${CUR_SOURCE_NAME}" ${i}
  225. done
  226. # Create 'src' and 'pkg' dirs
  227. if [[ ! -d ${srcdir} ]]; then
  228. mkdir ${srcdir}
  229. fi
  230. if [[ ! -d ${pkgdir} ]]; then
  231. mkdir ${pkgdir}
  232. fi
  233. # Extract sources
  234. if [[ ${OPT_EXTRACT} -eq 1 ]]; then
  235. cd ${srcdir}
  236. for ((i=0; i<${#source[@]}; i++)); do
  237. CUR_SOURCE="${source[${i}]}"
  238. parse_current_source
  239. if [[ "${CUR_SOURCE_PROTO}" == "git" ]]; then
  240. git clone ../"${CUR_SOURCE_NAME}" "${CUR_SOURCE_NAME}"
  241. exit_on_error
  242. else
  243. ln -s ../"${CUR_SOURCE_NAME}" ./"${CUR_SOURCE_NAME}"
  244. exit_on_error
  245. if [[ "${CUR_SOURCE_NAME}" == *.tar.gz ]]; then
  246. tar -xvzf "${CUR_SOURCE_NAME}"
  247. exit_on_error
  248. fi
  249. fi
  250. done
  251. fi
  252. # Set pkgver from pkgver() function
  253. cd ${srcdir}
  254. if [[ $(type -t pkgver) == function ]]; then
  255. pkgver=$(pkgver)
  256. exit_on_error
  257. fi
  258. # Prepare
  259. if [[ ${OPT_PREPARE} -eq 1 ]]; then
  260. prepare
  261. exit_on_error
  262. fi
  263. # Build the sources
  264. if [[ ${OPT_BUILD} -eq 1 ]]; then
  265. build
  266. exit_on_error
  267. fi
  268. # Package
  269. package
  270. exit_on_error
  271. mkdir "${pkgdir}/DEBIAN"
  272. echo "Package: ${pkgname}
  273. Version: ${pkgver}
  274. Maintainer: ${maintainer}
  275. Depends: ${depends}
  276. Architecture: ${arch}
  277. Homepage: ${url}
  278. Description: ${pkgdesc}" \
  279. > "${pkgdir}/DEBIAN/control"
  280. cd "${ROOT}"
  281. dpkg --build "${pkgdir}"
  282. exit_on_error
  283. NEW_PKGNAME="${pkgname}-${pkgver}-${pkgrel}_${arch}.deb"
  284. mv pkg.deb ${NEW_PKGNAME}
  285. exit_on_error
  286. if [[ ${OPT_CLEAN} -eq 1 ]]; then
  287. : # TODO
  288. fi
  289. echo "New package ready at ${NEW_PKGNAME}"