ipkg-build 6.2 KB

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