1
0

ipkg-build 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env bash
  2. # ipkg-build -- construct a .ipk from a directory
  3. # Waldemar Brodkorb <wbx@openadk.org>
  4. # use cpio instead of tar for uid/gid handling
  5. # Carl Worth <cworth@east.isi.edu>
  6. # based on a script by Steve Redler IV <steve@sr-tech.com>
  7. set -e
  8. version=1.0
  9. ipkg_extract_value() {
  10. sed -e "s/^[^:]*:[[:space:]]*//"
  11. }
  12. required_field() {
  13. field=$1
  14. value=`grep "^$field:" < $CONTROL/control | ipkg_extract_value`
  15. if [ -z "$value" ]; then
  16. echo "*** Error: $CONTROL/control is missing field $field" >&2
  17. return 1
  18. fi
  19. echo $value
  20. return 0
  21. }
  22. disallowed_field() {
  23. field=$1
  24. value=`grep "^$field:" < $CONTROL/control | ipkg_extract_value`
  25. if [ -n "$value" ]; then
  26. echo "*** Error: $CONTROL/control contains disallowed field $field" >&2
  27. return 1
  28. fi
  29. echo $value
  30. return 0
  31. }
  32. pkg_appears_sane() {
  33. local pkg_dir=$1
  34. local owd=$PWD
  35. cd $pkg_dir
  36. PKG_ERROR=0
  37. tilde_files=`find . -name '*~'`
  38. if [ -n "$tilde_files" ]; then
  39. if [ "$noclean" = "1" ]; then
  40. echo "*** Warning: The following files have names ending in '~'.
  41. You probably want to remove them: " >&2
  42. ls -ld $tilde_files
  43. echo >&2
  44. else
  45. echo "*** Removing the following files: $tilde_files"
  46. rm -f "$tilde_files"
  47. fi
  48. fi
  49. if [ ! -f "$CONTROL/control" ]; then
  50. echo "*** Error: Control file $pkg_dir/$CONTROL/control not found." >&2
  51. cd $owd
  52. return 1
  53. fi
  54. pkg=`required_field Package`
  55. [ "$?" -ne 0 ] && PKG_ERROR=1
  56. version=`required_field Version | sed 's/Version://; s/^.://g;'`
  57. [ "$?" -ne 0 ] && PKG_ERROR=1
  58. arch=`required_field Architecture`
  59. [ "$?" -ne 0 ] && PKG_ERROR=1
  60. required_field Maintainer >/dev/null
  61. [ "$?" -ne 0 ] && PKG_ERROR=1
  62. required_field Description >/dev/null
  63. [ "$?" -ne 0 ] && PKG_ERROR=1
  64. section=`required_field Section`
  65. [ "$?" -ne 0 ] && PKG_ERROR=1
  66. if [ -z "$section" ]; then
  67. echo "The Section field should have one of the following values:" >&2
  68. echo "admin, base, comm, editors, extras, games, graphics, kernel, lang, libs, misc, net, scm, text, web, x11" >&2
  69. fi
  70. priority=`required_field Priority`
  71. [ "$?" -ne 0 ] && PKG_ERROR=1
  72. if [ -z "$priority" ]; then
  73. echo "The Priority field should have one of the following values:" >&2
  74. echo "required, important, standard, optional, extra." >&2
  75. echo "If you don't know which priority value you should be using, then use \`optional'" >&2
  76. fi
  77. source=`required_field Source`
  78. [ "$?" -ne 0 ] && PKG_ERROR=1
  79. if [ -z "$source" ]; then
  80. echo "The Source field contain the URL's or filenames of the source code and any patches"
  81. echo "used to build this package. Either gnu-style tarballs or Debian source packages "
  82. echo "are acceptable. Relative filenames may be used if they are distributed in the same"
  83. echo "directory as the .ipk file."
  84. fi
  85. disallowed_filename=`disallowed_field Filename`
  86. [ "$?" -ne 0 ] && PKG_ERROR=1
  87. if echo $pkg | grep '[^a-z0-9.+-]'; then
  88. echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
  89. PKG_ERROR=1;
  90. fi
  91. local bad_fields=`sed -ne 's/^\([^[:space:]][^:[:space:]]\+[[:space:]]\+\)[^:].*/\1/p' < $CONTROL/control | sed -e 's/\\n//'`
  92. if [ -n "$bad_fields" ]; then
  93. bad_fields=`echo $bad_fields`
  94. echo "*** Error: The following fields in $CONTROL/control are missing a ':'" >&2
  95. echo " $bad_fields" >&2
  96. echo "ipkg-build: This may be due to a missing initial space for a multi-line field value" >&2
  97. PKG_ERROR=1
  98. fi
  99. for script in $CONTROL/preinst $CONTROL/postinst $CONTROL/prerm $CONTROL/postrm; do
  100. if [ -f $script -a ! -x $script ]; then
  101. if [ "$noclean" = "1" ]; then
  102. echo "*** Error: package script $script is not executable" >&2
  103. PKG_ERROR=1
  104. else
  105. chmod a+x $script
  106. fi
  107. fi
  108. done
  109. if [ -f $CONTROL/conffiles ]; then
  110. for cf in `cat $CONTROL/conffiles`; do
  111. if [ ! -f ./$cf ]; then
  112. echo "*** Error: $CONTROL/conffiles mentions conffile $cf which does not exist" >&2
  113. PKG_ERROR=1
  114. fi
  115. done
  116. fi
  117. cd $owd
  118. return $PKG_ERROR
  119. }
  120. ###
  121. # ipkg-build "main"
  122. ###
  123. noclean=0
  124. usage="Usage: $0 [-C] <pkg_directory> [<destination_directory>]"
  125. while getopts ":h:v" opt; do
  126. case $opt in
  127. C ) noclean=1
  128. ;;
  129. v ) echo $version
  130. exit 0
  131. ;;
  132. h ) echo $usage >&2 ;;
  133. \? ) echo $usage >&2
  134. esac
  135. done
  136. shift $(($OPTIND - 1))
  137. # continue on to process additional arguments
  138. case $# in
  139. 1)
  140. dest_dir=$PWD
  141. ;;
  142. 2)
  143. dest_dir=$2
  144. if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
  145. dest_dir=$PWD
  146. fi
  147. ;;
  148. *)
  149. echo $usage >&2
  150. exit 1
  151. ;;
  152. esac
  153. pkg_dir=$1
  154. if [ ! -d $pkg_dir ]; then
  155. echo "*** Error: Directory $pkg_dir does not exist" >&2
  156. exit 1
  157. fi
  158. # CONTROL is second so that it takes precedence
  159. CONTROL=
  160. [ -d $pkg_dir/DEBIAN ] && CONTROL=DEBIAN
  161. [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
  162. if [ -z "$CONTROL" ]; then
  163. echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
  164. exit 1
  165. fi
  166. if ! pkg_appears_sane $pkg_dir; then
  167. echo >&2
  168. echo "ipkg-build: Please fix the above errors and try again." >&2
  169. exit 1
  170. fi
  171. tmp_dir=$dest_dir/IPKG_BUILD.$$
  172. mkdir $tmp_dir
  173. ( cd $pkg_dir && find . | grep -v $CONTROL | \
  174. sed "s#\(.*\)#:0:0::::::\1#" | \
  175. sort | cpio -o -Hustar -P | gzip -n9 > $tmp_dir/data.tar.gz )
  176. ( cd $pkg_dir/$CONTROL && find . | \
  177. sed "s#\(.*\)#:0:0::::::\1#" | \
  178. sort | cpio -o -Hustar -P | gzip -n9 > $tmp_dir/control.tar.gz )
  179. echo "2.0" > $tmp_dir/debian-binary
  180. pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
  181. rm -f $pkg_file
  182. ( cd $tmp_dir && tar -zcf $pkg_file ./debian-binary ./data.tar.gz ./control.tar.gz )
  183. rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
  184. rmdir $tmp_dir
  185. echo "Packaged contents of $pkg_dir into $pkg_file"