2
0

ipkg-build 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  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. FIND="$(which find)"
  12. FIND="${FIND:-$(which gfind)}"
  13. TAR="${TAR:-$(which tar)}"
  14. GZIP="$(which gzip)"
  15. # look up date of last commit
  16. if [ -d "$TOPDIR/.git" ]; then
  17. GIT="$(which git)"
  18. TIMESTAMP=$(cd $TOPDIR; $GIT log -1 -s --format=%ci)
  19. elif [ -d "$TOPDIR/.svn" ]; then
  20. SVN="$(which svn)"
  21. TIMESTAMP=$($SVN info "$TOPDIR" | sed -n "s/^Last Changed Date: \(.*\)/\1/p")
  22. else
  23. TIMESTAMP=$(date)
  24. fi
  25. ipkg_extract_value() {
  26. sed -e "s/^[^:]*:[[:space:]]*//"
  27. }
  28. required_field() {
  29. field=$1
  30. grep "^$field:" < $CONTROL/control | ipkg_extract_value
  31. }
  32. pkg_appears_sane() {
  33. local pkg_dir=$1
  34. local owd=$PWD
  35. cd $pkg_dir
  36. PKG_ERROR=0
  37. pkg=`required_field Package`
  38. version=`required_field Version | sed 's/Version://; s/^.://g;'`
  39. arch=`required_field Architecture`
  40. if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
  41. echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
  42. PKG_ERROR=1;
  43. fi
  44. if [ -f $CONTROL/conffiles ]; then
  45. rm -f $CONTROL/conffiles.resolved
  46. for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
  47. echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
  48. done
  49. rm $CONTROL/conffiles
  50. mv $CONTROL/conffiles.resolved $CONTROL/conffiles
  51. chmod 0644 $CONTROL/conffiles
  52. fi
  53. cd $owd
  54. return $PKG_ERROR
  55. }
  56. ###
  57. # ipkg-build "main"
  58. ###
  59. ogargs=""
  60. noclean=0
  61. usage="Usage: $0 [-c] [-C] [-o owner] [-g group] <pkg_directory> [<destination_directory>]"
  62. while getopts "cg:ho:v" opt; do
  63. case $opt in
  64. o ) owner=$OPTARG
  65. ogargs="--owner=$owner"
  66. ;;
  67. g ) group=$OPTARG
  68. ogargs="$ogargs --group=$group"
  69. ;;
  70. c ) ;;
  71. C ) noclean=1;;
  72. v ) echo $version
  73. exit 0
  74. ;;
  75. h ) echo $usage >&2 ;;
  76. \? ) echo $usage >&2
  77. esac
  78. done
  79. shift $(($OPTIND - 1))
  80. # continue on to process additional arguments
  81. case $# in
  82. 1)
  83. dest_dir=$PWD
  84. ;;
  85. 2)
  86. dest_dir=$2
  87. if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
  88. dest_dir=$PWD
  89. fi
  90. ;;
  91. *)
  92. echo $usage >&2
  93. exit 1
  94. ;;
  95. esac
  96. pkg_dir=$1
  97. if [ ! -d $pkg_dir ]; then
  98. echo "*** Error: Directory $pkg_dir does not exist" >&2
  99. exit 1
  100. fi
  101. # CONTROL is second so that it takes precedence
  102. CONTROL=
  103. [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
  104. if [ -z "$CONTROL" ]; then
  105. echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
  106. exit 1
  107. fi
  108. if ! pkg_appears_sane $pkg_dir; then
  109. echo >&2
  110. echo "ipkg-build: Please fix the above errors and try again." >&2
  111. exit 1
  112. fi
  113. tmp_dir=$dest_dir/IPKG_BUILD.$$
  114. mkdir $tmp_dir
  115. echo $CONTROL > $tmp_dir/tarX
  116. # Preserve permissions (-p) when creating data.tar.gz as non-root user
  117. ( cd $pkg_dir && $TAR $ogargs -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz )
  118. installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
  119. sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
  120. $pkg_dir/$CONTROL/control
  121. ( cd $pkg_dir/$CONTROL && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
  122. rm $tmp_dir/tarX
  123. echo "2.0" > $tmp_dir/debian-binary
  124. pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
  125. rm -f $pkg_file
  126. ( cd $tmp_dir && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
  127. rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
  128. rmdir $tmp_dir
  129. echo "Packaged contents of $pkg_dir into $pkg_file"